|
| 1 | +package kotlinx.coroutines.experimental.channels |
| 2 | + |
| 3 | +import kotlinx.coroutines.experimental.* |
| 4 | +import kotlinx.coroutines.experimental.timeunit.* |
| 5 | +import kotlin.coroutines.experimental.* |
| 6 | + |
| 7 | + |
| 8 | +/** |
| 9 | + * Creates rendezvous channel which produces the first item after the given initial delay and subsequent items with the |
| 10 | + * given delay between them. Backpressure is guaranteed by [RendezvousChannel]. |
| 11 | + * |
| 12 | + * This channel stops producing elements immediately after [ReceiveChannel.cancel] invocation. |
| 13 | + * **Note** producer to this channel is dispatched via [Unconfined] dispatcher and started eagerly |
| 14 | + * |
| 15 | + * @param initialDelay delay after which the first item will be produced |
| 16 | + * @param delay delay between each element |
| 17 | + * @param unit unit of time that applies to [initialDelay] and [delay] |
| 18 | + * @param coroutineContext context of the producing coroutine |
| 19 | + * @param fixedPeriod whether producer should use fixed delay or should attempt to adjust delay when consumer cannot keep up |
| 20 | + */ |
| 21 | +public fun ticker( |
| 22 | + delay: Long, |
| 23 | + unit: TimeUnit = TimeUnit.MILLISECONDS, |
| 24 | + initialDelay: Long = delay, |
| 25 | + coroutineContext: CoroutineContext = Unconfined, |
| 26 | + fixedPeriod: Boolean = false |
| 27 | +): ReceiveChannel<Unit> { |
| 28 | + return if (fixedPeriod) fixedTicker(delay, unit, initialDelay, coroutineContext) |
| 29 | + else adjustingTicker(delay, unit, initialDelay, coroutineContext) |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Creates rendezvous channel which produces the first item after the given initial delay and subsequent items after |
| 34 | + * given delay. |
| 35 | + * |
| 36 | + * Producer to resulting channel tries to adjust delay if consumer cannot keep up: |
| 37 | + * ``` |
| 38 | + * val channel = adjustingTicker(delay = 100) |
| 39 | + * delay(350) // 250 ms late |
| 40 | + * println(channel.poll()) // prints Unit |
| 41 | + * println(channel.poll()) // prints null |
| 42 | + * |
| 43 | + * delay(50) |
| 44 | + * println(channel.poll() // prints Unit, delay was adjusted |
| 45 | + * delay(50) |
| 46 | + * println(channel.poll() // prints null, we'are not late relatively to previous element |
| 47 | + * ``` |
| 48 | + * |
| 49 | + * This channel stops producing elements immediately after [ReceiveChannel.cancel] invocation. |
| 50 | + * **Note** producer to this channel is dispatched via [Unconfined] dispatcher and started eagerly |
| 51 | + * |
| 52 | + * @param initialDelay delay after which the first item will be produced |
| 53 | + * @param delay delay between each elements |
| 54 | + * @param unit unit of time that applies to [initialDelay] and [delay] |
| 55 | + * @param coroutineContext context of the producing coroutine |
| 56 | + */ |
| 57 | +public fun adjustingTicker( |
| 58 | + delay: Long, |
| 59 | + unit: TimeUnit = TimeUnit.MILLISECONDS, |
| 60 | + initialDelay: Long = delay, |
| 61 | + coroutineContext: CoroutineContext = Unconfined |
| 62 | +): ReceiveChannel<Unit> { |
| 63 | + require(delay >= 0) { "Expected non-negative delay, but has $delay" } |
| 64 | + require(initialDelay >= 0) { "Expected non-negative initial delay, but has $initialDelay" } |
| 65 | + |
| 66 | + val result = RendezvousChannel<Unit>() |
| 67 | + launch(coroutineContext) { |
| 68 | + delay(initialDelay, unit) |
| 69 | + |
| 70 | + val delayNs = unit.toNanos(delay) |
| 71 | + var deadline = timeSource.nanoTime() |
| 72 | + while (true) { |
| 73 | + deadline += delayNs |
| 74 | + result.send(Unit) |
| 75 | + val now = timeSource.nanoTime() |
| 76 | + val nextDelay = (deadline - now).coerceAtLeast(0) |
| 77 | + if (nextDelay == 0L && delayNs != 0L) { |
| 78 | + val adjustedDelay = delayNs - (now - deadline) % delayNs |
| 79 | + deadline = now + adjustedDelay |
| 80 | + delay(adjustedDelay, java.util.concurrent.TimeUnit.NANOSECONDS) |
| 81 | + } else { |
| 82 | + delay(nextDelay, java.util.concurrent.TimeUnit.NANOSECONDS) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return result |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Creates rendezvous channel which produces the first item after the given initial delay and subsequent items after |
| 92 | + * given delay **after** consumption of the previous item. |
| 93 | + * |
| 94 | + * This channel stops producing elements immediately after [ReceiveChannel.cancel] invocation. |
| 95 | + * **Note** producer to this channel is dispatched via [Unconfined] dispatcher and started eagerly |
| 96 | + * |
| 97 | + * @param initialDelay delay after which the first item will be produced |
| 98 | + * @param delay delay between each elements |
| 99 | + * @param unit unit of time that applies to [initialDelay] and [delay] |
| 100 | + * @param coroutineContext context of the producing coroutine |
| 101 | + */ |
| 102 | +public fun fixedTicker( |
| 103 | + delay: Long, |
| 104 | + unit: TimeUnit = TimeUnit.MILLISECONDS, |
| 105 | + initialDelay: Long = delay, |
| 106 | + coroutineContext: CoroutineContext = Unconfined |
| 107 | +): ReceiveChannel<Unit> { |
| 108 | + require(delay >= 0) { "Expected non-negative delay, but has $delay" } |
| 109 | + require(initialDelay >= 0) { "Expected non-negative initial delay, but has $initialDelay" } |
| 110 | + |
| 111 | + val result = RendezvousChannel<Unit>() |
| 112 | + launch(coroutineContext) { |
| 113 | + delay(initialDelay, unit) |
| 114 | + while (true) { |
| 115 | + result.send(Unit) |
| 116 | + delay(delay, unit) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return result |
| 121 | +} |
0 commit comments