//arrow-fx-coroutines/arrow.fx.coroutines/fixedRate

fixedRate

common

@ExperimentalTime

fun fixedRate(period: Duration, dampen: Boolean = true, timeStampInMillis: () -> Long = { timeInMillis() }): <[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)>

common fun fixedRate(period: Long, dampen: Boolean = true, timeStampInMillis: () -> Long = { timeInMillis() }): <[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)>

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 / periodUnit 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

common

   
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

Do you like Arrow?

Arrow Org
<