xref: /aosp_15_r20/external/kotlinx.coroutines/kotlinx-coroutines-core/common/src/flow/FlowCollector.kt (revision 7a7160fed73afa6648ef8aa100d4a336fe921d9a)
1 package kotlinx.coroutines.flow
2 
3 /**
4  * [FlowCollector] is used as an intermediate or a terminal collector of the flow and represents
5  * an entity that accepts values emitted by the [Flow].
6  *
7  * This interface should usually not be implemented directly, but rather used as a receiver in a [flow] builder when implementing a custom operator,
8  * or with SAM-conversion.
9  * Implementations of this interface are not thread-safe.
10  *
11  * Example of usage:
12  *
13  * ```
14  * val flow = getMyEvents()
15  * try {
16  *     flow.collect { value ->
17  *         println("Received $value")
18  *     }
19  *     println("My events are consumed successfully")
20  * } catch (e: Throwable) {
21  *     println("Exception from the flow: $e")
22  * }
23  * ```
24  */
25 public fun interface FlowCollector<in T> {
26 
27     /**
28      * Collects the value emitted by the upstream.
29      * This method is not thread-safe and should not be invoked concurrently.
30      */
emitnull31     public suspend fun emit(value: T)
32 }
33