fixedRate

fun fixedRate(periodInMillis: Long, dampen: Boolean = true, timeStamp: () -> ComparableTimeMark = { TimeSource.Monotonic.markNow() }): Flow<Unit>


fun fixedRate(period: Duration, dampen: Boolean = true, timeStamp: () -> ComparableTimeMark = { TimeSource.Monotonic.markNow() }): Flow<Unit>

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.

timeStamp

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