Throttle pod device data for the UI, notifications or widgets.

We don't need faster updates than 1 per second.
(We keep reactions at maximum speed tho)
This commit is contained in:
darken
2022-11-05 12:11:09 +01:00
committed by Matthias Urhahn
parent b9f4992a33
commit 6340f73d61
5 changed files with 16 additions and 1 deletions
@@ -7,6 +7,7 @@ import eu.darken.capod.common.debug.logging.log
import eu.darken.capod.common.error.hasCause
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlin.time.Duration
@@ -71,4 +72,11 @@ fun <T> Flow<T>.setupCommonEventHandlers(tag: String, identifier: () -> String)
log(tag, ERROR) { "${identifier()} failed: ${it.asLog()}" }
throw it
}
}
fun <T> Flow<T>.throttleLatest(delayMillis: Long): Flow<T> = this
.conflate()
.transform {
emit(it)
delay(delayMillis)
}