@@ -123,19 +123,33 @@ actor AsyncBufferState<Input: Sendable, Output: Sendable> {
123
123
}
124
124
}
125
125
126
+ /// An asynchronous buffer storage actor protocol used for buffering
127
+ /// elements to an `AsyncBufferSequence`.
126
128
@rethrows
127
129
public protocol AsyncBuffer : Actor {
128
130
associatedtype Input : Sendable
129
131
associatedtype Output : Sendable
130
132
133
+ /// Push an element to enqueue to the buffer
131
134
func push( _ element: Input ) async
135
+
136
+ /// Pop an element from the buffer.
137
+ ///
138
+ /// Implementors of `pop()` may throw. In cases where types
139
+ /// throw from this function, that throwing behavior contributes to
140
+ /// the rethrowing characteristics of `AsyncBufferSequence`.
132
141
func pop( ) async throws -> Output ?
133
142
}
134
143
144
+ /// A buffer that limits pushed items by a certain count.
135
145
public actor AsyncLimitBuffer < Element: Sendable > : AsyncBuffer {
146
+ /// A policy for buffering elements to an `AsyncLimitBuffer`
136
147
public enum Policy : Sendable {
148
+ /// A policy for no bounding limit of pushed elements.
137
149
case unbounded
150
+ /// A policy for limiting to a specific number of oldest values.
138
151
case bufferingOldest( Int )
152
+ /// A policy for limiting to a specific number of newest values.
139
153
case bufferingNewest( Int )
140
154
}
141
155
@@ -154,6 +168,7 @@ public actor AsyncLimitBuffer<Element: Sendable>: AsyncBuffer {
154
168
self . policy = policy
155
169
}
156
170
171
+ /// Push an element to enqueue to the buffer.
157
172
public func push( _ element: Element ) async {
158
173
switch policy {
159
174
case . unbounded:
@@ -174,6 +189,7 @@ public actor AsyncLimitBuffer<Element: Sendable>: AsyncBuffer {
174
189
}
175
190
}
176
191
192
+ /// Pop an element from the buffer.
177
193
public func pop( ) async -> Element ? {
178
194
guard buffer. count > 0 else {
179
195
return nil
@@ -183,17 +199,31 @@ public actor AsyncLimitBuffer<Element: Sendable>: AsyncBuffer {
183
199
}
184
200
185
201
extension AsyncSequence where Element: Sendable {
202
+ /// Creates an asynchronous sequence that buffers elements using a buffer created from a supplied closure.
203
+ ///
204
+ /// Use the `buffer(_:)` method to account for `AsyncSequence` types that may produce elements faster
205
+ /// than they are iterated. The `createBuffer` closure returns a backing buffer for storing elements and dealing with
206
+ /// behavioral charcteristics of the `buffer(_:)` algorithm.
207
+ ///
208
+ /// - Parameter createBuffer: A closure that constructs a new `AsyncBuffer` actor to store buffered values.
209
+ /// - Returns: An asynchronous sequence that buffers elements using the specified `AsyncBuffer`.
186
210
public func buffer< Buffer: AsyncBuffer > ( _ createBuffer: @Sendable @escaping ( ) -> Buffer ) -> AsyncBufferSequence < Self , Buffer > where Buffer. Input == Element {
187
211
AsyncBufferSequence ( self , createBuffer: createBuffer)
188
212
}
189
213
214
+ /// Creates an asynchronous sequence that buffers elements using a specific policy to limit the number of
215
+ /// elements that are buffered.
216
+ ///
217
+ /// - Parameter policy: A limiting policy behavior on the buffering behavior of the `AsyncBufferSequence`
218
+ /// - Returns: An asynchronous sequence that buffers elements up to a given limit.
190
219
public func buffer( policy limit: AsyncLimitBuffer < Element > . Policy ) -> AsyncBufferSequence < Self , AsyncLimitBuffer < Element > > {
191
220
buffer {
192
221
AsyncLimitBuffer ( policy: limit)
193
222
}
194
223
}
195
224
}
196
225
226
+ /// An `AsyncSequence` that buffers elements utilizing an `AsyncBuffer`.
197
227
public struct AsyncBufferSequence < Base: AsyncSequence , Buffer: AsyncBuffer > where Base. Element == Buffer . Input , Base. AsyncIterator: Sendable {
198
228
let base : Base
199
229
let createBuffer : @Sendable ( ) -> Buffer
@@ -210,6 +240,7 @@ extension AsyncBufferSequence.Iterator: Sendable where Base: Sendable, Base.Asyn
210
240
extension AsyncBufferSequence : AsyncSequence {
211
241
public typealias Element = Buffer . Output
212
242
243
+ /// The iterator for a `AsyncBufferSequence` instance.
213
244
public struct Iterator : AsyncIteratorProtocol {
214
245
struct Active {
215
246
var task : Task < Void , Never > ?
0 commit comments