https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

Lilly

04/27/2021, 7:48 PM
I have a flow producer which emits continuously a specific byte to represent an idle mode. I would like to drop these bytes but log them every 2 or 3 seconds. Any ideas how to achieve this?
d

Derek Ellis

04/27/2021, 7:51 PM
What about the
sample
operator?
l

Lilly

04/27/2021, 8:12 PM
This would also filter the "good" bytes 😞 Thats says I'm also collecting useful bytes when not idling
e

ephemient

04/27/2021, 8:20 PM
Copy code
sealed class Mode {
    class Active : Mode() // Active() != Active()
    data class Idle(val unique: Long) : Mode()
}

.distinctUntilChangedBy { it ->
    if (isIdle(it)) Mode.Idle(System.currentTimeMillis() / 2500) else Mode.Active()
}
not as clever as
.sample()
but might be sufficient
l

Lilly

04/27/2021, 9:51 PM
Thanks guys
3 Views