@@ -183,19 +183,33 @@ public fun LongRange.asFlow(): Flow<Long> = flow {
183
183
}
184
184
185
185
/* *
186
- * Creates an instance of the cold [Flow] from a supplied [SendChannel].
186
+ * Creates an instance of the cold [Flow] with elements that are sent to a [SendChannel]
187
+ * that is provided to the builder's [block] of code. It allows elements to be
188
+ * produced by the code that is running in a different context,
189
+ * e.g. from a callback-based API.
190
+ *
191
+ * The resulting flow is _cold_, which means that [block] is called on each call of a terminal operator
192
+ * on the resulting flow.
187
193
*
188
194
* To control backpressure, [bufferSize] is used and matches directly the `capacity` parameter of [Channel] factory.
189
195
* The provided channel can later be used by any external service to communicate with flow and its buffer determines
190
196
* backpressure buffer size or its behaviour (e.g. in case when [Channel.CONFLATED] was used).
191
197
*
192
198
* Example of usage:
199
+ *
193
200
* ```
194
- * fun flowFrom(api: CallbackBasedApi): Flow<Int> = flowViaChannel { channel ->
195
- * val adapter = FlowSinkAdapter(channel) // implementation of callback interface
196
- * api.register(adapter)
201
+ * fun flowFrom(api: CallbackBasedApi): Flow<T> = flowViaChannel { channel ->
202
+ * val callback = object : Callback { // implementation of some callback interface
203
+ * override fun onNextValue(value: T) {
204
+ * channel.offer(value) // Note: offer drops value when buffer is full
205
+ * }
206
+ * override fun onApiError(cause: Throwable) {
207
+ * channel.cancel("API Error", CancellationException(cause))
208
+ * }
209
+ * }
210
+ * api.register(callback)
197
211
* channel.invokeOnClose {
198
- * api.unregister(adapter )
212
+ * api.unregister(callback )
199
213
* }
200
214
* }
201
215
* ```
0 commit comments