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

Broadcast StreamGroup doesn't listen to later added streams after _onCancelBroadcast is called #344

Closed
anhtuan23 opened this issue Jun 13, 2021 · 0 comments · Fixed by dart-archive/async#184

Comments

@anhtuan23
Copy link
Contributor

anhtuan23 commented Jun 13, 2021

Hi. I'm getting a problem with broadcast StreamGroup.

Specifically, when adding a new stream after all previous subscriptions to a broadcast StreamGroup have been canceled, the StreamGroup won't listen to the newly added stream.

Here's how to reproduce this problem:

import 'package:async/async.dart';

void main() async {
  final streamGroup = StreamGroup<String>.broadcast();

  Stream<String> getFirstStream() async* {
    var i = 0;
    while (true) {
      await Future.delayed(Duration(seconds: 1));
      yield '1st stream: ${i++}';
    }
  }

  // Add the first single subscription stream
  await streamGroup.add(getFirstStream());

  final firstSubs = streamGroup.stream.listen((event) {
    print(event);
  });

  await Future.delayed(Duration(seconds: 3));
  // * Cancel the only subscription to the StreamGroup, putting it into dormant
  await firstSubs.cancel();

  Stream<String> getSecondStream() async* {
    var i = 0;
    while (true) {
      await Future.delayed(Duration(seconds: 1));
      yield '2nd stream: ${i++}';
    }
  }

  // Add another single subscription stream
  await streamGroup.add(getSecondStream());

  // * Start to listen to the StreamGroup again, which should emits events from both the 1st stream and the 2nd stream
  streamGroup.stream.listen((event) {
    print(event);
  });
}

Expected behavior:

Events from both the 1st stream and the 2nd stream are emitted by the StreamGroup.

Actual behavior:

Only Events from the 1st stream are emitted. The 2nd stream is ignored.

Proposal:

I believe the problem is in this line
https://github.com/dart-lang/async/blob/f1c8882c927af68df6786124c94e857de0fa22c1/lib/src/stream_group.dart#L195
Here, since the 1st stream is already being listened and have value != null, the function returns without listening to subsequent streams.

Therefore, it should be continue instead of return.

Thank you guys for this very helpful package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants