| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart.async; | 5 part of dart.async; |
| 6 | 6 |
| 7 /** Abstract and private interface for a place to put events. */ | 7 /** Abstract and private interface for a place to put events. */ |
| 8 abstract class _EventSink<T> { | 8 abstract class _EventSink<T> { |
| 9 void _add(T data); | 9 void _add(T data); |
| 10 void _addError(Object error, StackTrace stackTrace); | 10 void _addError(Object error, StackTrace stackTrace); |
| (...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 // An async event has been scheduled to run a function. | 572 // An async event has been scheduled to run a function. |
| 573 static const int _STATE_SCHEDULED = 1; | 573 static const int _STATE_SCHEDULED = 1; |
| 574 // An async event has been scheduled, but it will do nothing when it runs. | 574 // An async event has been scheduled, but it will do nothing when it runs. |
| 575 // Async events can't be preempted. | 575 // Async events can't be preempted. |
| 576 static const int _STATE_CANCELED = 3; | 576 static const int _STATE_CANCELED = 3; |
| 577 | 577 |
| 578 /** | 578 /** |
| 579 * State of being scheduled. | 579 * State of being scheduled. |
| 580 * | 580 * |
| 581 * Set to [_STATE_SCHEDULED] when pending events are scheduled for | 581 * Set to [_STATE_SCHEDULED] when pending events are scheduled for |
| 582 * async dispatch. Since we can't cancel a [runAsync] call, if schduling | 582 * async dispatch. Since we can't cancel a [scheduleMicrotask] call, if |
| 583 * is "canceled", the _state is simply set to [_STATE_CANCELED] which will | 583 * scheduling is "canceled", the _state is simply set to [_STATE_CANCELED] |
| 584 * make the async code do nothing except resetting [_state]. | 584 * which will make the async code do nothing except resetting [_state]. |
| 585 * | 585 * |
| 586 * If events are scheduled while the state is [_STATE_CANCELED], it is | 586 * If events are scheduled while the state is [_STATE_CANCELED], it is |
| 587 * merely switched back to [_STATE_SCHEDULED], but no new call to [runAsync] | 587 * merely switched back to [_STATE_SCHEDULED], but no new call to |
| 588 * is performed. | 588 * [scheduleMicrotask] is performed. |
| 589 */ | 589 */ |
| 590 int _state = _STATE_UNSCHEDULED; | 590 int _state = _STATE_UNSCHEDULED; |
| 591 | 591 |
| 592 bool get isEmpty; | 592 bool get isEmpty; |
| 593 | 593 |
| 594 bool get isScheduled => _state == _STATE_SCHEDULED; | 594 bool get isScheduled => _state == _STATE_SCHEDULED; |
| 595 bool get _eventScheduled => _state >= _STATE_SCHEDULED; | 595 bool get _eventScheduled => _state >= _STATE_SCHEDULED; |
| 596 | 596 |
| 597 /** | 597 /** |
| 598 * Schedule an event to run later. | 598 * Schedule an event to run later. |
| 599 * | 599 * |
| 600 * If called more than once, it should be called with the same dispatch as | 600 * If called more than once, it should be called with the same dispatch as |
| 601 * argument each time. It may reuse an earlier argument in some cases. | 601 * argument each time. It may reuse an earlier argument in some cases. |
| 602 */ | 602 */ |
| 603 void schedule(_EventDispatch dispatch) { | 603 void schedule(_EventDispatch dispatch) { |
| 604 if (isScheduled) return; | 604 if (isScheduled) return; |
| 605 assert(!isEmpty); | 605 assert(!isEmpty); |
| 606 if (_eventScheduled) { | 606 if (_eventScheduled) { |
| 607 assert(_state == _STATE_CANCELED); | 607 assert(_state == _STATE_CANCELED); |
| 608 _state = _STATE_SCHEDULED; | 608 _state = _STATE_SCHEDULED; |
| 609 return; | 609 return; |
| 610 } | 610 } |
| 611 runAsync(() { | 611 scheduleMicrotask(() { |
| 612 int oldState = _state; | 612 int oldState = _state; |
| 613 _state = _STATE_UNSCHEDULED; | 613 _state = _STATE_UNSCHEDULED; |
| 614 if (oldState == _STATE_CANCELED) return; | 614 if (oldState == _STATE_CANCELED) return; |
| 615 handleNext(dispatch); | 615 handleNext(dispatch); |
| 616 }); | 616 }); |
| 617 _state = _STATE_SCHEDULED; | 617 _state = _STATE_SCHEDULED; |
| 618 } | 618 } |
| 619 | 619 |
| 620 void cancelSchedule() { | 620 void cancelSchedule() { |
| 621 if (isScheduled) _state = _STATE_CANCELED; | 621 if (isScheduled) _state = _STATE_CANCELED; |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 981 _Future<bool> hasNext = _futureOrPrefetch; | 981 _Future<bool> hasNext = _futureOrPrefetch; |
| 982 _clear(); | 982 _clear(); |
| 983 hasNext._complete(false); | 983 hasNext._complete(false); |
| 984 return; | 984 return; |
| 985 } | 985 } |
| 986 _subscription.pause(); | 986 _subscription.pause(); |
| 987 _futureOrPrefetch = null; | 987 _futureOrPrefetch = null; |
| 988 _state = _STATE_EXTRA_DONE; | 988 _state = _STATE_EXTRA_DONE; |
| 989 } | 989 } |
| 990 } | 990 } |
| OLD | NEW |