Skip to content

Commit e9e737d

Browse files
author
İbrahim Yilmaz
committed
Instead of using Jvm bounded Timer, current TimerChannel is used with receiveAsFlow extension function.
1 parent 7e15913 commit e9e737d

File tree

2 files changed

+27
-41
lines changed

2 files changed

+27
-41
lines changed
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package kotlinx.coroutines.flow
22

33
import kotlinx.coroutines.Job
4-
import kotlinx.coroutines.channels.awaitClose
4+
import kotlinx.coroutines.channels.TickerMode
5+
import kotlinx.coroutines.channels.ticker
56
import java.util.*
6-
import kotlin.concurrent.schedule
7+
import kotlin.coroutines.CoroutineContext
8+
import kotlin.coroutines.EmptyCoroutineContext
79

810
/**
911
* Creates a flow that produces the first item after the given initial delay and subsequent items with the
@@ -13,21 +15,17 @@ import kotlin.concurrent.schedule
1315
*
1416
* This Flow stops producing elements immediately after [Job.cancel] invocation.
1517
*
16-
* @param period period between each element in milliseconds.
17-
* @param initialDelay delay after which the first element will be produced (it is equal to [period] by default) in milliseconds.
18+
* @param delayMillis delay between each element in milliseconds.
19+
* @param initialDelayMillis delay after which the first element will be produced (it is equal to [delayMillis] by default) in milliseconds.
20+
* @param context context of the producing coroutine.
21+
* @param mode specifies behavior when elements are not received ([FIXED_PERIOD][TickerMode.FIXED_PERIOD] by default).
1822
*/
1923
public fun tickerFlow(
20-
period: Long,
21-
initialDelay: Long = period
22-
): Flow<Unit> = callbackFlow {
23-
require(period > 0)
24-
require(initialDelay > -1)
25-
26-
val timer = Timer()
27-
timer.schedule(initialDelay, period) {
28-
offer(Unit)
29-
}
30-
31-
awaitClose { timer.cancel() }
32-
}
33-
24+
delayMillis: Long,
25+
initialDelayMillis: Long = delayMillis,
26+
context: CoroutineContext = EmptyCoroutineContext,
27+
mode: TickerMode = TickerMode.FIXED_PERIOD
28+
): Flow<Unit> {
29+
require(delayMillis > 0)
30+
return ticker(delayMillis, initialDelayMillis, context, mode).receiveAsFlow()
31+
}

kotlinx-coroutines-core/jvm/test/flow/TickerFlowTest.kt

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package flow
22

33
import kotlinx.coroutines.TestBase
4-
import kotlinx.coroutines.cancelAndJoin
54
import kotlinx.coroutines.delay
65
import kotlinx.coroutines.flow.launchIn
76
import kotlinx.coroutines.flow.onEach
@@ -37,17 +36,14 @@ class TickerFlowTest : TestBase() {
3736
val inbox = mutableListOf<Unit>()
3837

3938
// WHEN
40-
val periodicTicker =
41-
tickerFlow(100, 100).onEach {
42-
inbox.add(Unit)
43-
}.launchIn(this)
39+
tickerFlow(100, 200).onEach {
40+
inbox.add(Unit)
41+
}.launchIn(this)
4442

4543
delay(500)
4644

4745
// THEN
4846
assertEquals(4, inbox.size)
49-
50-
periodicTicker.cancelAndJoin()
5147
}
5248

5349
@Test
@@ -56,17 +52,14 @@ class TickerFlowTest : TestBase() {
5652
val inbox = mutableListOf<Unit>()
5753

5854
// WHEN
59-
val periodicTicker =
60-
tickerFlow(100, 0).onEach {
61-
inbox.add(Unit)
62-
}.launchIn(this)
55+
tickerFlow(100, 0).onEach {
56+
inbox.add(Unit)
57+
}.launchIn(this)
6358

6459
delay(500)
6560

6661
// THEN
67-
assertEquals(5, inbox.size)
68-
69-
periodicTicker.cancelAndJoin()
62+
assertEquals(6, inbox.size)
7063
}
7164

7265

@@ -76,17 +69,14 @@ class TickerFlowTest : TestBase() {
7669
val inbox = mutableListOf<Unit>()
7770

7871
// WHEN
79-
val periodicTicker =
80-
tickerFlow(100).onEach {
81-
inbox.add(Unit)
82-
}.launchIn(this)
72+
tickerFlow(100).onEach {
73+
inbox.add(Unit)
74+
}.launchIn(this)
8375

8476
delay(500)
8577

8678
// THEN
87-
assertEquals(4, inbox.size)
88-
89-
periodicTicker.cancelAndJoin()
79+
assertEquals(5, inbox.size)
9080
}
9181

9282
@Test
@@ -106,6 +96,4 @@ class TickerFlowTest : TestBase() {
10696
// THEN
10797
assertEquals(0, inbox.size)
10898
}
109-
110-
11199
}

0 commit comments

Comments
 (0)