FlowCollector

interface FlowCollector<in T> (source)

FlowCollector is used as an intermediate or a terminal collector of the flow and represents an entity that accepts values emitted by the Flow.

This interface should usually not be implemented directly, but rather used as a receiver in a flow builder when implementing a custom operator. Implementations of this interface are not thread-safe.

Functions

emit

abstract suspend fun emit(value: T): Unit

Collects the value emitted by the upstream. This method is not thread-safe and should not be invoked concurrently.

Extension Properties

coroutineContext

val FlowCollector<*>.coroutineContext: CoroutineContext

isActive

val FlowCollector<*>.isActive: Boolean

Extension Functions

cancel

fun FlowCollector<*>.cancel(
    cause: CancellationException? = null
): Unit

emitAll

suspend fun <T> FlowCollector<T>.emitAll(
    channel: ReceiveChannel<T>
): Unit

Emits all elements from the given channel to this flow collector and cancels (consumes) the channel afterwards. If you need to iterate over the channel without consuming it, a regular for loop should be used instead.

suspend fun <T> FlowCollector<T>.emitAll(flow: Flow<T>): Unit

Collects all the values from the given flow and emits them to the collector. It is a shorthand for flow.collect { value -> emit(value) }.

Inheritors

MutableSharedFlow

interface MutableSharedFlow<T> : 
    SharedFlow<T>,
    FlowCollector<T>

A mutable SharedFlow that provides functions to emit values to the flow. An instance of MutableSharedFlow with the given configuration parameters can be created using MutableSharedFlow(...) constructor function.