Skip to content

Commit 72d8197

Browse files
committed
Update task cancellation API based on review feedback
Swap the order of the arguments so we now have: await withTaskCancellationHandler { // do stuff } onCancel: { // .. }
1 parent 8750f20 commit 72d8197

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

stdlib/public/Concurrency/SourceCompatibilityShims.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ extension TaskPriority {
4646
}
4747
}
4848

49+
@available(SwiftStdlib 5.5, *)
50+
@_alwaysEmitIntoClient
51+
public func withTaskCancellationHandler<T>(
52+
handler: @Sendable () -> (),
53+
operation: () async throws -> T
54+
) async rethrows -> T {
55+
try await withTaskCancellationHandler(operation: operation, onCancel: handler)
56+
}
57+
4958
@available(SwiftStdlib 5.5, *)
5059
extension Task where Success == Never, Failure == Never {
5160
@available(*, deprecated, message: "`Task.withCancellationHandler` has been replaced by `withTaskCancellationHandler` and will be removed shortly.")

stdlib/public/Concurrency/TaskCancellation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import Swift
2929
/// This function returns instantly and will never suspend.
3030
@available(SwiftStdlib 5.5, *)
3131
public func withTaskCancellationHandler<T>(
32-
handler: @Sendable () -> (),
33-
operation: () async throws -> T
32+
operation: () async throws -> T,
33+
onCancel handler: @Sendable () -> Void
3434
) async rethrows -> T {
3535
let task = Builtin.getCurrentAsyncTask()
3636

test/Concurrency/async_cancellation.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ func test_cancellation_withTaskCancellationHandler(_ anything: Any) async -> Pic
3030
let handle: Task<PictureData, Error> = .init {
3131
let file = SomeFile()
3232

33-
return await withTaskCancellationHandler(
34-
handler: { file.close() }) {
33+
return await withTaskCancellationHandler {
3534
await test_cancellation_guard_isCancelled(file)
35+
} onCancel: {
36+
file.close()
3637
}
3738
}
3839

0 commit comments

Comments
 (0)