poll
fun <T> poll(concurrency: ConcurrencyStrategy = ConcurrencyStrategy.disabled, stopOnEmptyList: Boolean = false, interval: Duration? = null, f: suspend ConcurrencyInfo.() -> List<T>): Flow<T>
Creates a flow that continuously polls elements concurrently by successively applying the f function. The flow stops based on the stopOnEmptyList parameter or when the coroutine context is no longer active. Concurrency is controlled by the concurrency strategy.
Return
A Flow of elements of type T, polled concurrently using the provided f function.
Example usage:
suspend fun fetchData(): List<Data> = ... // fetch data from somewhere
poll(ConcurrencyStrategy.increaseByOne(4), stopOnEmptyList = true) { fetchData() }
.collect { println(it) }
Content copied to clipboard
Parameters
concurrency
The ConcurrencyStrategy to control the number of concurrent polling operations allowed. Defaults to a static strategy with concurrency of 1.
stopOnEmptyList
If true, the flow will stop when an empty list of elements is received. Defaults to false. If false, the flow will continue polling elements indefinitely.
f
A lambda with receiver of type ConcurrencyInfo that produces a list of elements of type T.