-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathTickerFlow.kt
30 lines (28 loc) · 1.21 KB
/
TickerFlow.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package kotlinx.coroutines.flow
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.TickerMode
import kotlinx.coroutines.channels.ticker
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
/**
* Creates a flow that produces the first item after the given initial delay and subsequent items with the
* given delay between them.
*
* The resulting flow is basically using [ticker]
*
* This Flow stops producing elements immediately after [Job.cancel] invocation.
*
* @param delayMillis delay between each element in milliseconds.
* @param initialDelayMillis delay after which the first element will be produced (it is equal to [delayMillis] by default) in milliseconds.
* @param context context of the producing coroutine.
* @param mode specifies behavior when elements are not received ([FIXED_PERIOD][TickerMode.FIXED_PERIOD] by default).
*/
public fun tickerFlow(
delayMillis: Long,
initialDelayMillis: Long = delayMillis,
context: CoroutineContext = EmptyCoroutineContext,
mode: TickerMode = TickerMode.FIXED_PERIOD
): Flow<Unit> {
require(delayMillis > 0)
return ticker(delayMillis, initialDelayMillis, context, mode).receiveAsFlow()
}