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

magnumrocha

05/23/2021, 7:28 PM
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

Erik

05/23/2021, 7:47 PM
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

magnumrocha

05/23/2021, 7:49 PM
thank you, I will try it and make some tests…
u

ursus

05/26/2021, 12:38 PM
so you need to emit duplicated and be able to peek at the last value?
e

Erik

05/26/2021, 1:20 PM
If the type of data is
Unit
and there always is 1 value, then peeking makes no sense 😉
m

magnumrocha

05/26/2021, 1:22 PM
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

Erik

05/26/2021, 1:23 PM
Did you make it work with the shared flow?
m

magnumrocha

05/26/2021, 1:23 PM
need to try yet…. sorry 😁
u

ursus

05/26/2021, 5:02 PM
@Erik thats what I was getting at
m

magnumrocha

08/02/2021, 8:41 PM
@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….
3 Views