SharedFlow

interface SharedFlow<out T> : Flow<T> (source)

A hotFlow that shares emitted values among all its collectors in a broadcast fashion, so that all collectors get all emitted values. A shared flow is called hot because its active instance exists independently of the presence of collectors. This is opposed to a regular Flow, such as defined by the flow { ... } function, which is cold and is started separately for each collector.

Shared flow never completes. A call to Flow.collect on a shared flow never completes normally, and neither does a coroutine started by the Flow.launchIn function. An active collector of a shared flow is called a subscriber.

A subscriber of a shared flow can be cancelled. This usually happens when the scope in which the coroutine is running is cancelled. A subscriber to a shared flow is always cancellable, and checks for cancellation before each emission. Note that most terminal operators like Flow.toList would also not complete, when applied to a shared flow, but flow-truncating operators like Flow.take and Flow.takeWhile can be used on a shared flow to turn it into a completing one.

A mutable shared flow is created using the MutableSharedFlow(...) constructor function. Its state can be updated by emitting values to it and performing other operations. See the MutableSharedFlow documentation for details.

SharedFlow is useful for broadcasting events that happen inside an application to subscribers that can come and go. For example, the following class encapsulates an event bus that distributes events to all subscribers in a rendezvous manner, suspending until all subscribers receive emitted event:

class EventBus {
private val _events = MutableSharedFlow<Event>() // private mutable shared flow
val events = _events.asSharedFlow() // publicly exposed as read-only shared flow

suspend fun produceEvent(event: Event) {
_events.emit(event) // suspends until all subscribers receive it
}
}

As an alternative to the above usage with the MutableSharedFlow(...) constructor function, any coldFlow can be converted to a shared flow using the shareIn operator.

There is a specialized implementation of shared flow for the case where the most recent state value needs to be shared. See StateFlow for details.

Replay cache and buffer

A shared flow keeps a specific number of the most recent values in its replay cache. Every new subscriber first gets the values from the replay cache and then gets new emitted values. The maximum size of the replay cache is specified when the shared flow is created by the replay parameter. A snapshot of the current replay cache is available via the replayCache property and it can be reset with the MutableSharedFlow.resetReplayCache function.

A replay cache also provides buffer for emissions to the shared flow, allowing slow subscribers to get values from the buffer without suspending emitters. The buffer space determines how much slow subscribers can lag from the fast ones. When creating a shared flow, additional buffer capacity beyond replay can be reserved using the extraBufferCapacity parameter.

A shared flow with a buffer can be configured to avoid suspension of emitters on buffer overflow using the onBufferOverflow parameter, which is equal to one of the entries of the BufferOverflow enum. When a strategy other than SUSPENDED is configured, emissions to the shared flow never suspend.

Buffer overflow condition can happen only when there is at least one subscriber that is not ready to accept the new value. In the absence of subscribers only the most recent replay values are stored and the buffer overflow behavior is never triggered and has no effect. In particular, in the absence of subscribers emitter never suspends despite BufferOverflow.SUSPEND option and BufferOverflow.DROP_LATEST option does not have effect either. Essentially, the behavior in the absence of subscribers is always similar to BufferOverflow.DROP_OLDEST, but the buffer is just of replay size (without any extraBufferCapacity).

Unbuffered shared flow

A default implementation of a shared flow that is created with MutableSharedFlow() constructor function without parameters has no replay cache nor additional buffer. emit call to such a shared flow suspends until all subscribers receive the emitted value and returns immediately if there are no subscribers. Thus, tryEmit call succeeds and returns true only if there are no subscribers (in which case the emitted value is immediately lost).

SharedFlow vs BroadcastChannel

Conceptually shared flow is similar to BroadcastChannel and is designed to completely replace it. It has the following important differences:

  • SharedFlow is simpler, because it does not have to implement all the Channel APIs, which allows for faster and simpler implementation.

  • SharedFlow supports configurable replay and buffer overflow strategy.

  • SharedFlow has a clear separation into a read-only SharedFlow interface and a MutableSharedFlow.

  • SharedFlow cannot be closed like BroadcastChannel and can never represent a failure. All errors and completion signals should be explicitly materialized if needed.

To migrate BroadcastChannel usage to SharedFlow, start by replacing usages of the BroadcastChannel(capacity) constructor with MutableSharedFlow(0, extraBufferCapacity=capacity) (broadcast channel does not replay values to new subscribers). Replace send and trySend calls with emit and tryEmit, and convert subscribers' code to flow operators.

Concurrency

All methods of shared flow are thread-safe and can be safely invoked from concurrent coroutines without external synchronization.

Operator fusion

Application of flowOn, buffer with RENDEZVOUS capacity, or cancellable operators to a shared flow has no effect.

Implementation notes

Shared flow implementation uses a lock to ensure thread-safety, but suspending collector and emitter coroutines are resumed outside of this lock to avoid deadlocks when using unconfined coroutines. Adding new subscribers has O(1) amortized cost, but emitting has O(N) cost, where N is the number of subscribers.

Not stable for inheritance

The SharedFlow interface is not stable for inheritance in 3rd party libraries, as new methods might be added to this interface in the future, but is stable for use. Use the MutableSharedFlow(replay, ...) constructor function to create an implementation.

Functions

Link copied to clipboard
abstract suspend override fun collect(collector: FlowCollector<T>): Nothing

Accepts the given collector and emits values into it. To emit values from a shared flow into a specific collector, either collector.emitAll(flow) or collect { ... } SAM-conversion can be used.

Properties

Link copied to clipboard
abstract val replayCache: List<T>

A snapshot of the replay cache.

Inheritors

Link copied to clipboard
Link copied to clipboard

Extensions

Link copied to clipboard
fun <T> Flow<T>.buffer(capacity: Int = BUFFERED, onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND): Flow<T>

Buffers flow emissions via channel of a specified capacity and runs collector in a separate coroutine.

Link copied to clipboard
fun <T> Flow<T>.cancellable(): Flow<T>

Returns a flow which checks cancellation status on each emission and throws the corresponding cancellation cause if flow collector was cancelled. Note that flow builder and all implementations of SharedFlow are cancellable by default.

Link copied to clipboard
fun <T> Flow<T>.catch(action: suspend FlowCollector<T>.(cause: Throwable) -> Unit): Flow<T>

Catches exceptions in the flow completion and calls a specified action with the caught exception. This operator is transparent to exceptions that occur in downstream flow and does not catch exceptions that are thrown to cancel the flow.

Link copied to clipboard
suspend fun Flow<*>.collect()

Terminal flow operator that collects the given flow but ignores all emitted values. If any exception occurs during collect or in the provided flow, this exception is rethrown from this method.

Link copied to clipboard
inline suspend fun <T> Flow<T>.collectIndexed(crossinline action: suspend (index: Int, value: T) -> Unit)

Terminal flow operator that collects the given flow with a provided action that takes the index of an element (zero-based) and the element. If any exception occurs during collect or in the provided flow, this exception is rethrown from this method.

Link copied to clipboard
suspend fun <T> Flow<T>.collectLatest(action: suspend (value: T) -> Unit)

Terminal flow operator that collects the given flow with a provided action. The crucial difference from collect is that when the original flow emits a new value then the action block for the previous value is cancelled.

Link copied to clipboard
@JvmName(name = "flowCombine")
fun <T1, T2, R> Flow<T1>.combine(flow: Flow<T2>, transform: suspend (a: T1, b: T2) -> R): Flow<R>

Returns a Flow whose values are generated with transform function by combining the most recently emitted values by each flow.

Link copied to clipboard
@JvmName(name = "flowCombineTransform")
fun <T1, T2, R> Flow<T1>.combineTransform(flow: Flow<T2>, transform: suspend FlowCollector<R>.(a: T1, b: T2) -> Unit): Flow<R>

Returns a Flow whose values are generated by transform function that process the most recently emitted values by each flow.

Link copied to clipboard
fun <T> Flow<T>.conflate(): Flow<T>

Conflates flow emissions via conflated channel and runs collector in a separate coroutine. The effect of this is that emitter is never suspended due to a slow collector, but collector always gets the most recent value emitted.

Link copied to clipboard
suspend fun <T> Flow<T>.count(): Int

Returns the number of elements in this flow.

suspend fun <T> Flow<T>.count(predicate: suspend (T) -> Boolean): Int

Returns the number of elements matching the given predicate.

Link copied to clipboard
fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T>
fun <T> Flow<T>.debounce(timeoutMillis: (T) -> Long): Flow<T>
fun <T> Flow<T>.debounce(timeout: Duration): Flow<T>
@JvmName(name = "debounceDuration")
fun <T> Flow<T>.debounce(timeout: (T) -> Duration): Flow<T>

Returns a flow that mirrors the original flow, but filters out values that are followed by the newer values within the given timeout. The latest value is always emitted.

Link copied to clipboard

Returns flow where all subsequent repetitions of the same value are filtered out.

fun <T> Flow<T>.distinctUntilChanged(areEquivalent: (old: T, new: T) -> Boolean): Flow<T>

Returns flow where all subsequent repetitions of the same value are filtered out, when compared with each other via the provided areEquivalent function.

Link copied to clipboard
fun <T, K> Flow<T>.distinctUntilChangedBy(keySelector: (T) -> K): Flow<T>

Returns flow where all subsequent repetitions of the same key are filtered out, where key is extracted with keySelector function.

Link copied to clipboard
fun <T> Flow<T>.drop(count: Int): Flow<T>

Returns a flow that ignores first count elements. Throws IllegalArgumentException if count is negative.

Link copied to clipboard
fun <T> Flow<T>.dropWhile(predicate: suspend (T) -> Boolean): Flow<T>

Returns a flow containing all elements except first elements that satisfy the given predicate.

Link copied to clipboard
inline fun <T> Flow<T>.filter(crossinline predicate: suspend (T) -> Boolean): Flow<T>

Returns a flow containing only values of the original flow that match the given predicate.

Link copied to clipboard
inline fun <R> Flow<*>.filterIsInstance(): Flow<R>

Returns a flow containing only values that are instances of specified type R.

Link copied to clipboard
inline fun <T> Flow<T>.filterNot(crossinline predicate: suspend (T) -> Boolean): Flow<T>

Returns a flow containing only values of the original flow that do not match the given predicate.

Link copied to clipboard
fun <T : Any> Flow<T?>.filterNotNull(): Flow<T>

Returns a flow containing only values of the original flow that are not null.

Link copied to clipboard
suspend fun <T> Flow<T>.first(): T

The terminal operator that returns the first element emitted by the flow and then cancels flow's collection. Throws NoSuchElementException if the flow was empty.

suspend fun <T> Flow<T>.first(predicate: suspend (T) -> Boolean): T

The terminal operator that returns the first element emitted by the flow matching the given predicate and then cancels flow's collection. Throws NoSuchElementException if the flow has not contained elements matching the predicate.

Link copied to clipboard
suspend fun <T> Flow<T>.firstOrNull(): T?

The terminal operator that returns the first element emitted by the flow and then cancels flow's collection. Returns null if the flow was empty.

suspend fun <T> Flow<T>.firstOrNull(predicate: suspend (T) -> Boolean): T?

The terminal operator that returns the first element emitted by the flow matching the given predicate and then cancels flow's collection. Returns null if the flow did not contain an element matching the predicate.

Link copied to clipboard
fun <T, R> Flow<T>.flatMapConcat(transform: suspend (value: T) -> Flow<R>): Flow<R>

Transforms elements emitted by the original flow by applying transform, that returns another flow, and then concatenating and flattening these flows.

Link copied to clipboard
inline fun <T, R> Flow<T>.flatMapLatest(crossinline transform: suspend (value: T) -> Flow<R>): Flow<R>

Returns a flow that switches to a new flow produced by transform function every time the original flow emits a value. When the original flow emits a new value, the previous flow produced by transform block is cancelled.

Link copied to clipboard
fun <T, R> Flow<T>.flatMapMerge(concurrency: Int = DEFAULT_CONCURRENCY, transform: suspend (value: T) -> Flow<R>): Flow<R>

Transforms elements emitted by the original flow by applying transform, that returns another flow, and then merging and flattening these flows.

Link copied to clipboard

Flattens the given flow of flows into a single flow in a sequential manner, without interleaving nested flows.

Link copied to clipboard
fun <T> Flow<Flow<T>>.flattenMerge(concurrency: Int = DEFAULT_CONCURRENCY): Flow<T>

Flattens the given flow of flows into a single flow with a concurrency limit on the number of concurrently collected flows.

Link copied to clipboard
fun <T> Flow<T>.flowOn(context: CoroutineContext): Flow<T>

Changes the context where this flow is executed to the given context. This operator is composable and affects only preceding operators that do not have its own context. This operator is context preserving: contextdoes not leak into the downstream flow.

Link copied to clipboard
inline suspend fun <T, R> Flow<T>.fold(initial: R, crossinline operation: suspend (acc: R, value: T) -> R): R

Accumulates value starting with initial value and applying operation current accumulator value and each element

Link copied to clipboard
suspend fun <T> Flow<T>.last(): T

The terminal operator that returns the last element emitted by the flow.

Link copied to clipboard
suspend fun <T> Flow<T>.lastOrNull(): T?

The terminal operator that returns the last element emitted by the flow or null if the flow was empty.

Link copied to clipboard
fun <T> Flow<T>.launchIn(scope: CoroutineScope): Job

Terminal flow operator that launches the collection of the given flow in the scope. It is a shorthand for scope.launch { flow.collect() }.

Link copied to clipboard
inline fun <T, R> Flow<T>.map(crossinline transform: suspend (value: T) -> R): Flow<R>

Returns a flow containing the results of applying the given transform function to each value of the original flow.

Link copied to clipboard
fun <T, R> Flow<T>.mapLatest(transform: suspend (value: T) -> R): Flow<R>

Returns a flow that emits elements from the original flow transformed by transform function. When the original flow emits a new value, computation of the transform block for previous value is cancelled.

Link copied to clipboard
inline fun <T, R : Any> Flow<T>.mapNotNull(crossinline transform: suspend (value: T) -> R?): Flow<R>

Returns a flow that contains only non-null results of applying the given transform function to each value of the original flow.

Link copied to clipboard
fun <T> Flow<T>.onCompletion(action: suspend FlowCollector<T>.(cause: Throwable?) -> Unit): Flow<T>

Returns a flow that invokes the given actionafter the flow is completed or cancelled, passing the cancellation exception or failure as cause parameter of action.

Link copied to clipboard
fun <T> Flow<T>.onEach(action: suspend (T) -> Unit): Flow<T>

Returns a flow that invokes the given actionbefore each value of the upstream flow is emitted downstream.

Link copied to clipboard
fun <T> Flow<T>.onEmpty(action: suspend FlowCollector<T>.() -> Unit): Flow<T>

Invokes the given action when this flow completes without emitting any elements. The receiver of the action is FlowCollector, so onEmpty can emit additional elements. For example:

Link copied to clipboard
fun <T> Flow<T>.onStart(action: suspend FlowCollector<T>.() -> Unit): Flow<T>

Returns a flow that invokes the given actionbefore this flow starts to be collected.

Link copied to clipboard
fun <T> SharedFlow<T>.onSubscription(action: suspend FlowCollector<T>.() -> Unit): SharedFlow<T>

Returns a flow that invokes the given actionafter this shared flow starts to be collected (after the subscription is registered).

Link copied to clipboard

Creates a produce coroutine that collects the given flow.

Link copied to clipboard
suspend fun <S, T : S> Flow<T>.reduce(operation: suspend (accumulator: S, value: T) -> S): S

Accumulates value starting with the first element and applying operation to current accumulator value and each element. Throws NoSuchElementException if flow was empty.

Link copied to clipboard
fun <T> Flow<T>.retry(retries: Long = Long.MAX_VALUE, predicate: suspend (cause: Throwable) -> Boolean = { true }): Flow<T>

Retries collection of the given flow up to retries times when an exception that matches the given predicate occurs in the upstream flow. This operator is transparent to exceptions that occur in downstream flow and does not retry on exceptions that are thrown to cancel the flow.

Link copied to clipboard
fun <T> Flow<T>.retryWhen(predicate: suspend FlowCollector<T>.(cause: Throwable, attempt: Long) -> Boolean): Flow<T>

Retries collection of the given flow when an exception occurs in the upstream flow and the predicate returns true. The predicate also receives an attempt number as parameter, starting from zero on the initial call. This operator is transparent to exceptions that occur in downstream flow and does not retry on exceptions that are thrown to cancel the flow.

Link copied to clipboard
fun <T, R> Flow<T>.runningFold(initial: R, operation: suspend (accumulator: R, value: T) -> R): Flow<R>

Folds the given flow with operation, emitting every intermediate result, including initial value. Note that initial value should be immutable (or should not be mutated) as it is shared between different collectors. For example:

Link copied to clipboard
fun <T> Flow<T>.runningReduce(operation: suspend (accumulator: T, value: T) -> T): Flow<T>

