mirror of https://github.com/tasks/tasks
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.
20 lines
464 B
Kotlin
20 lines
464 B
Kotlin
4 years ago
|
package org.tasks.sync
|
||
|
|
||
|
import kotlinx.coroutines.delay
|
||
|
import timber.log.Timber
|
||
|
|
||
|
class Debouncer constructor(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.invoke(immediate)
|
||
|
} else {
|
||
|
Timber.v("debouncing $tag")
|
||
|
}
|
||
|
}
|
||
|
}
|