AbstractFlow

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

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
suspend override fun collect(collector: FlowCollector<T>)

Accepts the given collector and emits values into it.

Link copied to clipboard
abstract suspend fun collectSafely(collector: FlowCollector<T>)

Accepts the given collector and emits values into it.