Skip to content

Commit 663a131

Browse files
committed
+ AsyncStreamPublisher
1 parent 06fb7a3 commit 663a131

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}

0 commit comments

Comments
 (0)