dart-lang / dart-lang/language
`async*` methods should start synchronously when their streams are listened to.
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
The `async` functions start synchronously in Dart 2. So should the `async*` functions, meaning the initial part of the function should be called immediately when a client `listen`s on the returned stream.
We have not implemented that yet.
The specification says that
> When *s* is listened to, execution of the body of *f* will begin.
(where *s* is the stream returned by calling the `async*` function *f*).
Starting immediately is important. It allows an `async*` function to immediately listen to, say, a broadcast stream immediately when the user asks it to start. Otherwise it might miss events fired between the time of the request and when the `async*` function actually starts.
Example:
```dart
class Stream {
...
Stream takeWhile(bool test(T value)) async* {
await for (var value in this) {
if (!test(value)) break;
yield value;
}
}
...
}
```
This implementation of `takeWhile` is currently not viable because the `async*` function doesn't start immediately.
(You still can't send events synchronously during the `onListen` callback, so this would not allow you to deliver events before the subscription has been returned by the `listen` call).
Contributor guide
Research direction
Start with the async* function and Stream.listen behavior described in the issue, using the takeWhile example as the concrete case. Check the language specification rule that execution begins when the stream is listened to, then identify the relevant async* implementation and tests. Done means the function body starts immediately on listen without allowing synchronous event delivery during onListen.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100