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

MultiStreamController.onListen is never called? #56925

Open
sgrekhov opened this issue Oct 21, 2024 · 4 comments
Open

MultiStreamController.onListen is never called? #56925

sgrekhov opened this issue Oct 21, 2024 · 4 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-async type-documentation A request to add or improve documentation

Comments

@sgrekhov
Copy link
Contributor

sgrekhov commented Oct 21, 2024

import "dart:async";
import "../../../Utils/expect.dart";

main() {
  asyncStart(2);
  var stream = Stream<int>.multi((controller) {
    controller.onListen = () {
      print("onListen"); // Not printed
      asyncEnd();
    };
    controller.add(1);
    controller.add(2);
    controller.add(3);
    controller.close();
  });
  listen(stream);
  listen(stream);
}

void listen(Stream<int> stream) {
  int i = 0;
  stream.listen((v) {
    Expect.equals(++i, v);
  }, onDone: () {
    Expect.equals(3, i);
  });
}

According to the documentation "The callback which is called when the stream is listened to.". Why it is not called in the code above?

cc @lrhn

Dart SDK version: 3.7.0-27.0.dev (dev) (Tue Oct 15 17:02:51 2024 -0700) on "windows_x64"

@dart-github-bot
Copy link
Collaborator

Summary: The user is reporting that the onListen callback on a MultiStreamController is not being called when the stream is listened to, despite the documentation stating it should be. The provided code demonstrates the issue with two listeners on the same stream.

@dart-github-bot dart-github-bot added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-question A question about expected behavior or functionality labels Oct 21, 2024
@lrhn
Copy link
Member

lrhn commented Oct 21, 2024

This is documented on MultiStreamController.
The Stream.multi constructor's onListen parameter is called when the stream is listened to, and the MultiStreamController is created at that point, as a controller for that particular steam subscription.
Calling listen on the stream again creates a new controller.

Because of that, setting the onListen on the MultiStreamController has no effect, the one subscription that the controller applies to has already started listening.

Or, as an alternative view, the onListen argument is set as the onListen of a new MultiStreamController every time you listen to the Stream.multi stream, then that controller's stream is listened to instead, and the original stream listen returns that controller.

The code there is then, wrt. onListen, equivalent to a single subscription stream controller which sets onListen again inside its onListen function. The second value will never be called.

@lrhn lrhn removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Oct 21, 2024
@sgrekhov
Copy link
Contributor Author

Thank you! This is documented not on MultiStreamController but on Stream.multi. IMHO it makes sense to document it on MultiStreamController and onListen as well.

@lrhn
Copy link
Member

lrhn commented Oct 22, 2024

ACK. More documentation is better.

@lrhn lrhn added type-documentation A request to add or improve documentation and removed type-question A question about expected behavior or functionality labels Oct 22, 2024
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. library-async type-documentation A request to add or improve documentation
Projects
None yet
Development

No branches or pull requests

3 participants