-
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
Add debounce selector #2148
Changes from 4 commits
ea8d564
2dc4968
59751da
f188316
3f108cb
8e8feb7
78a20ba
7c6fb41
ca53fb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: | ||
|
@@ -40,36 +40,46 @@ import kotlin.time.* | |
*/ | ||
@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 | ||
} | ||
} | ||
require(timeoutMillis >= 0L) { "Debounce timeout should not be negative" } | ||
if (timeoutMillis == 0L) return this | ||
return 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) { | ||
* 0L | ||
* } else { | ||
* 300L | ||
* } | ||
* } | ||
* ``` | ||
* produces `3, 4, 6`. | ||
* | ||
* @param timeoutMillisSelector [T] is the emitted value and the return value is timeout in milliseconds. | ||
*/ | ||
@ExperimentalTime | ||
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. This one should not have |
||
@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 +109,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 = 0) { | ||
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 >= 0L) { "Debounce timeout should not be negative" } | ||
// 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]. | ||
* | ||
|
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)