You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tasks/app/src/main/java/org/tasks/sync/Debouncer.kt

20 lines
445 B
Kotlin

package org.tasks.sync
import kotlinx.coroutines.delay
import timber.log.Timber
class Debouncer(private val tag: String, private val block: suspend (Boolean) -> Unit) {
private var count = 0
suspend fun sync(immediate: Boolean) {
val thisCount = ++count
delay(1000)
if (immediate || thisCount == count) {
block(immediate)
} else {
Timber.v("debouncing $tag")
}
}
}