KDebouncer
class KDebouncer<T>(scope: CoroutineScope, val delay: Duration = 300.milliseconds, val leading: Boolean = false)(source)
A coroutine-based debouncer that delays forwarding values until no new values have arrived for the specified delay duration.
This is typically used for search fields, auto-saves, or any input-triggered actions that should only execute after the user has stopped typing.
Basic usage (Flow-based)
val debouncer = KDebouncer<String>(scope = viewModelScope, delay = 300.milliseconds)
// Emit from a text-field's onValueChange:
onValueChange = { text -> debouncer.emit(text) }
// Collect the stable values:
viewModelScope.launch {
debouncer.flow.collect { stableText -> search(stableText) }
}Content copied to clipboard
Callback-style usage
val debouncer = KDebouncer<String>(scope = viewModelScope)
debouncer.onDebounced { query -> viewModel.search(query) }
// Then in UI:
onValueChange = { debouncer.emit(it) }Content copied to clipboard
Parameters
scope
The CoroutineScope in which the debouncing coroutine runs.
delay
The quiet period duration before a value is forwarded. Default: 300ms.
leading
If true, emit the first value immediately and suppress subsequent values until the quiet period elapses. Default: false.
Type Parameters
T
The type of values being debounced.
Properties
Functions
Link copied to clipboard
Registers a suspending block that is called each time a debounced value arrives. Replaces any previously registered callback.