-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add state and init observation to manager (#48)
* Add state and init observation to manager * Add a comment to RootViewModel.manager
- Loading branch information
Showing
6 changed files
with
262 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Foundation | ||
|
||
/// Send a value to multiple observers | ||
actor Broadcaster<Element: Sendable> { | ||
typealias Identifier = UUID | ||
private var continuations: [Identifier: AsyncStream<Element>.Continuation] = [:] | ||
|
||
func values() -> AsyncStream<Element> { | ||
.init { continuation in | ||
let id = Identifier() | ||
continuations[id] = continuation | ||
|
||
continuation.onTermination = { _ in | ||
Task { [weak self] in | ||
await self?.remove(id) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func remove(_ id: Identifier) { | ||
continuations[id] = nil | ||
} | ||
|
||
func send(_ value: Element) { | ||
continuations.values.forEach { $0.yield(value) } | ||
} | ||
|
||
deinit { | ||
continuations.values.forEach { $0.finish() } | ||
} | ||
} |
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,29 @@ | ||
import Foundation | ||
|
||
/// When bridging from a sync to async context using multiple `Task`s, order of execution is not guaranteed. | ||
/// Using an `AsyncStream` we can bridge enqueued work to an async context within a single `Task`. | ||
/// https://forums.swift.org/t/a-pitfall-when-using-didset-and-task-together-order-cant-be-guaranteed/71311/6 | ||
final class Queue { | ||
typealias Operation = @Sendable () async -> Void | ||
private let continuation: AsyncStream<Operation>.Continuation | ||
private let task: Task<Void, Never> | ||
|
||
init() { | ||
let (stream, continuation) = AsyncStream.makeStream(of: Operation.self) | ||
|
||
self.continuation = continuation | ||
self.task = Task { | ||
for await operation in stream { | ||
await operation() | ||
} | ||
} | ||
} | ||
|
||
func enqueue(_ operation: @escaping Operation) { | ||
continuation.yield(operation) | ||
} | ||
|
||
deinit { | ||
task.cancel() | ||
} | ||
} |
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
Oops, something went wrong.