Updated on 2026-08-14

This commit is contained in:
Tangem 2023-11-30 15:51:25 +03:00
parent 82c19782eb
commit f3aa08093d
21 changed files with 657 additions and 129 deletions

View file

@ -0,0 +1,57 @@
package com.tangem.utils.coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicBoolean
class PeriodicTask<T>(
private val delay: Long,
private val task: suspend () -> Result<T>,
private val onSuccess: (T) -> Unit,
private val onError: (Throwable) -> Unit,
) {
private var isActive: AtomicBoolean = AtomicBoolean(false)
suspend fun runTaskWithDelay() {
isActive.set(true)
while (isActive.get()) {
task.invoke()
.onSuccess {
if (!isActive.get()) {
return@onSuccess
}
onSuccess.invoke(it)
}
.onFailure {
if (!isActive.get()) {
return@onFailure
}
onError.invoke(it)
}
delay(delay)
}
}
fun cancel() {
isActive.set(false)
}
}
class SingleTaskScheduler<T> {
private var lastTask: PeriodicTask<T>? = null
fun scheduleTask(scope: CoroutineScope, task: PeriodicTask<T>) {
lastTask?.cancel()
lastTask = task
scope.launch {
task.runTaskWithDelay()
}
}
fun cancelTask() {
lastTask?.cancel()
}
}