Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kotlin coroutine debounce for EditText
fun <T> debounce(
waitMs: Long = 300L,
scope: CoroutineScope,
destinationFunction: (T) -> Unit
): (T) -> Unit {
var debounceJob: Job? = null
return { param: T ->
debounceJob?.cancel()
debounceJob = scope.launch {
delay(waitMs)
destinationFunction(param)
}
}
}
val debounceTextChange = debounce(300L, viewModel.viewModelScope, viewModel::search)
editText.onChange(debounceTextChange)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment