-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Suspending broadcast flow emission #1901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What is your use-case? What kind of application scenario calls for this kind of behavior? |
At the moment I would be beneficial in the sign-in and sign-out processes of an Android app.
If I write that without subscriptions, the If I use a synchronous Flow, synchronous event bus or similar then the I've heard that a |
I'm not sure that
Here, I suppose that
In the code of the
This looks pretty straigtforward and simple to me. Would it work for you? |
That for sure would work. I just wonder why I can use
but not
It just feels weird that I have to make an exception here and use Maybe |
Flow for events is a complicated story, indeed. There was a big discussion in #1082 and one outcome for that is that we might introduce some kind of |
Here is a design for |
It looks like it is now safe to close this issue, as its use-cases are fully accounted for by #2034. |
I propose to add a
Flow
operation that broadcasts upstream values downstream without involving aBroadcastChannel
and thus suspending the emitter until the broadcast for a value is complete.Status quo on suspending emitters
emit()
ing a value to aFlow
suspends the emitter until the collector has returned from the collection lambda for that value.Flow
values using.broadcastIn(scope).asFlow()
the emitter is no longer suspended until the collector(s) have returned from the collection lambda for that value.emit()
a value to multiple collectors where the emitter remains suspended until all collectors have processed that value.Example output
(1) Emitting two values [1, 2] to one collector looks like this:
(2) Emitting two values [1, 2] to multiple collectors through a
BroadcastChannel
looks like this:(3) The desired behavior is that two values [1, 2] emitted to multiple collectors looks like this:
Use case
My use case for (3) are events where the event emitter has to wait for all event subscribers to fully process the event.
Example
AuthenticationManager
has aFlow<WipeDataEvent>
.WipeDataEvent
is emitted by theAuthenticationManager
all subscribers must wipe the data they have stored.AuthenticationManager
can continue with its work, e.g. the sign-out/sign-in process..broadcast()
example for (3)The following code is very naive. It merely illustrates how the behavior in the example output for (3) can be achieved.
.broadcast()
..broadcastIn(scope).asFlow()
but not involve a channel.A fully working example which creates all of the output mentioned above can be found here:
https://gist.github.com/fluidsonic/f7b2b0084f184932ea3be4cf0074496a
The text was updated successfully, but these errors were encountered: