Skip to content

Commit 6a13624

Browse files
Fix: Fixed 3 issue with mapStream
1 parent 3071750 commit 6a13624

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

Sources/GraphQL/Subscription/EventStream.swift

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,42 @@ public class ConcurrentEventStream<Element>: EventStream<Element> {
3131
extension AsyncThrowingStream {
3232
func mapStream<To>(_ closure: @escaping (Element) throws -> To) -> AsyncThrowingStream<To, Error> {
3333
return AsyncThrowingStream<To, Error> { continuation in
34-
Task {
35-
for try await event in self {
36-
let newEvent = try closure(event)
37-
continuation.yield(newEvent)
34+
let task = Task {
35+
do {
36+
for try await event in self {
37+
let newEvent = try closure(event)
38+
continuation.yield(newEvent)
39+
}
40+
continuation.finish()
41+
} catch {
42+
continuation.finish(throwing: error)
3843
}
3944
}
45+
46+
continuation.onTermination = { @Sendable _ in
47+
task.cancel()
48+
}
4049
}
4150
}
4251

4352
func filterStream(_ isIncluded: @escaping (Element) throws -> Bool) -> AsyncThrowingStream<Element, Error> {
4453
return AsyncThrowingStream<Element, Error> { continuation in
45-
Task {
46-
for try await event in self {
47-
if try isIncluded(event) {
48-
continuation.yield(event)
54+
let task = Task {
55+
do {
56+
for try await event in self {
57+
if try isIncluded(event) {
58+
continuation.yield(event)
59+
}
4960
}
61+
continuation.finish()
62+
} catch {
63+
continuation.finish(throwing: error)
5064
}
5165
}
66+
67+
continuation.onTermination = { @Sendable _ in
68+
task.cancel()
69+
}
5270
}
5371
}
5472
}

0 commit comments

Comments
 (0)