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

ReplaySubject

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

Caches and replays some or all items to collectors.

Constructors

<init>

ReplaySubject()

Creates a ReplaySubject with an unbounded internal buffer caching all values received via emit.

ReplaySubject(maxSize: Int)

Creates a ReplaySubject that caches at most maxSize values to be replayed to late collectors.

ReplaySubject(maxTime: Long, unit: TimeUnit)

Creates a ReplaySubject that caches values at most for the given maxTime real-time duration and replays those upfront to late collectors.

ReplaySubject(maxSize: Int, maxTime: Long, unit: TimeUnit)

Creates a ReplaySubject that caches at most maxSize values at most for the given maxTime real-time duration and replays those upfront to late collectors.

ReplaySubject(maxSize: Int, maxTime: Long, unit: TimeUnit, timeSource: (TimeUnit) -> Long)

Creates a ReplaySubject that caches at most maxSize values at most for the given maxTime duration (measured via the custom timeSource) and replays those upfront to late collectors.

Functions

collectSafely

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

Accepts a collector and emits the cached values upfront and any subsequent value received by this ReplaySubject until the ReplaySubject gets terminated.

collectorCount

fun collectorCount(): Int

Returns the current number of collectors.

complete

suspend fun complete(): Unit

Indicate no further items will be produced.

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 Throwable to the collector.

hasCollectors

fun hasCollectors(): Boolean

Returns true if this PublishSubject has any collectors.

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.