Updated on 2026-08-14

This commit is contained in:
Tangem 2023-04-03 07:48:17 +03:00
parent 3744216681
commit 5ee06b0a31
2 changed files with 14 additions and 3 deletions

View file

@ -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()
}
}

View file

@ -359,7 +359,7 @@ internal class SwapViewModel @Inject constructor(
lastAmount.value = cutValue
uiState =
stateBuilder.updateSwapAmount(uiState, inputNumberFormatter.formatWithThousands(cutValue, decimals))
amountDebouncer.debounce(DEBOUNCE_AMOUNT_DELAY, viewModelScope) {
amountDebouncer.debounce(viewModelScope, DEBOUNCE_AMOUNT_DELAY) {
startLoadingQuotes(fromToken, toToken, lastAmount.value)
}
}