| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library _js_helper; | 5 library _js_helper; |
| 6 | 6 |
| 7 import 'dart:_js_embedded_names' show | 7 import 'dart:_js_embedded_names' show |
| 8 GET_TYPE_FROM_NAME, | 8 GET_TYPE_FROM_NAME, |
| 9 GET_ISOLATE_TAG, | 9 GET_ISOLATE_TAG, |
| 10 INTERCEPTED_NAMES, | 10 INTERCEPTED_NAMES, |
| (...skipping 3532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3543 /// | 3543 /// |
| 3544 /// If the async* function wants to do an await it calls this function with | 3544 /// If the async* function wants to do an await it calls this function with |
| 3545 /// [object] not and [IterationMarker]. | 3545 /// [object] not and [IterationMarker]. |
| 3546 /// | 3546 /// |
| 3547 /// If [object] is not a [Future], it is wrapped in a `Future.value`. | 3547 /// If [object] is not a [Future], it is wrapped in a `Future.value`. |
| 3548 /// The [helperCallback] is called on successfull completion of the | 3548 /// The [helperCallback] is called on successfull completion of the |
| 3549 /// future. | 3549 /// future. |
| 3550 /// | 3550 /// |
| 3551 /// If [helperCallback] or [errorCallback] throws the error is added to the | 3551 /// If [helperCallback] or [errorCallback] throws the error is added to the |
| 3552 /// stream. | 3552 /// stream. |
| 3553 dynamic streamHelper(dynamic object, | 3553 void streamHelper(dynamic object, |
| 3554 dynamic /* js function */ helperCallback, | 3554 dynamic /* js function */ helperCallback, |
| 3555 AsyncStarStreamController controller, | 3555 AsyncStarStreamController controller, |
| 3556 dynamic /* js function */ errorCallback) { | 3556 dynamic /* js function */ errorCallback) { |
| 3557 if (helperCallback == null) { | 3557 if (helperCallback == null) { |
| 3558 // This happens on return from the async* function. | 3558 // This happens on return from the async* function. |
| 3559 controller.close(); | 3559 controller.close(); |
| 3560 return null; | 3560 return; |
| 3561 } | 3561 } |
| 3562 | 3562 |
| 3563 if (object is IterationMarker) { | 3563 if (object is IterationMarker) { |
| 3564 if (controller.stopRunning) { | 3564 if (controller.stopRunning) { |
| 3565 _wrapJsFunctionForStream(errorCallback, controller)(); | 3565 _wrapJsFunctionForStream(errorCallback, controller)(); |
| 3566 return null; | 3566 return; |
| 3567 } | 3567 } |
| 3568 if (object.state == IterationMarker.YIELD_SINGLE) { | 3568 if (object.state == IterationMarker.YIELD_SINGLE) { |
| 3569 controller.add(object.value); | 3569 controller.add(object.value); |
| 3570 // If the controller is paused we stop producing more values. | 3570 // If the controller is paused we stop producing more values. |
| 3571 if (controller.isPaused) { | 3571 if (controller.isPaused) { |
| 3572 return null; | 3572 return; |
| 3573 } | 3573 } |
| 3574 // TODO(sigurdm): We should not suspend here according to the spec. | 3574 // TODO(sigurdm): We should not suspend here according to the spec. |
| 3575 scheduleMicrotask(() { | 3575 scheduleMicrotask(() { |
| 3576 _wrapJsFunctionForStream(helperCallback, controller)(null); | 3576 _wrapJsFunctionForStream(helperCallback, controller)(null); |
| 3577 }); | 3577 }); |
| 3578 return; | 3578 return; |
| 3579 } else if (object.state == IterationMarker.YIELD_STAR) { | 3579 } else if (object.state == IterationMarker.YIELD_STAR) { |
| 3580 Stream stream = object.value; | 3580 Stream stream = object.value; |
| 3581 controller.isAdding = true; | 3581 controller.isAdding = true; |
| 3582 // Errors of [stream] are passed though to the main stream. (see | 3582 // Errors of [stream] are passed though to the main stream. (see |
| 3583 // [AsyncStreamController.addStream]. | 3583 // [AsyncStreamController.addStream]. |
| 3584 // TODO(sigurdm): The spec is not very clear here. Clarify with Gilad. | 3584 // TODO(sigurdm): The spec is not very clear here. Clarify with Gilad. |
| 3585 controller.addStream(stream).then((_) { | 3585 controller.addStream(stream).then((_) { |
| 3586 controller.isAdding = false; | 3586 controller.isAdding = false; |
| 3587 _wrapJsFunctionForStream(helperCallback, controller)(null); | 3587 _wrapJsFunctionForStream(helperCallback, controller)(null); |
| 3588 }); | 3588 }); |
| 3589 return null; | 3589 return; |
| 3590 } | 3590 } |
| 3591 } | 3591 } |
| 3592 | 3592 |
| 3593 Future future = object is Future ? object : new Future.value(object); | 3593 Future future = object is Future ? object : new Future.value(object); |
| 3594 future.then(_wrapJsFunctionForStream(helperCallback, controller), | 3594 future.then(_wrapJsFunctionForStream(helperCallback, controller), |
| 3595 onError: errorCallback == null | 3595 onError: errorCallback == null |
| 3596 ? null | 3596 ? null |
| 3597 : _wrapJsFunctionForStream(errorCallback, controller)); | 3597 : _wrapJsFunctionForStream(errorCallback, controller)); |
| 3598 } |
| 3599 |
| 3600 Stream streamOfController(AsyncStarStreamController controller) { |
| 3598 return controller.stream; | 3601 return controller.stream; |
| 3599 } | 3602 } |
| 3600 | 3603 |
| 3601 /// A wrapper around a [StreamController] that remembers if that controller | 3604 /// A wrapper around a [StreamController] that remembers if that controller |
| 3602 /// got a cancel. | 3605 /// got a cancel. |
| 3603 /// | 3606 /// |
| 3604 /// Also has a subSubscription that when not null will provide events for the | 3607 /// Also has a subSubscription that when not null will provide events for the |
| 3605 /// stream, and will be paused and resumed along with this controller. | 3608 /// stream, and will be paused and resumed along with this controller. |
| 3606 class AsyncStarStreamController { | 3609 class AsyncStarStreamController { |
| 3607 StreamController controller; | 3610 StreamController controller; |
| 3608 Stream get stream => controller.stream; | 3611 Stream get stream => controller.stream; |
| 3609 bool stopRunning = false; | 3612 bool stopRunning = false; |
| 3610 bool isAdding = false; | 3613 bool isAdding = false; |
| 3611 bool get isPaused => controller.isPaused; | 3614 bool get isPaused => controller.isPaused; |
| 3612 add(event) => controller.add(event); | 3615 add(event) => controller.add(event); |
| 3613 addStream(Stream stream) { | 3616 addStream(Stream stream) { |
| 3614 return controller.addStream(stream, cancelOnError: false); | 3617 return controller.addStream(stream, cancelOnError: false); |
| 3615 } | 3618 } |
| 3616 addError(error, stackTrace) => controller.addError(error, stackTrace); | 3619 addError(error, stackTrace) => controller.addError(error, stackTrace); |
| 3617 close() => controller.close(); | 3620 close() => controller.close(); |
| 3618 | 3621 |
| 3619 AsyncStarStreamController(helperCallback) { | 3622 AsyncStarStreamController(helperCallback) { |
| 3620 controller = new StreamController( | 3623 controller = new StreamController( |
| 3624 onListen: () { |
| 3625 scheduleMicrotask(() => JS('', '#(null)', helperCallback)); |
| 3626 }, |
| 3621 onResume: () { | 3627 onResume: () { |
| 3622 if (!isAdding) { | 3628 if (!isAdding) { |
| 3623 streamHelper(null, helperCallback, this, null); | 3629 streamHelper(null, helperCallback, this, null); |
| 3624 } | 3630 } |
| 3625 }, onCancel: () { | 3631 }, onCancel: () { |
| 3626 stopRunning = true; | 3632 stopRunning = true; |
| 3627 }); | 3633 }); |
| 3628 } | 3634 } |
| 3629 } | 3635 } |
| 3630 | 3636 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3714 // This is a function that will return a helper function that does the | 3720 // This is a function that will return a helper function that does the |
| 3715 // iteration of the sync*. | 3721 // iteration of the sync*. |
| 3716 // | 3722 // |
| 3717 // Each invocation should give a helper with fresh state. | 3723 // Each invocation should give a helper with fresh state. |
| 3718 final dynamic /* js function */ _outerHelper; | 3724 final dynamic /* js function */ _outerHelper; |
| 3719 | 3725 |
| 3720 SyncStarIterable(this._outerHelper); | 3726 SyncStarIterable(this._outerHelper); |
| 3721 | 3727 |
| 3722 Iterator get iterator => new SyncStarIterator(JS('', '#()', _outerHelper)); | 3728 Iterator get iterator => new SyncStarIterator(JS('', '#()', _outerHelper)); |
| 3723 } | 3729 } |
| OLD | NEW |