mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-30 04:58:44 +00:00
[Channel] improve send cancellation (#184)
* asyncChannel: introduce ChannelToken to model Pending and Awaiting * asyncChannel: harmonize send and next cancellation * asyncChannel: add .finished as an emission state * asyncThrowingChannel: harmonize send and next cancellation * channel: update documentation for cancellation
This commit is contained in:
1 parent
68c8dc285b
commit
2b5fbf3c91
6 files changed
+321
-226
No files matched your search
@@ -51,7 +51,7 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
|
||||
}
|
||||
```
|
||||
|
||||
Channels are intended to be used as communication types between tasks. Particularly when one task produces values and another task consumes said values. On the one hand, the back pressure applied by `send(_:)` via the suspension/resume ensures that the production of values does not exceed the consumption of values from iteration. This method suspends after enqueuing the event and is resumed when the next call to `next()` on the `Iterator` is made. On the other hand, the call to `finish()` or `fail(_:)` immediately resumes all the pending operations for every producers and consumers. Thus, every suspended `send(_:)` operations instantly resume, so as every suspended `next()` operations by producing a nil value, or by throwing an error, indicating the termination of the iterations. Further calls to `send(_:)` will immediately resume.
|
||||
Channels are intended to be used as communication types between tasks. Particularly when one task produces values and another task consumes said values. On the one hand, the back pressure applied by `send(_:)` via the suspension/resume ensures that the production of values does not exceed the consumption of values from iteration. This method suspends after enqueuing the event and is resumed when the next call to `next()` on the `Iterator` is made. On the other hand, the call to `finish()` or `fail(_:)` immediately resumes all the pending operations for every producers and consumers. Thus, every suspended `send(_:)` operations instantly resume, so as every suspended `next()` operations by producing a nil value, or by throwing an error, indicating the termination of the iterations. Further calls to `send(_:)` will immediately resume. The calls to `send(:)` and `next()` will immediately resume when their supporting task is cancelled, other operations from other tasks will remain active.
|
||||
|
||||
```swift
|
||||
let channel = AsyncChannel<String>()
|
||||
|
||||
Reference in new issue
Block a user