Chromium Code Reviews| Index: runtime/lib/core_patch.dart |
| =================================================================== |
| --- runtime/lib/core_patch.dart (revision 43971) |
| +++ runtime/lib/core_patch.dart (working copy) |
| @@ -22,11 +22,88 @@ |
| String toString() => _enum_names[index]; |
| } |
| -typedef bool SyncGeneratorCallback(Iterator iterator); |
| +// _AsyncStarStreamController is used by the compiler to implement |
| +// async* generator functions. |
| +class _AsyncStarStreamController { |
| + StreamController controller; |
| + Function asyncStarBody; |
| + bool isCancelled = false; |
|
Lasse Reichstein Nielsen
2015/02/24 13:12:34
"isCanceled"
I've recently been informed that one
hausner
2015/02/25 22:07:41
I'm using controller.hasListener now. Apparently t
|
| + bool isAdding = false; |
| + |
| + Stream get stream => controller.stream; |
| + |
| + // Adds element to steam, returns true iff the caller should suspend |
| + // execution of the generator. |
| + bool add(event) { |
| + // TODO(hausner): what is the expected behavior if the stream |
| + // has been cancelled? |
| + if (isCancelled) return true; |
|
Lasse Reichstein Nielsen
2015/02/24 13:12:34
Since you are using a single-subscription controll
hausner
2015/02/25 22:07:41
Done.
|
| + controller.add(event); |
| + if (true) { |
|
Lasse Reichstein Nielsen
2015/02/24 13:12:34
if (true) ?
hausner
2015/02/25 22:07:41
That was a way to have the dart2js and the spec-co
|
| + // Always suspend. Matches dart2js implementation but not Spec. |
| + scheduleMicrotask(asyncStarBody); |
|
Lasse Reichstein Nielsen
2015/02/24 13:12:34
If the stream subscription is paused, you should n
hausner
2015/02/25 22:07:41
Done.
|
| + return true; |
| + } |
| + // Tell caller to suspend the generator if the controller is paused. |
| + return controller.isPaused; |
| + } |
| + |
| + // Adds stream, tells caller to suspend execution of the generator |
| + // function. The generator will be scheduled again when all of the |
| + // elements of the added stream have been consumed. |
| + bool addStream(Stream stream) { |
| + // TODO(hausner): what is the expected behavior if the stream |
| + // has been cancelled? |
|
Lasse Reichstein Nielsen
2015/02/24 13:12:34
If controller.stream has been canceled or if the a
hausner
2015/02/25 22:07:41
Done.
|
| + if (isCancelled) return true; |
| + isAdding = true; |
| + var whenDoneAdding = |
| + controller.addStream(stream as Stream, cancelOnError: false); |
| + whenDoneAdding.then((_) { |
| + isAdding = false; |
| + if (!controller.isPaused) { |
| + scheduleMicrotask(asyncStarBody); |
| + } |
| + }); |
| + return true; |
| + } |
| + |
| + addError(error, stackTrace) { |
| + controller.addError(error, stackTrace); |
| + } |
| + |
| + close() { |
| + controller.close(); |
|
Lasse Reichstein Nielsen
2015/02/24 13:12:34
If the subscription was paused, you will need to s
hausner
2015/02/25 22:07:41
Done. One thing that is still sup-optimal: the con
|
| + } |
| + |
| + _AsyncStarStreamController(this.asyncStarBody) { |
| + controller = new StreamController(onListen: this.onListen, |
| + onResume: this.onResume, |
| + onCancel: this.onCancel); |
| + } |
| + |
| + onListen() { |
| + scheduleMicrotask(asyncStarBody); |
| + } |
| + |
| + onResume() { |
| + if (!isAdding) { |
| + scheduleMicrotask(asyncStarBody); |
|
Lasse Reichstein Nielsen
2015/02/24 13:12:34
You can get two onResumes in a row if someone does
hausner
2015/02/25 22:07:41
Acknowledged.
|
| + } |
| + } |
| + |
| + onCancel() { |
| + isCancelled = true; |
|
Lasse Reichstein Nielsen
2015/02/24 13:12:34
If the subscription was paused, you will need to s
hausner
2015/02/25 22:07:41
See comment above.
|
| + } |
| +} |
| + |
| + |
| // _SyncIterable and _syncIterator are used by the compiler to |
| // implement sync* generator functions. A sync* generator allocates |
| // and returns a new _SyncIterable object. |
| + |
| +typedef bool SyncGeneratorCallback(Iterator iterator); |
| + |
| class _SyncIterable extends IterableBase { |
| // moveNextFn is the closurized body of the generator function. |
| final SyncGeneratorCallback moveNextFn; |