Skip to content

Commit 07518fe

Browse files
committed
update for new withTaskCancellationHandler parameter order
1 parent d079f05 commit 07518fe

15 files changed

+83
-83
lines changed

Sources/AsyncChannels/AsyncBufferedChannel.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,27 +157,7 @@ public final class AsyncBufferedChannel<Element>: AsyncSequence, Sendable {
157157
let awaitingId = self.generateId()
158158
let cancellation = ManagedCriticalState<Bool>(false)
159159

160-
return await withTaskCancellationHandler { [state] in
161-
let awaiting = state.withCriticalRegion { state -> Awaiting? in
162-
cancellation.withCriticalRegion { cancellation in
163-
cancellation = true
164-
}
165-
switch state {
166-
case .awaiting(var awaitings):
167-
let awaiting = awaitings.remove(.placeHolder(id: awaitingId))
168-
if awaitings.isEmpty {
169-
state = .idle
170-
} else {
171-
state = .awaiting(awaitings)
172-
}
173-
return awaiting
174-
default:
175-
return nil
176-
}
177-
}
178-
179-
awaiting?.continuation?.resume(returning: nil)
180-
} operation: {
160+
return await withTaskCancellationHandler {
181161
await withCheckedContinuation { [state] (continuation: CheckedContinuation<Element?, Never>) in
182162
let decision = state.withCriticalRegion { state -> AwaitingDecision in
183163
let isCancelled = cancellation.withCriticalRegion { $0 }
@@ -218,6 +198,26 @@ public final class AsyncBufferedChannel<Element>: AsyncSequence, Sendable {
218198
onSuspend?()
219199
}
220200
}
201+
} onCancel: { [state] in
202+
let awaiting = state.withCriticalRegion { state -> Awaiting? in
203+
cancellation.withCriticalRegion { cancellation in
204+
cancellation = true
205+
}
206+
switch state {
207+
case .awaiting(var awaitings):
208+
let awaiting = awaitings.remove(.placeHolder(id: awaitingId))
209+
if awaitings.isEmpty {
210+
state = .idle
211+
} else {
212+
state = .awaiting(awaitings)
213+
}
214+
return awaiting
215+
default:
216+
return nil
217+
}
218+
}
219+
220+
awaiting?.continuation?.resume(returning: nil)
221221
}
222222
}
223223

Sources/AsyncChannels/AsyncThrowingBufferedChannel.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -178,27 +178,7 @@ public final class AsyncThrowingBufferedChannel<Element, Failure: Error>: AsyncS
178178
let awaitingId = self.generateId()
179179
let cancellation = ManagedCriticalState<Bool>(false)
180180

181-
return try await withTaskCancellationHandler { [state] in
182-
let awaiting = state.withCriticalRegion { state -> Awaiting? in
183-
cancellation.withCriticalRegion { cancellation in
184-
cancellation = true
185-
}
186-
switch state {
187-
case .awaiting(var awaitings):
188-
let awaiting = awaitings.remove(.placeHolder(id: awaitingId))
189-
if awaitings.isEmpty {
190-
state = .idle
191-
} else {
192-
state = .awaiting(awaitings)
193-
}
194-
return awaiting
195-
default:
196-
return nil
197-
}
198-
}
199-
200-
awaiting?.continuation?.resume(returning: nil)
201-
} operation: {
181+
return try await withTaskCancellationHandler {
202182
try await withUnsafeThrowingContinuation { [state] (continuation: UnsafeContinuation<Element?, Error>) in
203183
let decision = state.withCriticalRegion { state -> AwaitingDecision in
204184
let isCancelled = cancellation.withCriticalRegion { $0 }
@@ -245,6 +225,26 @@ public final class AsyncThrowingBufferedChannel<Element, Failure: Error>: AsyncS
245225
onSuspend?()
246226
}
247227
}
228+
} onCancel: { [state] in
229+
let awaiting = state.withCriticalRegion { state -> Awaiting? in
230+
cancellation.withCriticalRegion { cancellation in
231+
cancellation = true
232+
}
233+
switch state {
234+
case .awaiting(var awaitings):
235+
let awaiting = awaitings.remove(.placeHolder(id: awaitingId))
236+
if awaitings.isEmpty {
237+
state = .idle
238+
} else {
239+
state = .awaiting(awaitings)
240+
}
241+
return awaiting
242+
default:
243+
return nil
244+
}
245+
}
246+
247+
awaiting?.continuation?.resume(returning: nil)
248248
}
249249
}
250250

Sources/AsyncSubjects/AsyncCurrentValueSubject.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ public final class AsyncCurrentValueSubject<Element>: AsyncSubject where Element
137137
}
138138

139139
public mutating func next() async -> Element? {
140-
await withTaskCancellationHandler { [unregister] in
141-
unregister()
142-
} operation: {
140+
await withTaskCancellationHandler {
143141
await self.iterator.next()
142+
} onCancel: { [unregister] in
143+
unregister()
144144
}
145145
}
146146
}

Sources/AsyncSubjects/AsyncPassthroughSubject.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ public final class AsyncPassthroughSubject<Element>: AsyncSubject {
120120
}
121121

122122
public mutating func next() async -> Element? {
123-
await withTaskCancellationHandler { [unregister] in
124-
unregister()
125-
} operation: {
123+
await withTaskCancellationHandler {
126124
await self.iterator.next()
125+
} onCancel: { [unregister] in
126+
unregister()
127127
}
128128
}
129129
}

Sources/AsyncSubjects/AsyncReplaySubject.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ public final class AsyncReplaySubject<Element>: AsyncSubject where Element: Send
124124
}
125125

126126
public mutating func next() async -> Element? {
127-
await withTaskCancellationHandler { [unregister] in
128-
unregister()
129-
} operation: {
127+
await withTaskCancellationHandler {
130128
await self.iterator.next()
129+
} onCancel: { [unregister] in
130+
unregister()
131131
}
132132
}
133133
}

Sources/AsyncSubjects/AsyncThrowingCurrentValueSubject.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ public final class AsyncThrowingCurrentValueSubject<Element, Failure: Error>: As
148148
}
149149

150150
public mutating func next() async throws -> Element? {
151-
try await withTaskCancellationHandler { [unregister] in
152-
unregister()
153-
} operation: {
151+
try await withTaskCancellationHandler {
154152
try await self.iterator.next()
153+
} onCancel: { [unregister] in
154+
unregister()
155155
}
156156
}
157157
}

Sources/AsyncSubjects/AsyncThrowingPassthroughSubject.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ public final class AsyncThrowingPassthroughSubject<Element, Failure: Error>: Asy
132132
}
133133

134134
public mutating func next() async throws -> Element? {
135-
try await withTaskCancellationHandler { [unregister] in
136-
unregister()
137-
} operation: {
135+
try await withTaskCancellationHandler {
138136
try await self.iterator.next()
137+
} onCancel: { [unregister] in
138+
unregister()
139139
}
140140
}
141141
}

Sources/AsyncSubjects/AsyncThrowingReplaySubject.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ public final class AsyncThrowingReplaySubject<Element, Failure: Error>: AsyncSub
134134
}
135135

136136
public mutating func next() async throws -> Element? {
137-
try await withTaskCancellationHandler { [unregister] in
138-
unregister()
139-
} operation: {
137+
try await withTaskCancellationHandler {
140138
try await self.iterator.next()
139+
} onCancel: { [unregister] in
140+
unregister()
141141
}
142142
}
143143
}

Sources/Combiners/Merge/MergeStateMachine.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,6 @@ struct MergeStateMachine<Element>: Sendable {
196196

197197
func next() async -> RegulatedElement<Element> {
198198
await withTaskCancellationHandler {
199-
self.unsuspendAndClearOnCancel()
200-
} operation: {
201199
self.requestNextRegulatedElements()
202200

203201
let regulatedElement = await withUnsafeContinuation { (continuation: UnsafeContinuation<RegulatedElement<Element>, Never>) in
@@ -244,6 +242,8 @@ struct MergeStateMachine<Element>: Sendable {
244242
}
245243

246244
return regulatedElement
245+
} onCancel: {
246+
self.unsuspendAndClearOnCancel()
247247
}
248248
}
249249
}

Sources/Combiners/WithLatestFrom/AsyncWithLatestFrom2Sequence.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,7 @@ where Other1: Sendable, Other2: Sendable, Other1.Element: Sendable, Other2.Eleme
172172
let shouldReturnNil = self.isTerminated.withCriticalRegion { $0 }
173173
guard !shouldReturnNil else { return nil }
174174

175-
return try await withTaskCancellationHandler { [isTerminated, othersTask] in
176-
isTerminated.withCriticalRegion { isTerminated in
177-
isTerminated = true
178-
}
179-
othersTask?.cancel()
180-
} operation: { [othersTask, othersState, onBaseElement] in
175+
return try await withTaskCancellationHandler { [othersTask, othersState, onBaseElement] in
181176
do {
182177
while true {
183178
guard let baseElement = try await self.base.next() else {
@@ -219,6 +214,11 @@ where Other1: Sendable, Other2: Sendable, Other1.Element: Sendable, Other2.Eleme
219214
othersTask?.cancel()
220215
throw error
221216
}
217+
} onCancel: { [isTerminated, othersTask] in
218+
isTerminated.withCriticalRegion { isTerminated in
219+
isTerminated = true
220+
}
221+
othersTask?.cancel()
222222
}
223223
}
224224
}

0 commit comments

Comments
 (0)