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

rafal

04/10/2017, 8:07 AM
Hello everyone. Do you know whether there Is any built-in mechanism to emulate rx's `BehaviorSubject`/`ReplaySubject` on `Channel`s ?
e

elizarov

04/10/2017, 8:24 AM
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

rafal

04/10/2017, 9:21 AM
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

elizarov

04/10/2017, 9:28 AM
The key question here is what kind of behaviour you are exactly looking for? What is your use-case?
r

rafal

04/10/2017, 9:34 AM
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

elizarov

04/10/2017, 9:41 AM
Do you need multiple subscribers (consumers) or just a channel between producer and consumer?
r

rafal

04/10/2017, 9:45 AM
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

elizarov

04/10/2017, 9:47 AM
Do you need to support multiple active view both showing the same state?
r

rafal

04/10/2017, 9:48 AM
At the moment I doubt it's necessary, however I don't know what future holds.
e

elizarov

04/10/2017, 9:50 AM
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

rafal

04/10/2017, 10:03 AM
Thanks for all the hints. I'm not sure buffering is sth I can use. Nonetheless I'll definitely look into it.
k

kenkyee

04/10/2017, 12:57 PM
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

elizarov

04/10/2017, 1:29 PM
So what is wrong with using Rx subjects for that purpose?
k

kenkyee

04/10/2017, 9:54 PM
Nothing... Just depends if you want coroutines to be able to do everything ☺️
e

elizarov

04/11/2017, 6:56 AM
🙂
3 Views