File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Sources/ComposableArchitecturePattern Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ //
2+ // AsyncStreamPublisher.swift
3+ // ComposableArchitecturePattern
4+ //
5+ // Created by Jonathan Holland on 10/5/25.
6+ //
7+
8+ /// A property wrapper for a value that can handle publishing streams.
9+ @propertyWrapper public struct AsyncStreamPublisher < Value: Sendable > : Sendable {
10+ public var wrappedValue : Value {
11+ didSet {
12+ self . _publish ( self . wrappedValue)
13+ }
14+ }
15+
16+ /// Create a new `AsyncStreamPublisher` with the specified value.
17+ public init ( wrappedValue: Value ) {
18+ self . wrappedValue = wrappedValue
19+ }
20+
21+ /// A new stream for the underlying value.
22+ public mutating func newStream( ) -> AsyncStream < Value > {
23+ AsyncStream { continuation in
24+ self . _streams. append ( continuation)
25+ continuation. yield ( self . wrappedValue)
26+ }
27+ }
28+
29+ /// The streams that have been created to listen to the underlying value.
30+ private var _streams : [ AsyncStream < Value > . Continuation ] = [ ]
31+
32+ /// Publish the new value to the streams.
33+ private func _publish( _ value: Value ) {
34+ for continuation in self . _streams {
35+ continuation. yield ( value)
36+ }
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments