Hello everyone. Do you know whether there Is any b...
# coroutines
r
Hello everyone. Do you know whether there Is any built-in mechanism to emulate rx's `BehaviorSubject`/`ReplaySubject` on `Channel`s ?
e
A
Channel
is similar to a subject to start with — one or more coroutines can send to it (fan-in) and one or more coroutines can read from it (fan-out). This document explains the differences and parallels between Channels and Rx in more details: https://github.com/Kotlin/kotlinx.coroutines/blob/master/reactive/coroutines-guide-reactive.md What kind of functionality you are exactly looking at? Do you need to broadcast to multiple coroutines and/or to store all events for replay later?
r
Thank @elizarov for a link to a detailed documentation. 1. Long story short - `Channel`s cannot replay elements in any way and to obtain similar behavior I need to fall back rx-based coroutines. Am I correct? 2. Would it be wrong to implement custom
Channel
that holds reference to last element and resend it each time someone "subscribes"?
e
The key question here is what kind of behaviour you are exactly looking for? What is your use-case?
r
I'm trying to implement MVI pattern based on Mosby library using coroutines (http://hannesdorfmann.com/android/mosby3-mvi-4). In general there's a object that generates ViewState based on user's input. However due to Android's specific lifecycle, there are some cases (e.g. screen rotation) when the last state needs to be resend as a new view is being recreated. In Rx version this is done with
BehaviorSubject
e
Do you need multiple subscribers (consumers) or just a channel between producer and consumer?
r
Multiple consumers. view#1 (consumer#1), user rotates screen, Android destroys view#1, creates view#2 (consumer#2). view#2 must immediately receive last valid state. Especially when whole view is rather static and updates are rare
e
Do you need to support multiple active view both showing the same state?
r
At the moment I doubt it's necessary, however I don't know what future holds.
e
So, here is the deal. The support for multiple consumers in CSP world (that is the world the channels come from) is called “broadcast”. Whether we need it to be support “out of the box” is discussed in this issue: https://github.com/Kotlin/kotlinx.coroutines/issues/54 You are really welcome to join the discussion.
It does popup from time to time and people a trying to replace Rx with Channels, so I’m starting to get inclined that we should have it implemented in
kotlinx.coroutines
, however it is still an open question on what kind of functionality it shall support out-of-the box
Anyway, I do suggest to watch that issue, at least
It also has a links on some previous discussions in this slack channel with pretty straght-forward implementation
r
Thanks for all the hints. I'm not sure buffering is sth I can use. Nonetheless I'll definitely look into it.
k
FWIW, I'd be inclined to include it. I've used Rx subjects to replace event buses or to invert UI listeners so multiple models can listen to an event... They're useful.
e
So what is wrong with using Rx subjects for that purpose?
k
Nothing... Just depends if you want coroutines to be able to do everything ☺️
e
🙂