Skip to content

Commit e739815

Browse files
committed
[NetLens] [Observe NetworkCalls use case] - prepare helpers methods to simulate wait for stream value
1 parent 40cb2af commit e739815

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// NetworkCall.swift
3+
// NetLens
4+
//
5+
// Created by NetLens on 25/06/2025.
6+
//
7+
8+
import Foundation
9+
10+
enum AsyncTestError: Error {
11+
case timedOut
12+
}
13+
14+
extension AsyncStream {
15+
func waitForStreamValue<T:Sendable> (from stream: AsyncStream<T>, timeout: TimeInterval = 1.0) async throws -> T {
16+
try await withThrowingTaskGroup(of: T?.self) { group in
17+
18+
group.addTask {
19+
var iterator = stream.makeAsyncIterator()
20+
21+
return await iterator.next()
22+
}
23+
24+
guard let result = try await group.next() else {
25+
throw AsyncTestError.timedOut
26+
}
27+
28+
group.cancelAll()
29+
30+
guard let result else { throw AsyncTestError.timedOut }
31+
32+
return result
33+
34+
}
35+
}
36+
}
37+

0 commit comments

Comments
 (0)