https://kotlinlang.org logo
#feed
Title
# feed
e

elizarov

11/16/2020, 1:18 PM
The recent release of kotlinx-coroutines added the concept of shared flow which made broadcast channels obsolete. Here's the background story https://medium.com/@elizarov/shared-flows-broadcast-channels-899b675e805c
👀 4
⏸️ 9
K 13
👍 37
🍅 6
🙏 5
K 3
d

dave08

11/18/2020, 10:30 AM
Copy code
suspend fun postEvent(event: Event) {
        _events.emit(event) // suspends until subscribers receive it
    }
Does is suspend until all subscribers receive it, or just the first one? Is that configurable?
e

elizarov

11/18/2020, 10:31 AM
all.
👍🏼 1
d

dave08

11/18/2020, 10:47 AM
With the shared flow, events are broadcast to an unknown number (zero or more) of subscribers. In the absence of a subscriber, any posted event is immediately dropped.
There really isn't a way to add functionality to
SharedFlow
do such a thing without channels? Will it be added in the future, or is it some kind of limitiation of
SharedFlow
?
e

elizarov

11/18/2020, 10:48 AM
To do what thing?
d

dave08

11/18/2020, 10:48 AM
To suspend on emit for 0 subscribers for something like a SingleShotEventBus
e

elizarov

11/18/2020, 10:49 AM
But that's what Channel already does perfectly. There's no need to duplicate this feature in SharedFlow.
d

dave08

11/18/2020, 10:50 AM
But isn't it much heavier and low-level? Such a requirement isn't common enough to be implemented more high level?
e

elizarov

11/18/2020, 11:08 AM
No. Channel perfectly fits there both conceptually and from the implementation standpoint. We don't currently see any need to reimplement it. That was the whole point of the post.
d

dave08

11/18/2020, 11:27 AM
Thanks! The post was very good at clarifying where Flow and its variants play their role in replacing some of the Channel's functions, but I didn't really see the conceptual boundary where Channel is required. The SingleShotEventBus is just brought as an example of when a Channel should be used instead because SharedFlow can't do such a thing, but not WHY Channel SHOULD do that and NOT SharedFlow...
e

elizarov

11/18/2020, 11:33 AM
It is a conceptual difference between a flow and channel. A channel is a channel -- it is a well known concept in software engineering, it does it thing well, and there's no need to reinvent/reimplement it.
👍🏼 2
A
BroadcastChannel
, in essence, was a misnomer. We invented this name before we had sketched out the concept of a flow. As soon as we did it, we immediately saw that it is, conceptually, a flow, not a channel.
d

dave08

11/18/2020, 11:39 AM
So
BroadcastChannel
will be removed or renamed?
e

elizarov

11/18/2020, 12:26 PM
BroadcastChannel
will be deprecated and removed in the future.
d

dave08

11/18/2020, 12:28 PM
So a
SingleShotEventBus
for multiple listeners needs to use
receiveAsFlow().shareIn(..)
... I guess all this is starting to make sense now.. thanks alot!
e

elizarov

11/18/2020, 12:30 PM
No. No
shareIn
. Just
receiveAsFlow()
👍🏼 1
p

psh

11/18/2020, 7:23 PM
Slightly tangential question - why did you pick delicious cherry tomatoes as a header image for the story?
e

elizarov

11/18/2020, 8:19 PM
Great dish for sharing! ❤️
5 Views