Skip to content

Commit 1bd0739

Browse files
authored
Deprecation Housekeeping (#206)
`withTaskCancellationHandler<T>(_:, operation:)` -> `withTaskCancellationHandler<T>(operation:onCancel:)`
1 parent 314b10c commit 1bd0739

File tree

8 files changed

+35
-35
lines changed

8 files changed

+35
-35
lines changed

Sources/AsyncAlgorithms/AsyncBufferSequence.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,15 @@ extension AsyncBufferSequence: AsyncSequence {
265265

266266
func next() async rethrows -> Element? {
267267
let result: Result<Element?, Error> = await withTaskCancellationHandler {
268-
task?.cancel()
269-
} operation: {
270268
do {
271269
let value = try await state.next(buffer: buffer)
272270
return .success(value)
273271
} catch {
274272
task?.cancel()
275273
return .failure(error)
276274
}
275+
} onCancel: {
276+
task?.cancel()
277277
}
278278
return try result._rethrowGet()
279279
}

Sources/AsyncAlgorithms/AsyncChannel.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
4040
let generation = channel.establish()
4141
let nextTokenStatus = ManagedCriticalState<ChannelTokenStatus>(.new)
4242

43-
let value = await withTaskCancellationHandler { [channel] in
44-
channel.cancelNext(nextTokenStatus, generation)
45-
} operation: {
43+
let value = await withTaskCancellationHandler {
4644
await channel.next(nextTokenStatus, generation)
45+
} onCancel: { [channel] in
46+
channel.cancelNext(nextTokenStatus, generation)
4747
}
4848

4949
if let value {
@@ -237,10 +237,10 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
237237
let generation = establish()
238238
let sendTokenStatus = ManagedCriticalState<ChannelTokenStatus>(.new)
239239

240-
await withTaskCancellationHandler { [weak self] in
241-
self?.cancelSend(sendTokenStatus, generation)
242-
} operation: {
240+
await withTaskCancellationHandler {
243241
await send(sendTokenStatus, generation, element)
242+
} onCancel: { [weak self] in
243+
self?.cancelSend(sendTokenStatus, generation)
244244
}
245245
}
246246

Sources/AsyncAlgorithms/AsyncThrowingChannel.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
3939
let nextTokenStatus = ManagedCriticalState<ChannelTokenStatus>(.new)
4040

4141
do {
42-
let value = try await withTaskCancellationHandler { [channel] in
43-
channel.cancelNext(nextTokenStatus, generation)
44-
} operation: {
42+
let value = try await withTaskCancellationHandler {
4543
try await channel.next(nextTokenStatus, generation)
44+
} onCancel: { [channel] in
45+
channel.cancelNext(nextTokenStatus, generation)
4646
}
4747

4848
if let value = value {
@@ -295,10 +295,10 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
295295
let generation = establish()
296296
let sendTokenStatus = ManagedCriticalState<ChannelTokenStatus>(.new)
297297

298-
await withTaskCancellationHandler { [weak self] in
299-
self?.cancelSend(sendTokenStatus, generation)
300-
} operation: {
298+
await withTaskCancellationHandler {
301299
await send(sendTokenStatus, generation, element)
300+
} onCancel: { [weak self] in
301+
self?.cancelSend(sendTokenStatus, generation)
302302
}
303303
}
304304

Sources/AsyncAlgorithms/TaskSelect.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,6 @@ extension Task {
3636
where Tasks.Element == Task<Success, Failure> {
3737
let state = ManagedCriticalState(TaskSelectState<Success, Failure>())
3838
return await withTaskCancellationHandler {
39-
let tasks = state.withCriticalRegion { state -> [Task<Success, Failure>] in
40-
defer { state.tasks = nil }
41-
return state.tasks ?? []
42-
}
43-
for task in tasks {
44-
task.cancel()
45-
}
46-
} operation: {
4739
await withUnsafeContinuation { continuation in
4840
for task in tasks {
4941
Task<Void, Never> {
@@ -61,6 +53,14 @@ extension Task {
6153
}?.cancel()
6254
}
6355
}
56+
} onCancel: {
57+
let tasks = state.withCriticalRegion { state -> [Task<Success, Failure>] in
58+
defer { state.tasks = nil }
59+
return state.tasks ?? []
60+
}
61+
for task in tasks {
62+
task.cancel()
63+
}
6464
}
6565
}
6666

Sources/AsyncSequenceValidation/Clock.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ extension AsyncSequenceValidationDiagram.Clock {
114114
) async throws {
115115
let token = queue.prepare()
116116
try await withTaskCancellationHandler {
117-
queue.cancel(token)
118-
} operation: {
119117
try await withUnsafeThrowingContinuation { continuation in
120118
queue.enqueue(AsyncSequenceValidationDiagram.Context.currentJob, deadline: deadline, continuation: continuation, token: token)
121119
}
120+
} onCancel: {
121+
queue.cancel(token)
122122
}
123123
}
124124
}

Sources/AsyncSequenceValidation/Input.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ extension AsyncSequenceValidationDiagram {
5050
eventIndex = 0
5151
}
5252
}
53-
return try await withTaskCancellationHandler { [queue] in
54-
queue.cancel(token)
55-
} operation: {
53+
return try await withTaskCancellationHandler {
5654
try await withUnsafeThrowingContinuation { continuation in
5755
queue.enqueue(Context.currentJob, deadline: when, continuation: continuation, results[eventIndex], index: index, token: token)
5856
}
57+
} onCancel: { [queue] in
58+
queue.cancel(token)
5959
}
6060
}
6161

Tests/AsyncAlgorithmsTests/Support/ManualClock.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,11 @@ public struct ManualClock: Clock {
245245
return state.generation
246246
}
247247
try await withTaskCancellationHandler {
248-
cancel(generation)
249-
} operation: {
250248
try await withUnsafeThrowingContinuation { continuation in
251249
schedule(generation, continuation: continuation, deadline: deadline)
252250
}
251+
} onCancel: {
252+
cancel(generation)
253253
}
254254
}
255255
}

Tests/AsyncAlgorithmsTests/TestTaskSelect.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,28 @@ final class TestTaskSelect: XCTestCase {
6767
let secondCancelled = expectation(description: "second cancelled")
6868
let task = Task {
6969
_ = await Task.select(Task {
70-
await withTaskCancellationHandler {
71-
firstCancelled.fulfill()
72-
} operation: { () -> Int in
70+
await withTaskCancellationHandler { () -> Int in
7371
firstReady.fulfill()
7472
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
7573
try? await Task.sleep(until: .now + .seconds(2), clock: .continuous)
7674
} else {
7775
try? await Task.sleep(nanoseconds: 2_000_000_000)
7876
}
7977
return 1
78+
} onCancel: {
79+
firstCancelled.fulfill()
8080
}
8181
}, Task {
82-
await withTaskCancellationHandler {
83-
secondCancelled.fulfill()
84-
} operation: { () -> Int in
82+
await withTaskCancellationHandler { () -> Int in
8583
secondReady.fulfill()
8684
if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
8785
try? await Task.sleep(until: .now + .seconds(2), clock: .continuous)
8886
} else {
8987
try? await Task.sleep(nanoseconds: 2_000_000_000)
9088
}
9189
return 1
90+
} onCancel: {
91+
secondCancelled.fulfill()
9292
}
9393
})
9494
}

0 commit comments

Comments
 (0)