-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from fwcd/async-await
Begin migrating to Swift Concurrency by introducing async/await at the top-level
- Loading branch information
Showing
5 changed files
with
18 additions
and
32 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
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 |
---|---|---|
@@ -1,38 +1,22 @@ | ||
import Dispatch | ||
|
||
/// A simple pub/sub facility for publishing log messages. | ||
class LogOutput { | ||
private static let dispatchQueue = DispatchQueue(label: "D2.LogOutput") | ||
|
||
actor LogOutput { | ||
private var isSilenced: Bool = false | ||
private var consumers: [(String) -> Void] = [] | ||
|
||
/// Registers a consumer for log messages. | ||
private func register(_ consumer: @escaping (String) -> Void) { | ||
func register(_ consumer: @escaping (String) -> Void) { | ||
consumers.append(consumer) | ||
} | ||
|
||
/// Publishes a log message. | ||
private func publish(_ message: String) { | ||
func publish(_ message: String) { | ||
guard !isSilenced else { return } | ||
isSilenced = true | ||
for consumer in consumers { | ||
consumer(message) | ||
} | ||
isSilenced = false | ||
} | ||
|
||
/// Registers a consumer for log messages asynchronously. | ||
func registerAsync(_ consumer: @escaping (String) -> Void) { | ||
Self.dispatchQueue.async { | ||
self.consumers.append(consumer) | ||
} | ||
} | ||
|
||
/// Publishes a log message asynchronously. | ||
func publishAsync(_ message: String) { | ||
guard !isSilenced else { return } | ||
Self.dispatchQueue.async { | ||
self.publish(message) | ||
} | ||
} | ||
} |