AbstractFlow

abstract class AbstractFlow<T> : Flow<T> , CancellableFlow<T>

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.

Example of the implementation:

// list.asFlow() + collect counter
class CountingListFlow(private val values: List<Int>) : AbstractFlow<Int>() {
private val collectedCounter = AtomicInteger(0)

override suspend fun collectSafely(collector: FlowCollector<Int>) {
collectedCounter.incrementAndGet() // Increment collected counter
values.forEach { // Emit all the values
collector.emit(it)
}
}

fun toDiagnosticString(): String = "Flow with values $values was collected ${collectedCounter.value} times"
}

Constructors

AbstractFlow
Link copied to clipboard
common
fun AbstractFlow()

Functions

collect
Link copied to clipboard
common
suspend override fun collect(collector: FlowCollector<T>)
Accepts the given collector and emits values into it.
collectSafely
Link copied to clipboard
common
abstract suspend fun collectSafely(collector: FlowCollector<T>)
Accepts the given collector and emits values into it.

Sources

common source
Link copied to clipboard