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"
}
Content copied to clipboard