Guys, hi. Question: if I want to use something lik...
# coroutines
v
Guys, hi. Question: if I want to use something like PublishSubject from Rx, so I can both emit and subscribe to it, what is the analogue in coroutines? Do we still use some kind of Channel or Flow already has this functionality?
w
PublishSubject
is a
BroadcastChannel<T>(1)
d
StateFlow
is the best available tool right now.
SharedFlow
is more powerful and will be released within the next 2 months.
w
BehaviorSubject
can be replaced with a
ConflatedBroadcastChannel
.
d
Ah. In that case, wait for
SharedFlow
while you use
BroadcastChannel(1)
.
v
thank you!
j
@wasyl then
BehaviorSubject
equivalent is
StateFlow
when
ConflatedBroadcastChannel
dissapear
đź‘Ť 1
w
Right, I forgot
StateFlow
is already released and working, I haven’t migrated to it myself yet. It’s a direct replacement for
ConflatedBroadcastChannel(1)
, no (obvious) surprises right?
d
Just around closing. It cannot (or doesn't have to) be closed (yet?).
🤔 1
w
Thanks! 🙂
j
It should be I guess @wasyl, they will deprecate
ConflatedBroadcastChannel
when
StateFlow
is released
m
I think there is one more difference between
StateFlow
and
ConflatedBroadcastChannel
In case when you set/emit the same value twice StateFlow won’t emit an update
StateFlow
 conflation is based on equality like distinctUntilChanged operator, unlike conflation in 
ConflatedBroadcastChannel
 that is based on reference identity.
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/
đź‘Ť 2
t