https://kotlinlang.org logo
Title
v

Vsevolod Kaganovych

08/11/2020, 11:42 AM
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

wasyl

08/11/2020, 11:43 AM
PublishSubject
is a
BroadcastChannel<T>(1)
d

Dominaezzz

08/11/2020, 11:44 AM
StateFlow
is the best available tool right now.
SharedFlow
is more powerful and will be released within the next 2 months.
w

wasyl

08/11/2020, 11:44 AM
BehaviorSubject
can be replaced with a
ConflatedBroadcastChannel
.
d

Dominaezzz

08/11/2020, 11:46 AM
Ah. In that case, wait for
SharedFlow
while you use
BroadcastChannel(1)
.
v

Vsevolod Kaganovych

08/11/2020, 11:48 AM
thank you!
j

Javier

08/11/2020, 12:00 PM
@wasyl then
BehaviorSubject
equivalent is
StateFlow
when
ConflatedBroadcastChannel
dissapear
👍 1
w

wasyl

08/11/2020, 12:02 PM
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

Dominaezzz

08/11/2020, 12:03 PM
Just around closing. It cannot (or doesn't have to) be closed (yet?).
🤔 1
w

wasyl

08/11/2020, 12:12 PM
Thanks! 🙂
j

Javier

08/11/2020, 12:15 PM
It should be I guess @wasyl, they will deprecate
ConflatedBroadcastChannel
when
StateFlow
is released
m

mateusz.kwiecinski

08/11/2020, 1:18 PM
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

travis

08/11/2020, 4:30 PM