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

haroldadmin

06/03/2019, 6:21 AM
Hello everyone! I'm trying to use a
RendezvousChannel
in the place of
PublishSubject
from RxJava in a project, but I need a way to access the current element in the channel without removing it. Something like a 'peek' operation on a
LinkedBlockingQueue
. Now a
RendezvousChannel
has a method called
poll()
which does give me the current element (or null) but it also removes it from the channel. How can I perform a peek operation on a channel then?
l

louiscad

06/03/2019, 7:54 AM
Hello! You're using the wrong type of channel. You need to use
ConflatedBroadcastChannel
which is the correct analogue to RxJava's
PublishSubject
.
g

gildor

06/03/2019, 7:54 AM
ConflatedBroadcastChannel is more like BehaviorSubject
l

louiscad

06/03/2019, 7:58 AM
Possible, I'm really not familiar with RxJava.
ConflatedBroadcastChannel
has a
value
property to get last value without removing it though, so it suits the use case here.
g

gildor

06/03/2019, 8:02 AM
Also, I would say, that you cannot get current value of PublishSubject, PublishSubject analogue in coroutines is BroadcastChannel, but you also cannot get last emitted value without consuming it, this possible with ConflatedBroadcastChannel/BehaviorSubject, but it also means that every new subscriber also will get this value on subscribe (so, not like with PublishSubject)
h

haroldadmin

06/03/2019, 8:03 AM
You're right, I mixed
PublishSubject
with
BehaviourSubject
. A
BehaviourSubject
is what I meant to say.
ConflatedBroadcastChannel
looks to be the tool I need. Thanks for your help!
8 Views