File tree 3 files changed +25
-4
lines changed
3 files changed +25
-4
lines changed Original file line number Diff line number Diff line change
1
+ import Foundation
2
+
1
3
extension AsyncStream {
2
4
/// Produces an `AsyncStream` from an `AsyncSequence` by consuming the sequence till it
3
5
/// terminates, ignoring any failure.
@@ -51,10 +53,13 @@ extension AsyncStream {
51
53
///
52
54
/// - Parameter sequence: An async sequence.
53
55
public init < S: AsyncSequence > ( _ sequence: S ) where S. Element == Element {
56
+ let lock = NSLock ( )
54
57
var iterator : S . AsyncIterator ?
55
58
self . init {
56
- if iterator == nil {
57
- iterator = sequence. makeAsyncIterator ( )
59
+ lock. withLock {
60
+ if iterator == nil {
61
+ iterator = sequence. makeAsyncIterator ( )
62
+ }
58
63
}
59
64
return try ? await iterator? . next ( )
60
65
}
Original file line number Diff line number Diff line change
1
+ import Foundation
2
+
1
3
extension AsyncThrowingStream where Failure == Error {
2
4
/// Produces an `AsyncThrowingStream` from an `AsyncSequence` by consuming the sequence till it
3
5
/// terminates, rethrowing any failure.
4
6
///
5
7
/// - Parameter sequence: An async sequence.
6
8
public init < S: AsyncSequence > ( _ sequence: S ) where S. Element == Element {
9
+ let lock = NSLock ( )
7
10
var iterator : S . AsyncIterator ?
8
11
self . init {
9
- if iterator == nil {
10
- iterator = sequence. makeAsyncIterator ( )
12
+ lock. withLock {
13
+ if iterator == nil {
14
+ iterator = sequence. makeAsyncIterator ( )
15
+ }
11
16
}
12
17
return try await iterator? . next ( )
13
18
}
Original file line number Diff line number Diff line change
1
+ import Foundation
2
+
3
+ #if !(os(iOS) || os(macOS) || os(tvOS) || os(watchOS))
4
+ extension NSLock {
5
+ func withLock< R> ( _ body: ( ) throws -> R ) rethrows -> R {
6
+ self . lock ( )
7
+ defer { self . unlock ( ) }
8
+ return try body ( )
9
+ }
10
+ }
11
+ #endif
You can’t perform that action at this time.
0 commit comments