Erik
09/08/2020, 1:30 PMdebounce
input. I'd like to know if some value is currently being debounced (and I'd like to know the value), so that I don't have to wait for it to be collected downstream. How would I do that?Erik
09/08/2020, 1:39 PMstreetsofboston
09/08/2020, 1:53 PMErik
09/08/2020, 2:00 PMErik
09/08/2020, 2:01 PMstreetsofboston
09/08/2020, 2:06 PMErik
09/08/2020, 3:32 PMDaniele Segato
09/08/2020, 3:50 PMthrottleFirst
I believe. Isn't there an operator like this for Flow?Daniele Segato
09/08/2020, 3:52 PMfun <T> Flow<T>.throttleFist(windowDuration: Long): Flow<T> = flow {
var windowStartTime = System.currentTimeMillis()
var emitted = false
collect { value ->
val currentTime = System.currentTimeMillis()
val delta = currentTime - windowStartTime
if (delta >= windowDuration) {
windowStartTime += delta / windowDuration * windowDuration
emitted = false
}
if (!emitted) {
emit(value)
emitted = true
}
}
}
this should work as a throttleFirst operator (implementation linked here: https://github.com/Kotlin/kotlinx.coroutines/issues/1446)