• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 package kotlinx.coroutines.flow
6 
7 /**
8  * [FlowCollector] is used as an intermediate or a terminal collector of the flow and represents
9  * an entity that accepts values emitted by the [Flow].
10  *
11  * This interface should usually not be implemented directly, but rather used as a receiver in a [flow] builder when implementing a custom operator,
12  * or with SAM-conversion.
13  * Implementations of this interface are not thread-safe.
14  *
15  * Example of usage:
16  *
17  * ```
18  * val flow = getMyEvents()
19  * try {
20  *     flow.collect { value ->
21  *         println("Received $value")
22  *     }
23  *     println("My events are consumed successfully")
24  * } catch (e: Throwable) {
25  *     println("Exception from the flow: $e")
26  * }
27  * ```
28  */
29 public fun interface FlowCollector<in T> {
30 
31     /**
32      * Collects the value emitted by the upstream.
33      * This method is not thread-safe and should not be invoked concurrently.
34      */
emitnull35     public suspend fun emit(value: T)
36 }
37