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

PublishSubject

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

Multicasts items to any number of collectors when they are ready to receive.

Parameters

Constructors

<init>

PublishSubject()

Multicasts items to any number of collectors when they are ready to receive.

Functions

collectSafely

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

Start collecting signals from this PublishSubject.

collectorCount

fun collectorCount(): Int

Returns the current number of collectors.

complete

suspend fun complete(): Unit

Indicate no further items will be emitted

emit

suspend fun emit(value: T): Unit

Emit the value to all current collectors, waiting for each of them to be ready for consuming it.

emitError

suspend fun emitError(ex: Throwable): Unit

Throw an error on the consumer side.

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.