I'm trying to create a cold
Flow
where a lot of updates can happen at once and I want the collector to only process the last one.
The producer and the consumer are both on the UI thread. The desired behavior is when the producer gets out of their loop and goes back to waiting for some event, then the consumer gets the latest state.
What I have now is a
callbackFlow
that is
conflated
.
val flow = callbackFlow<Any> {
event.register { offer(someValue) }
awaitClose { event.unregister() }
}.conflate()
val job = scope.launch() {
flow.collect { }
The scope is using Android's
MainScope
. And the event is sending events on the main thread.
When I was using a
MutableStateFlow
this behaves as I wanted, but with the conflated
callbackFlow
the caller is getting the first and last event, once all the events have been offerred.