-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add debounce selector #2148
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
Closed
Closed
Add debounce selector #2148
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ea8d564
Add debounce selector
mkano9 2dc4968
Allow 0 debounce time only for the selector
mkano9 59751da
Remove <= 0ms illegal argument exceptions
mkano9 f188316
Add ExperimentalTime, throw IAE on negative, 0 capacity
mkano9 3f108cb
Update shared-mutable-state-and-concurrency.md (#2309)
MasoodFallahpoor 8e8feb7
Correct kdoc, optimize 0ms timeout, fix wrong annotation
mkano9 78a20ba
Merge branch 'master' of https://github.com/Kotlin/kotlinx.coroutines…
mkano9 7c6fb41
Revert "Merge branch 'master' of https://github.com/Kotlin/kotlinx.co…
mkano9 ca53fb2
Revert "Revert "Merge branch 'master' of https://github.com/Kotlin/ko…
mkano9 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 hidden or 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 |
---|---|---|
|
@@ -16,7 +16,7 @@ import kotlin.time.* | |
|
||
/** | ||
* Returns a flow that mirrors the original flow, but filters out values | ||
* that are followed by the newer values within the given [timeout][timeoutMillis]. | ||
* that are followed by the newer values within the given [timeoutMillis]. | ||
* The latest value is always emitted. | ||
* | ||
* Example: | ||
|
@@ -37,39 +37,45 @@ import kotlin.time.* | |
* | ||
* Note that the resulting flow does not emit anything as long as the original flow emits | ||
* items faster than every [timeoutMillis] milliseconds. | ||
* @param timeoutMillis must be positive | ||
*/ | ||
@FlowPreview | ||
public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> { | ||
require(timeoutMillis > 0) { "Debounce timeout should be positive" } | ||
return scopedFlow { downstream -> | ||
// Actually Any, KT-30796 | ||
val values = produce<Any?>(capacity = Channel.CONFLATED) { | ||
collect { value -> send(value ?: NULL) } | ||
} | ||
var lastValue: Any? = null | ||
while (lastValue !== DONE) { | ||
select<Unit> { | ||
// Should be receiveOrClosed when boxing issues are fixed | ||
values.onReceiveOrNull { | ||
if (it == null) { | ||
if (lastValue != null) downstream.emit(NULL.unbox(lastValue)) | ||
lastValue = DONE | ||
} else { | ||
lastValue = it | ||
} | ||
} | ||
public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> = debounceInternal { timeoutMillis } | ||
|
||
lastValue?.let { value -> | ||
// set timeout when lastValue != null | ||
onTimeout(timeoutMillis) { | ||
lastValue = null // Consume the value | ||
downstream.emit(NULL.unbox(value)) | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* A variation of [debounce] that allows specifying the timeout value dynamically. | ||
* | ||
* Example: | ||
* ``` | ||
* flow { | ||
* emit(1) | ||
* delay(100) | ||
* emit(2) | ||
* delay(100) | ||
* emit(3) | ||
* delay(1000) | ||
* emit(4) | ||
* delay(100) | ||
* emit(5) | ||
* delay(100) | ||
* emit(6) | ||
* }.debounce { | ||
* if (it == 4) { | ||
* 1L | ||
* } else { | ||
* 300L | ||
* } | ||
* } | ||
* ``` | ||
* produces `3, 4, 6`. | ||
* | ||
* @param timeoutMillisSelector [T] is the emitted value and the return value must be a positive timeout in milliseconds | ||
*/ | ||
@FlowPreview | ||
public fun <T> Flow<T>.debounce(timeoutMillisSelector: (T) -> Long): Flow<T> = | ||
debounceInternal { emittedItem -> | ||
timeoutMillisSelector(emittedItem) | ||
} | ||
} | ||
|
||
/** | ||
* Returns a flow that mirrors the original flow, but filters out values | ||
|
@@ -99,6 +105,39 @@ public fun <T> Flow<T>.debounce(timeoutMillis: Long): Flow<T> { | |
@FlowPreview | ||
public fun <T> Flow<T>.debounce(timeout: Duration): Flow<T> = debounce(timeout.toDelayMillis()) | ||
|
||
private fun <T> Flow<T>.debounceInternal(timeoutMillisSelector: (T) -> Long) : Flow<T> = | ||
scopedFlow { downstream -> | ||
// Actually Any, KT-30796 | ||
val values = produce<Any?>(capacity = Channel.CONFLATED) { | ||
collect { value -> send(value ?: NULL) } | ||
} | ||
var lastValue: Any? = null | ||
while (lastValue !== DONE) { | ||
select<Unit> { | ||
// Should be receiveOrClosed when boxing issues are fixed | ||
values.onReceiveOrNull { | ||
if (it == null) { | ||
if (lastValue != null) downstream.emit(NULL.unbox(lastValue)) | ||
lastValue = DONE | ||
} else { | ||
lastValue = it | ||
} | ||
} | ||
|
||
lastValue?.let { value -> | ||
val unboxedValue: T = NULL.unbox(value) | ||
val timeoutMillis = timeoutMillisSelector(unboxedValue) | ||
require(timeoutMillis > 0) { "Debounce timeout should be positive" } | ||
mkano9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// set timeout when lastValue != null | ||
onTimeout(timeoutMillis) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A case with |
||
lastValue = null // Consume the value | ||
downstream.emit(unboxedValue) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns a flow that emits only the latest value emitted by the original flow during the given sampling [period][periodMillis]. | ||
* | ||
|
This file contains hidden or 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
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.
Please, leave this part of the docs as before (it is consistent with how
sample
is documented, too)