fixedRate

fun fixedRate(period: Duration, dampen: Boolean = true, timeStampInMillis: () -> Long = { timeInMillis() }): Flow<Unit>(source)


fun fixedRate(period: Long, dampen: Boolean = true, timeStampInMillis: () -> Long = { timeInMillis() }): Flow<Unit>(source)

Flow that emits Unit every period while taking into account how much time it takes downstream to consume the emission. If downstream takes longer to process than period than it immediately emits another Unit, if you set dampen to false it will send n = downstreamTime / period Unit elements immediately.

Use onEach { delay(timeMillis) } for an alternative that sleeps period between every element. This is different in that the time between every element is equal to the specified period, regardless of how much time it takes to process that tick downstream.

i.e, for a period of 1 second and a delay(100), the timestamps of the emission would be 1s, 2s, 3s, ... when using fixedRate. Whereas with onEach { delay(timeMillis) } it would run at timestamps 1s, 2.1s, 3.2s, ...

Parameters

period

period between Unit emits of the resulting Flow.

dampen

if you set dampen to false it will send n times period time it took downstream to process the emission.

timeStampInMillis

allows for supplying a different timestamp function, useful to override with runBlockingTest