@@ -31,12 +31,11 @@ extension AsyncStream {
31
31
/// }
32
32
/// ```
33
33
///
34
- /// While your tests can use `AsyncStream.streamWithContinuation` to spin up a controllable stream
35
- /// for tests:
34
+ /// While your tests can use `AsyncStream.makeStream` to spin up a controllable stream for tests:
36
35
///
37
36
/// ```swift
38
37
/// func testScreenshots() {
39
- /// let screenshots = AsyncStream<Void>.streamWithContinuation( )
38
+ /// let screenshots = AsyncStream.makeStream(of: Void.self )
40
39
///
41
40
/// let model = withDependencies {
42
41
/// $0.screenshots = { screenshots.stream }
@@ -61,59 +60,65 @@ extension AsyncStream {
61
60
}
62
61
}
63
62
64
- /// Constructs and returns a stream along with its backing continuation.
65
- ///
66
- /// This is handy for immediately escaping the continuation from an async stream, which typically
67
- /// requires multiple steps:
68
- ///
69
- /// ```swift
70
- /// var _continuation: AsyncStream<Int>.Continuation!
71
- /// let stream = AsyncStream<Int> { continuation = $0 }
72
- /// let continuation = _continuation!
73
- ///
74
- /// // vs.
75
- ///
76
- /// let (stream, continuation) = AsyncStream<Int>.streamWithContinuation()
77
- /// ```
78
- ///
79
- /// This tool is usually used for tests where we need to supply an async sequence to a dependency
80
- /// endpoint and get access to its continuation so that we can emulate the dependency emitting
81
- /// data. For example, suppose you have a dependency exposing an async sequence for listening to
82
- /// notifications. To test this you can use `streamWithContinuation`:
83
- ///
84
- /// ```swift
85
- /// func testScreenshots() {
86
- /// let screenshots = AsyncStream<Void>.streamWithContinuation()
87
- ///
88
- /// let model = withDependencies {
89
- /// $0.screenshots = { screenshots.stream }
90
- /// } operation: {
91
- /// FeatureModel()
92
- /// }
93
- ///
94
- /// XCTAssertEqual(model.screenshotCount, 0)
95
- /// screenshots.continuation.yield() // Simulate a screenshot being taken.
96
- /// XCTAssertEqual(model.screenshotCount, 1)
97
- /// }
98
- /// ```
99
- ///
100
- /// > Warning: ⚠️ `AsyncStream` does not support multiple subscribers, therefore you can only use
101
- /// > this helper to test features that do not subscribe multiple times to the dependency
102
- /// > endpoint.
103
- ///
104
- /// - Parameters:
105
- /// - elementType: The type of element the `AsyncStream` produces.
106
- /// - limit: A Continuation.BufferingPolicy value to set the stream’s buffering behavior. By
107
- /// default, the stream buffers an unlimited number of elements. You can also set the policy
108
- /// to buffer a specified number of oldest or newest elements.
109
- /// - Returns: An `AsyncStream`.
110
- public static func streamWithContinuation(
111
- _ elementType: Element . Type = Element . self,
112
- bufferingPolicy limit: Continuation . BufferingPolicy = . unbounded
113
- ) -> ( stream: Self , continuation: Continuation ) {
114
- var continuation : Continuation !
115
- return ( Self ( elementType, bufferingPolicy: limit) { continuation = $0 } , continuation)
116
- }
63
+ #if swift(<5.9)
64
+ /// Constructs and returns a stream along with its backing continuation.
65
+ ///
66
+ /// A back-port of [SE-0388: Convenience Async[Throwing]Stream.makeStream methods][se-0388].
67
+ ///
68
+ /// This is handy for immediately escaping the continuation from an async stream, which
69
+ /// typically requires multiple steps:
70
+ ///
71
+ /// ```swift
72
+ /// var _continuation: AsyncStream<Int>.Continuation!
73
+ /// let stream = AsyncStream<Int> { continuation = $0 }
74
+ /// let continuation = _continuation!
75
+ ///
76
+ /// // vs.
77
+ ///
78
+ /// let (stream, continuation) = AsyncStream.makeStream(of: Int.self)
79
+ /// ```
80
+ ///
81
+ /// This tool is usually used for tests where we need to supply an async sequence to a
82
+ /// dependency endpoint and get access to its continuation so that we can emulate the dependency
83
+ /// emitting data. For example, suppose you have a dependency exposing an async sequence for
84
+ /// listening to notifications. To test this you can use `makeStream`:
85
+ ///
86
+ /// ```swift
87
+ /// func testScreenshots() {
88
+ /// let screenshots = AsyncStream.makeStream(of: Void.self)
89
+ ///
90
+ /// let model = withDependencies {
91
+ /// $0.screenshots = { screenshots.stream }
92
+ /// } operation: {
93
+ /// FeatureModel()
94
+ /// }
95
+ ///
96
+ /// XCTAssertEqual(model.screenshotCount, 0)
97
+ /// screenshots.continuation.yield() // Simulate a screenshot being taken.
98
+ /// XCTAssertEqual(model.screenshotCount, 1)
99
+ /// }
100
+ /// ```
101
+ ///
102
+ /// > Warning: ⚠️ `AsyncStream` does not support multiple subscribers, therefore you can only
103
+ /// > use this helper to test features that do not subscribe multiple times to the dependency
104
+ /// > endpoint.
105
+ ///
106
+ /// [se-0388]: https://github.com/apple/swift-evolution/blob/main/proposals/0388-async-stream-factory.md
107
+ ///
108
+ /// - Parameters:
109
+ /// - elementType: The type of element the `AsyncStream` produces.
110
+ /// - limit: A Continuation.BufferingPolicy value to set the stream’s buffering behavior. By
111
+ /// default, the stream buffers an unlimited number of elements. You can also set the policy
112
+ /// to buffer a specified number of oldest or newest elements.
113
+ /// - Returns: An `AsyncStream`.
114
+ public static func makeStream(
115
+ of elementType: Element . Type = Element . self,
116
+ bufferingPolicy limit: Continuation . BufferingPolicy = . unbounded
117
+ ) -> ( stream: Self , continuation: Continuation ) {
118
+ var continuation : Continuation !
119
+ return ( Self ( elementType, bufferingPolicy: limit) { continuation = $0 } , continuation)
120
+ }
121
+ #endif
117
122
118
123
/// An `AsyncStream` that never emits and never completes unless cancelled.
119
124
public static var never : Self {
0 commit comments