Updated on 2026-08-14
This commit is contained in:
parent
3744216681
commit
5ee06b0a31
2 changed files with 14 additions and 3 deletions
|
|
@ -8,6 +8,8 @@ import kotlinx.coroutines.delay
|
|||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
|
||||
suspend fun <R> runCatching(dispatcher: CoroutineDispatcher, block: suspend () -> R): Result<R> {
|
||||
return runCatching {
|
||||
|
|
@ -25,11 +27,20 @@ class Debouncer {
|
|||
|
||||
private var debounceJob: Job? = null
|
||||
|
||||
fun debounce(waitMs: Long = 300L, coroutineScope: CoroutineScope, destinationFunction: () -> Unit) {
|
||||
fun debounce(
|
||||
coroutineScope: CoroutineScope,
|
||||
waitMs: Long = 300L,
|
||||
context: CoroutineContext = EmptyCoroutineContext,
|
||||
destinationFunction: suspend () -> Unit,
|
||||
) {
|
||||
debounceJob?.cancel()
|
||||
debounceJob = coroutineScope.launch {
|
||||
debounceJob = coroutineScope.launch(context) {
|
||||
delay(waitMs)
|
||||
destinationFunction.invoke()
|
||||
}
|
||||
}
|
||||
|
||||
fun release() {
|
||||
debounceJob?.cancel()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue