Skip to content

Commit db05057

Browse files
committed
+ AsyncStreamable
1 parent 663a131 commit db05057

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+
// AsyncStreamable.swift
3+
// ComposableArchitecturePattern
4+
//
5+
// Created by Jonathan Holland on 10/5/25.
6+
//
7+
8+
/// A protocol for streaming a value.
9+
public protocol _AsyncStreamable {
10+
associatedtype Value: Sendable
11+
/// The underlying value to be streamed.
12+
var value: AsyncStreamPublisher<Value> { get }
13+
}
14+
15+
/// An actor to handle streaming for a value.
16+
public actor AsyncStreamable<T: Sendable>: _AsyncStreamable {
17+
nonisolated(unsafe) public var value: AsyncStreamPublisher<T>
18+
19+
/// Create a new `AsyncStreamable` with the specified `AsyncStreamPublisher`.
20+
public init(value: AsyncStreamPublisher<T>) {
21+
self.value = value
22+
}
23+
24+
/// Create a new `AsyncStreamable` with the specified underlying value.
25+
public init(value: T) {
26+
self.value = AsyncStreamPublisher(wrappedValue: value)
27+
}
28+
29+
/// Update the underlying value with the new value.
30+
public func update(to newValue: T) {
31+
self.value.wrappedValue = newValue
32+
}
33+
34+
/// A new stream on the underlying value.
35+
public func stream() async -> AsyncStream<T> {
36+
self.value.newStream()
37+
}
38+
}

0 commit comments

Comments
 (0)