Skip to content

Commit 83992aa

Browse files
committed
use checked continuations
1 parent 369ddaf commit 83992aa

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

Sources/Operators/AsyncSwitchToLatestSequence.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ where Base.Element: AsyncSequence, Base: Sendable, Base.Element.Element: Sendabl
4646
enum BaseState {
4747
case notStarted
4848
case idle
49-
case waitingForChildIterator(UnsafeContinuation<Task<ChildValue?, Never>?, Never>)
49+
case waitingForChildIterator(CheckedContinuation<Task<ChildValue?, Never>?, Never>)
5050
case newChildIteratorAvailable(Result<Base.Element.AsyncIterator, Error>)
5151
case processingChildIterator(Result<Base.Element.AsyncIterator, Error>)
5252
case finished(Result<Base.Element.AsyncIterator, Error>?)
@@ -92,7 +92,7 @@ where Base.Element: AsyncSequence, Base: Sendable, Base.Element.Element: Sendabl
9292
}
9393

9494
enum BaseDecision {
95-
case resumeNext(UnsafeContinuation<Task<ChildValue?, Never>?, Never>, Task<ChildValue?, Never>?)
95+
case resumeNext(CheckedContinuation<Task<ChildValue?, Never>?, Never>, Task<ChildValue?, Never>?)
9696
case cancelPreviousChildTask(Task<ChildValue?, Never>?)
9797
}
9898

@@ -221,14 +221,9 @@ where Base.Element: AsyncSequence, Base: Sendable, Base.Element.Element: Sendabl
221221
guard !Task.isCancelled else { return nil }
222222
self.startBase()
223223

224-
return try await withTaskCancellationHandler { [baseTask, state] in
225-
baseTask?.cancel()
226-
state.withCriticalRegion {
227-
$0.childTask?.cancel()
228-
}
229-
} operation: {
224+
return try await withTaskCancellationHandler {
230225
while true {
231-
let childTask = await withUnsafeContinuation { [state] (continuation: UnsafeContinuation<Task<ChildValue?, Never>?, Never>) in
226+
let childTask = await withCheckedContinuation { [state] (continuation: CheckedContinuation<Task<ChildValue?, Never>?, Never>) in
232227
let decision = state.withCriticalRegion { state -> NextDecision in
233228
switch state.base {
234229
case .newChildIteratorAvailable(let childIterator):
@@ -303,6 +298,11 @@ where Base.Element: AsyncSequence, Base: Sendable, Base.Element.Element: Sendabl
303298
return try element._rethrowGet()
304299
}
305300
}
301+
} onCancel: { [baseTask, state] in
302+
baseTask?.cancel()
303+
state.withCriticalRegion {
304+
$0.childTask?.cancel()
305+
}
306306
}
307307
}
308308
}

0 commit comments

Comments
 (0)