kotlin-flow-extensions / hu.akarnokd.kotlin.flow / BehaviorSubject

BehaviorSubject

@FlowPreview class BehaviorSubject<T> : AbstractFlow<T>, SubjectAPI<T>

Caches one item and replays it to fresh collectors.

Constructors

<init>

BehaviorSubject()

Constructs an empty BehaviorSubject.

BehaviorSubject(initialValue: T)

Constructs a BehaviorSubject with an initial value to emit to collectors.

Functions

collectSafely

suspend fun collectSafely(collector: FlowCollector<T>): Unit

Accepts a collector and emits the latest (if available) value and any subsequent value received by this BehaviorSubject until the BehaviorSubject gets terminated.

collectorCount

fun collectorCount(): Int

Returns the number of collectors waiting for data.

complete

suspend fun complete(): Unit

Signal current and future collectors that no further values will be coming.

emit

suspend fun emit(value: T): Unit

Emit a value to all current collectors when they are ready.

emitError

suspend fun emitError(ex: Throwable): Unit

Signal an exception to all current and future collectors when they are ready.

hasCollectors

fun hasCollectors(): Boolean

Returns true if this subject has collectors waiting for data.

Extension Functions

concatWith

fun <T> Flow<T>.concatWith(other: Flow<T>): Flow<T>

Emit values from the upstream followed by values from the other flow.

flatMapDrop

fun <T, R> Flow<T>.flatMapDrop(mapper: suspend (T) -> Flow<R>): Flow<R>

Maps items from the upstream to Flow and relays its items while dropping upstream items until the current inner Flow completes.

groupBy

fun <T, K> Flow<T>.groupBy(keySelector: suspend (T) -> K): Flow<GroupedFlow<K, T>>

Groups the upstream values into their own Flows keyed by the value returned by the keySelector function.

fun <T, K, V> Flow<T>.groupBy(keySelector: suspend (T) -> K, valueSelector: suspend (T) -> V): Flow<GroupedFlow<K, V>>

Groups the mapped upstream values into their own Flows keyed by the value returned by the keySelector function.

onBackpressurureDrop

fun <T> Flow<T>.onBackpressurureDrop(): Flow<T>

Drops items from the upstream when the downstream is not ready to receive them.

parallel

fun <T> Flow<T>.parallel(parallelism: Int, runOn: (Int) -> CoroutineDispatcher): ParallelFlow<T>

Consumes the upstream and dispatches individual items to a parrallel rail of the parallel flow for further consumption.

publish

fun <T, R> Flow<T>.publish(transform: suspend (Flow<T>) -> Flow<R>): Flow<R>

Shares a single collector towards the upstream source and multicasts values to any number of consumers which then can produce the output flow of values.

replay

fun <T, R> Flow<T>.replay(transform: suspend (Flow<T>) -> Flow<R>): Flow<R>

Shares a single collector towards the upstream source and multicasts cached values to any number of consumers which then can produce the output flow of values.

fun <T, R> Flow<T>.replay(maxSize: Int, transform: suspend (Flow<T>) -> Flow<R>): Flow<R>

Shares a single collector towards the upstream source and multicasts up to a given maxSize number of cached values to any number of consumers which then can produce the output flow of values.

fun <T, R> Flow<T>.replay(maxTime: Long, unit: TimeUnit, transform: suspend (Flow<T>) -> Flow<R>): Flow<R>

Shares a single collector towards the upstream source and multicasts up to maxTime old cached values to any number of consumers which then can produce the output flow of values.

fun <T, R> Flow<T>.replay(maxSize: Int, maxTime: Long, unit: TimeUnit, transform: suspend (Flow<T>) -> Flow<R>): Flow<R>
fun <T, R> Flow<T>.replay(maxSize: Int, maxTime: Long, unit: TimeUnit, timeSource: (TimeUnit) -> Long, transform: suspend (Flow<T>) -> Flow<R>): Flow<R>

Shares a single collector towards the upstream source and multicasts up to a given maxSize number and up to maxTime old cached values to any number of consumers which then can produce the output flow of values.

startCollectOn

fun <T> Flow<T>.startCollectOn(dispatcher: CoroutineDispatcher): Flow<T>

Stats collecting the upstream on the specified dispatcher.

takeUntil

fun <T, U> Flow<T>.takeUntil(other: Flow<U>): Flow<T>

Consumes the main source until the other source emits an item or completes.

toList

fun <T> Flow<T>.toList(): Flow<List<T>>

Collects all items of the upstream into a list.