Skip to content
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

Stream.asBroadcastStream() is broken semantically #11289

Closed
alxhub opened this issue Jun 14, 2013 · 7 comments
Closed

Stream.asBroadcastStream() is broken semantically #11289

alxhub opened this issue Jun 14, 2013 · 7 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug

Comments

@alxhub
Copy link

alxhub commented Jun 14, 2013

The semantics of Stream.asBroadcastStream() result in something that is deeply counterintuitive.

A broadcast stream is supposed to support any number of listeners. This is indeed true of Stream.asBroadcastStream() - it can be listened to any number of times.

However, if for some reason its listener count ever falls to 0, it will automatically unsubscribe from its backing single-subscriber stream, rendering it forevermore useless.

This results in bugs in real-world code and hacks like calling .drain() on the result of .asBroadcastStream() to add a permanent listener to hold the stream open. This behavior is also substantially different from new StreamController.broadcast() which behaves properly in this case.

@madsager
Copy link
Contributor

Added Area-Library, Triaged labels.

@floitschG
Copy link
Contributor

Stream.asBroadcastStream() is basically implemented as
new StreamController.broadcast(onListen: subscribeToPredecessor, onCancel: cancel);

We will add named onList and onCancel arguments on the asBroadcastStream method that could (partially) override the default behavior. By replacing the onCancel method one could then avoid the cancel and keep the stream open.


Removed Type-Defect label.
Added Type-Enhancement, Ready-to-implement, Accepted labels.

@floitschG
Copy link
Contributor

More details:
asBroadCast({ void onListen(StreamSubscription subscription), void onCancel(StreamSubscription subscription) });

The subscription will be a wrapped version of the actual subscription. It will not allow to change onData, onError and onDone.

@alxhub
Copy link
Author

alxhub commented Jun 18, 2013

Will the default onCancel cancel the underlying subscription? I think this is a bad idea. asBroadcastStream() should not have surprise behavior where it "dies" if it ever temporarily loses all of its listeners. If someone wants that behavior, they should specify it in their onCancel explicitly.

What about: asBroadcastStream({persistWithoutListeners: true});

or some well-named parameter?

@lrhn
Copy link
Member

lrhn commented Jun 20, 2013

The default onCancel will no longer cancel the original subscription.
We did this because there was no way for the user to signal that it didn't need the stream any more, so we picked the one signal that was there.

With the onCancel callback available, we can let the user decide when to cancel, and we don't need a hacky heuristic any more.

That does mean that some existing uses of asBroadcastStream will now not close the source stream, but wait for it to run dry. It's likely that it wasn't a deliberate choice before, so we don't think it will be a problem. Many uses of asBroadcastStream were on a StreamController's strem, and they are better rewritten as using a broadcast StreamController directly.


Added Started label.

@szakarias
Copy link
Contributor

Set owner to @lrhn.

@lrhn
Copy link
Member

lrhn commented Jul 8, 2013

Added Fixed label.

@alxhub alxhub added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Jul 8, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants