How can I use distinctUntilChanged()
but also add an expiry to it, which means if the same value is in the flow we still collect it because it was longer than expiry
milliseconds after the previous duplicated value was emitted.
flow {
emit("A") // printed
emit("B") // printed
emit("A") // printed
emit("A") // NOT printed because duplicate
delay(5000)
emit("A") // printed because 5 seconds elapsed which is more than expiry
}
.distinctUntilChanged(expiry = 2000)
.collect {
println(it)
}
I would like this to print:
A
B
A
A
Here's the code to test it:
@Test
fun `distinctUntilChanged works as expected`(): Unit = runBlocking {
flow {
emit("A") // printed
emit("B") // printed
emit("A") // printed
emit("A") // NOT printed because duplicate
delay(5000)
emit("A") // printed because 5 seconds elapsed which is more than expiry
}
.distinctUntilChanged(expiry = 2000)
.toList().also {
assertEquals("A", it[0])
assertEquals("B", it[1])
assertEquals("A", it[2])
assertEquals("A", it[3])
}
}