Skip to content

Instantly share code, notes, and snippets.

@tuannha95
Forked from faruktoptas/debounce.kt
Created July 29, 2020 04:15
Show Gist options
  • Save tuannha95/784718a9335be40d388801bc4b5538e3 to your computer and use it in GitHub Desktop.
Save tuannha95/784718a9335be40d388801bc4b5538e3 to your computer and use it in GitHub Desktop.
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)
@Stephen-cp
Copy link

what is onChange method ?
how to implement this in edit text ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment