Skip to content

Commit bcd0ee1

Browse files
[WIP] First pass at some documentation for AsyncChannel (#91)
* First pass at some documentation for AsyncChannel * Apply suggestions from code review Co-authored-by: Chris Adamson <[email protected]> * Manually line break docs Co-authored-by: Chris Adamson <[email protected]>
1 parent 37ad908 commit bcd0ee1

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Sources/AsyncAlgorithms/AsyncChannel.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12+
/// A channel for sending elements from on task to another with back pressure.
13+
///
14+
/// The `AsyncChannel` class is intended to be used as a communication type between tasks,
15+
/// particularly when one task produces values and another task consumes those values. The back
16+
/// pressure applied by `send(_:)` and `finish()` via the suspension/resume ensure that
17+
/// the production of values does not exceed the consumption of values from iteration. Each of these
18+
/// methods suspends after enqueuing the event and is resumed when the next call to `next()`
19+
/// on the `Iterator` is made.
1220
public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
21+
/// The iterator for a `AsyncChannel` instance.
1322
public struct Iterator: AsyncIteratorProtocol, Sendable {
1423
let channel: AsyncChannel<Element>
1524
var active: Bool = true
@@ -18,6 +27,7 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
1827
self.channel = channel
1928
}
2029

30+
/// Await the next sent element or finish.
2131
public mutating func next() async -> Element? {
2232
guard active else {
2333
return nil
@@ -98,6 +108,7 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
98108

99109
let state = ManagedCriticalState(State())
100110

111+
/// Create a new `AsyncChannel` given an element type.
101112
public init(element elementType: Element.Type = Element.self) { }
102113

103114
func establish() -> Int {
@@ -180,14 +191,19 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
180191
continuation?.resume(with: result)
181192
}
182193

194+
/// Send an element to an awaiting iteration. This function will resume when the next call to `next()` is made.
195+
/// If the channel is already finished then this returns immediately
183196
public func send(_ element: Element) async {
184197
await _send(.success(element))
185198
}
186199

200+
/// Send a finish to an awaiting iteration. This function will resume when the next call to `next()` is made.
201+
/// If the channel is already finished then this returns immediately
187202
public func finish() async {
188203
await _send(.success(nil))
189204
}
190205

206+
/// Create an `Iterator` for iteration of an `AsyncChannel`
191207
public func makeAsyncIterator() -> Iterator {
192208
return Iterator(self)
193209
}

Sources/AsyncAlgorithms/AsyncThrowingChannel.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12+
/// A throwing channel for sending elements from on task to another with back pressure.
13+
///
14+
/// The `AsyncThrowingChannel` class is intended to be used as a communication types between tasks., particularly when one task produces values and another task consumes those values. The back pressure applied by `send(_:)`, `fail(_:)` and `finish()` via the suspension/resume ensure that the production of values does not exceed the consumption of values from iteration. Each of these methods suspends after enqueuing the event and is resumed when the next call to `next()` on the `Iterator` is made.
1215
public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: AsyncSequence, Sendable {
1316
public struct Iterator: AsyncIteratorProtocol, Sendable {
1417
let channel: AsyncThrowingChannel<Element, Failure>

0 commit comments

Comments
 (0)