hi guys, what is a best replacement for `Conflated...
# coroutines
m
hi guys, what is a best replacement for
ConflatedBroadcastChannel(Unit)
using the
StateFlow
or
SharedFlow
? my first try is with a
MutableStateFlow<Boolean>(false)
and every time I have a change in my logic that dispatch a signal, I do:
myflow.value = !myflow.value
, but this looks weird 😅
e
Try a
MutableSharedFlow<Unit>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
and immediately emit
Unit
. Not sure if really equivalent, but it should be able to emit the same value multiple times and emitters should never suspend/fail.
👍 1
m
thank you, I will try it and make some tests…
u
so you need to emit duplicated and be able to peek at the last value?
e
If the type of data is
Unit
and there always is 1 value, then peeking makes no sense 😉
m
just to clarify, I use the
ConflatedBroadcastChannel(Unit)
just as a trigger to notify about some event… this is a easy approach, and
StateFlow<Unit>
will not work….
e
Did you make it work with the shared flow?
m
need to try yet…. sorry 😁
u
@Erik thats what I was getting at
m
@Erik the
MutableSharedFlow<Unit>(replay = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
behaviours like a
StateFlow
So, I’ve created an object like:
Copy code
sealed class BaseTriggerEvent {
    override fun equals(other: Any?): Boolean = false
    override fun hashCode(): Int = kotlin.random.Random.nextInt()
}

object TriggerEvent: BaseTriggerEvent()
That never will be equals the same object, so I can use like:
Copy code
private val refreshTrigger = MutableStateFlow(TriggerEvent)
that’s the best approach I could do so far….