Reduces the given flow with operation, emitting every intermediate result, including initial value. The first element is taken as initial value for operation accumulator. This operator has a sibling with initial value -- scan.

Link copied to clipboard
fun <T> Flow<T>.sample(periodMillis: Long): Flow<T>
fun <T> Flow<T>.sample(period: Duration): Flow<T>

Returns a flow that emits only the latest value emitted by the original flow during the given sampling period.

Link copied to clipboard
fun <T, R> Flow<T>.scan(initial: R, operation: suspend (accumulator: R, value: T) -> R): Flow<R>

Folds the given flow with operation, emitting every intermediate result, including initial value. Note that initial value should be immutable (or should not be mutated) as it is shared between different collectors. For example:

Link copied to clipboard
fun <T> Flow<T>.shareIn(    scope: CoroutineScope,     started: SharingStarted,     replay: Int = 0): SharedFlow<T>

Converts a coldFlow into a hotSharedFlow that is started in the given coroutine scope, sharing emissions from a single running instance of the upstream flow with multiple downstream subscribers, and replaying a specified number of replay values to new subscribers. See the SharedFlow documentation for the general concepts of shared flows.

Link copied to clipboard
suspend fun <T> Flow<T>.single(): T

The terminal operator that awaits for one and only one value to be emitted. Throws NoSuchElementException for empty flow and IllegalStateException for flow that contains more than one element.

Link copied to clipboard
suspend fun <T> Flow<T>.singleOrNull(): T?

The terminal operator that awaits for one and only one value to be emitted. Returns the single value or null, if the flow was empty or emitted more than one value.

Link copied to clipboard
fun <T> Flow<T>.stateIn(    scope: CoroutineScope,     started: SharingStarted,     initialValue: T): StateFlow<T>

Converts a coldFlow into a hotStateFlow that is started in the given coroutine scope, sharing the most recently emitted value from a single running instance of the upstream flow with multiple downstream subscribers. See the StateFlow documentation for the general concepts of state flows.

suspend fun <T> Flow<T>.stateIn(scope: CoroutineScope): StateFlow<T>

Starts the upstream flow in a given scope, suspends until the first value is emitted, and returns a hotStateFlow of future emissions, sharing the most recently emitted value from this running instance of the upstream flow with multiple downstream subscribers. See the StateFlow documentation for the general concepts of state flows.

Link copied to clipboard
fun <T> Flow<T>.take(count: Int): Flow<T>

Returns a flow that contains first count elements. When count elements are consumed, the original flow is cancelled. Throws IllegalArgumentException if count is not positive.

Link copied to clipboard
fun <T> Flow<T>.takeWhile(predicate: suspend (T) -> Boolean): Flow<T>

Returns a flow that contains first elements satisfying the given predicate.

Link copied to clipboard
suspend fun <T, C : MutableCollection<in T>> Flow<T>.toCollection(destination: C): C

Collects given flow into a destination

Link copied to clipboard
suspend fun <T> Flow<T>.toList(destination: MutableList<T> = ArrayList()): List<T>

Collects given flow into a destination

Link copied to clipboard
suspend fun <T> Flow<T>.toSet(destination: MutableSet<T> = LinkedHashSet()): Set<T>

Collects given flow into a destination

Link copied to clipboard
inline fun <T, R> Flow<T>.transform(crossinline transform: suspend FlowCollector<R>.(value: T) -> Unit): Flow<R>

Applies transform function to each value of the given flow.

Link copied to clipboard
fun <T, R> Flow<T>.transformLatest(transform: suspend FlowCollector<R>.(value: T) -> Unit): Flow<R>

Returns a flow that produces element by transform function every time the original flow emits a value. When the original flow emits a new value, the previous transform block is cancelled, thus the name transformLatest.

Link copied to clipboard
fun <T, R> Flow<T>.transformWhile(transform: suspend FlowCollector<R>.(value: T) -> Boolean): Flow<R>

Applies transform function to each value of the given flow while this function returns true.

Link copied to clipboard

Returns a flow that wraps each element into IndexedValue, containing value and its index (starting from zero).

Link copied to clipboard
fun <T1, T2, R> Flow<T1>.zip(other: Flow<T2>, transform: suspend (T1, T2) -> R): Flow<R>

Zips values from the current flow (this) with other flow using provided transform function applied to each pair of values. The resulting flow completes as soon as one of the flows completes and cancel is called on the remaining flow.