-
Notifications
You must be signed in to change notification settings - Fork 1.9k
tickerFlow
implementation
#1908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ibrahimyilmaz
wants to merge
5
commits into
Kotlin:develop
Choose a base branch
from
ibrahimyilmaz:flowable_ticker
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2d231f6
Flow based Ticker implemented.
70410db
ticker renamed to tickerFlow to be more specific.
7e15913
testZeroNegativePeriod renamed to testZeroPeriod
e9e737d
Instead of using Jvm bounded Timer, current TimerChannel is used with…
1c7f015
Documentation is updated according to current implementation.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package flow | ||
|
||
import kotlinx.coroutines.TestBase | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.flow.tickerFlow | ||
import java.util.concurrent.CancellationException | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
|
||
class TickerFlowTest : TestBase() { | ||
|
||
@Test(expected = IllegalArgumentException::class) | ||
fun testNegativePeriod() = runTest { | ||
// WHEN | ||
tickerFlow(-1).launchIn(this) | ||
} | ||
|
||
@Test(expected = IllegalArgumentException::class) | ||
fun testZeroPeriod() = runTest { | ||
// WHEN | ||
tickerFlow(0).launchIn(this) | ||
} | ||
|
||
@Test(expected = IllegalArgumentException::class) | ||
fun testNegativeInitialDelay() = runTest { | ||
// WHEN | ||
tickerFlow(100, -1).launchIn(this) | ||
} | ||
|
||
@Test | ||
fun testInitialDelay() = runTest { | ||
// GIVEN | ||
val inbox = mutableListOf<Unit>() | ||
|
||
// WHEN | ||
tickerFlow(100, 200).onEach { | ||
inbox.add(Unit) | ||
}.launchIn(this) | ||
|
||
delay(500) | ||
|
||
// THEN | ||
assertEquals(4, inbox.size) | ||
} | ||
|
||
@Test | ||
fun testZeroInitialDelay() = runTest { | ||
// GIVEN | ||
val inbox = mutableListOf<Unit>() | ||
|
||
// WHEN | ||
tickerFlow(100, 0).onEach { | ||
inbox.add(Unit) | ||
}.launchIn(this) | ||
|
||
delay(500) | ||
|
||
// THEN | ||
assertEquals(6, inbox.size) | ||
} | ||
|
||
|
||
@Test | ||
fun testReceive() = runTest { | ||
// GIVEN | ||
val inbox = mutableListOf<Unit>() | ||
|
||
// WHEN | ||
tickerFlow(100).onEach { | ||
inbox.add(Unit) | ||
}.launchIn(this) | ||
|
||
delay(500) | ||
|
||
// THEN | ||
assertEquals(5, inbox.size) | ||
} | ||
|
||
@Test | ||
fun testDoNotReceiveAfterCancel() = runTest { | ||
// GIVEN | ||
val inbox = mutableListOf<Unit>() | ||
|
||
// WHEN | ||
val periodicTicker = | ||
tickerFlow(100).onEach { | ||
inbox.add(Unit) | ||
}.launchIn(this) | ||
|
||
delay(50) | ||
periodicTicker.cancel(CancellationException()) | ||
|
||
// THEN | ||
assertEquals(0, inbox.size) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the channel is hidden here, we probably want to handle cancellation properly.
Using
consumeAsFlow()
instead ofreceiveAsFlow()
should do the trick I think.