Package-level declarations
Flow — asynchronous cold and hot streams of elements.
Types
Base class for stateful implementations of Flow
. It tracks all the properties required for context preservation and throws an IllegalStateException if any of the properties are violated.
FlowCollector is used as an intermediate or a terminal collector of the flow and represents an entity that accepts values emitted by the Flow.
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.
A hot Flow 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.
A command emitted by SharingStarted implementations to control the sharing coroutine in the shareIn and stateIn operators.
A SharedFlow that represents a read-only state with a single updatable data value that emits updates to the value to its collectors. A state flow is a hot flow because its active instance exists independently of the presence of collectors. Its current value can be retrieved via the value property.
Properties
Default concurrency limit that is used by flattenMerge and flatMapMerge operators. It is 16 by default and can be changed on JVM using DEFAULT_CONCURRENCY_PROPERTY_NAME property. This is a preview API and can be changed in a backwards-incompatible manner within a single release.
Name of the property that defines the value of DEFAULT_CONCURRENCY. This is a preview API and can be changed in a backwards-incompatible manner within a single release.
Functions
Creates a cold flow that produces values from the given array. The flow being cold means that the array components are read every time a terminal operator is applied to the resulting flow.
Creates a cold flow that produces a single value from the given functional type.
Creates a cold flow that produces values from the array. The flow being cold means that the array components are read every time a terminal operator is applied to the resulting flow.
Creates a cold flow that produces values from the given iterable.
Creates a cold flow that produces values from the given iterator.
Creates a flow that produces values from the range.
Creates a cold flow that produces values from the given sequence.
Represents this mutable shared flow as a read-only shared flow.
Represents this mutable state flow as a read-only state flow.
Creates an instance of a cold Flow with elements that are sent to a SendChannel provided to the builder's block of code via ProducerScope. It allows elements to be produced by code that is running in a different context or concurrently.
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.
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.
Creates an instance of a cold Flow with elements that are sent to a SendChannel provided to the builder's block of code via ProducerScope. It allows elements to be produced by code that is running in a different context or concurrently. The resulting flow is cold, which means that block is called every time a terminal operator is applied to the resulting flow.
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.
Represents the given receive channel as a hot flow and consumes the channel on the first collection from this flow. The resulting flow can be collected just once and throws IllegalStateException when trying to collect it more than once.
Returns flow where all subsequent repetitions of the same value are filtered out.
Returns flow where all subsequent repetitions of the same value are filtered out, when compared with each other via the provided areEquivalent function.
Returns flow where all subsequent repetitions of the same key are filtered out, where key is extracted with keySelector function.
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.
Collects all the values from the given flow and emits them to the collector. It is a shorthand for flow.collect { value -> emit(value) }
.
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.
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.
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.
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.
Flattens the given flow of flows into a single flow in a sequential manner, without interleaving nested flows.
Flattens the given flow of flows into a single flow with a concurrency limit on the number of concurrently collected flows.
Updates the MutableStateFlow.value atomically using the specified function of its value, and returns its prior value.
The terminal operator that returns the last element emitted by the flow or null
if the flow was empty.
Terminal flow operator that launches the collection of the given flow in the scope. It is a shorthand for scope.launch { flow.collect() }
.
Creates a MutableSharedFlow with the given configuration parameters.
Creates a MutableStateFlow with the given initial value.
Returns a flow that invokes the given action after this shared flow starts to be collected (after the subscription is registered).
Creates a produce coroutine that collects the given flow.
Represents the given receive channel as a hot flow and receives from the channel in fan-out fashion every time this flow is collected. One element will be emitted to one collector only.
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.
Converts a cold Flow into a hot SharedFlow 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.
The terminal operator that awaits for one and only one value to be emitted. Throws NoSuchElementException for empty flow and IllegalArgumentException for flow that contains more than one element.
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.
Starts the upstream flow in a given scope, suspends until the first value is emitted, and returns a hot StateFlow 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.
Returns a flow that will emit a TimeoutCancellationException if the upstream doesn't emit an item within the given time.
Collects given flow into a destination
Collects given flow into a destination
A specialized version of Flow.toList that returns Nothing to indicate that SharedFlow collection never completes.
Collects given flow into a destination
A specialized version of Flow.toSet that returns Nothing to indicate that SharedFlow collection never completes.
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
.
Updates the MutableStateFlow.value atomically using the specified function of its value.
Updates the MutableStateFlow.value atomically using the specified function of its value, and returns the new value.
Sharing is started when the first subscriber appears, immediately stops when the last subscriber disappears (by default), keeping the replay cache forever (by default).
Returns a flow that wraps each element into IndexedValue, containing value and its index (starting from zero).