This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da1f1fd
commit ea99e90
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// Copyright 2023 Signal Messenger, LLC | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
// | ||
|
||
import Foundation | ||
|
||
/// Scheduler that always runs any work synchronously on the current thread. | ||
/// Useful if you don't care what thread your work runs on and want to incur | ||
/// the least overhead. Should NOT be used for scheduled methods, e.g.: | ||
/// `promise.after(seconds: 10, on: SyncScheduler()` would be a bad form and | ||
/// fall back to scheduling in the future on the main thread. | ||
public class SyncScheduler: Scheduler { | ||
|
||
public init() {} | ||
|
||
public func async(_ work: @escaping () -> Void) { | ||
work() | ||
} | ||
|
||
public func sync(_ work: () -> Void) { | ||
work() | ||
} | ||
|
||
public func sync<T>(_ work: () throws -> T) rethrows -> T { | ||
return try work() | ||
} | ||
|
||
public func sync<T>(_ work: () -> T) -> T { | ||
return work() | ||
} | ||
|
||
public func asyncAfter(deadline: DispatchTime, _ work: @escaping () -> Void) { | ||
owsFailDebug("Should not schedule on async queue. Using main queue instead.") | ||
DispatchQueue.main.asyncAfter(deadline: deadline, work) | ||
} | ||
|
||
public func asyncAfter(wallDeadline: DispatchWallTime, _ work: @escaping () -> Void) { | ||
owsFailDebug("Should not schedule on async queue. Using main queue instead.") | ||
DispatchQueue.main.asyncAfter(wallDeadline: wallDeadline, work) | ||
} | ||
|
||
public func asyncIfNecessary(execute work: @escaping () -> Void) { | ||
work() | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters