Last active
June 5, 2024 15:46
-
-
Save robertmryan/2cbfeaa369502411d6dbccfc12565736 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
actor SerialTasksIgnoresAnyError<Success: Sendable> { | |
private var previousTask: Task<Success, Error>? | |
private let priority: TaskPriority? | |
init(priority: TaskPriority? = nil) { | |
self.priority = priority | |
} | |
func add(block: @Sendable @escaping () async throws -> Success) async throws -> Success { | |
let task = Task(priority: priority) { [previousTask] in | |
let _ = try? await previousTask?.value | |
return try await block() | |
} | |
previousTask = task | |
return try await withTaskCancellationHandler { | |
try await task.value | |
} onCancel: { | |
task.cancel() | |
} | |
} | |
} | |
actor SerialTasksStopsOnAnyErrorIncludingCancelation<Success: Sendable> { | |
private var previousTask: Task<Success, Error>? | |
private let priority: TaskPriority? | |
init(priority: TaskPriority? = nil) { | |
self.priority = priority | |
} | |
func add(block: @Sendable @escaping () async throws -> Success) async throws -> Success { | |
let task = Task(priority: priority) { [previousTask] in | |
let _ = try await previousTask?.value | |
return try await block() | |
} | |
previousTask = task | |
return try await withTaskCancellationHandler { | |
try await task.value | |
} onCancel: { | |
task.cancel() | |
} | |
} | |
} | |
actor SerialTasksStopsOnlyOnCancelation<Success: Sendable> { | |
private var previousTask: Task<Success, Error>? | |
private let priority: TaskPriority? | |
init(priority: TaskPriority? = nil) { | |
self.priority = priority | |
} | |
func add(block: @Sendable @escaping () async throws -> Success) async throws -> Success { | |
let task = Task(priority: priority) { [previousTask] in | |
do { | |
let _ = try await previousTask?.value // assuming this properly throws `CancellationError` | |
} catch is CancellationError { | |
throw CancellationError() | |
} catch { | |
// ignore other errors | |
} | |
return try await block() | |
} | |
previousTask = task | |
return try await withTaskCancellationHandler { | |
try await task.value | |
} onCancel: { | |
task.cancel() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Disclaimer: I have not done exhaustive testing of the above, so caveat emptor. I just wanted to outline a few alternatives and the concerns.
Personally, I now tend to use
AsyncChannel
when I want to process a series of tasks sequentially. This manual “await prior task” approach was something I used before discovering Swift Async Algorithms library.