| 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 // ------------------------------------------------------------------- | 7 // ------------------------------------------------------------------- |
| 8 // Controller for creating and adding events to a stream. | 8 // Controller for creating and adding events to a stream. |
| 9 // ------------------------------------------------------------------- | 9 // ------------------------------------------------------------------- |
| 10 | 10 |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 * If [cancelOnError] is true, only the first error on [source] is | 189 * If [cancelOnError] is true, only the first error on [source] is |
| 190 * forwarded to the controller's stream, and the `addStream` ends | 190 * forwarded to the controller's stream, and the `addStream` ends |
| 191 * after this. If [cancelOnError] is false, all errors are forwarded | 191 * after this. If [cancelOnError] is false, all errors are forwarded |
| 192 * and only a done event will end the `addStream`. | 192 * and only a done event will end the `addStream`. |
| 193 */ | 193 */ |
| 194 Future addStream(Stream<T> source, {bool cancelOnError: true}); | 194 Future addStream(Stream<T> source, {bool cancelOnError: true}); |
| 195 } | 195 } |
| 196 | 196 |
| 197 | 197 |
| 198 abstract class _StreamControllerLifecycle<T> { | 198 abstract class _StreamControllerLifecycle<T> { |
| 199 StreamSubscription<T> _subscribe(bool cancelOnError); | 199 StreamSubscription<T> _subscribe( |
| 200 void onData(T data), |
| 201 Function onError, |
| 202 void onDone(), |
| 203 bool cancelOnError); |
| 200 void _recordPause(StreamSubscription<T> subscription) {} | 204 void _recordPause(StreamSubscription<T> subscription) {} |
| 201 void _recordResume(StreamSubscription<T> subscription) {} | 205 void _recordResume(StreamSubscription<T> subscription) {} |
| 202 Future _recordCancel(StreamSubscription<T> subscription) => null; | 206 Future _recordCancel(StreamSubscription<T> subscription) => null; |
| 203 } | 207 } |
| 204 | 208 |
| 205 /** | 209 /** |
| 206 * Default implementation of [StreamController]. | 210 * Default implementation of [StreamController]. |
| 207 * | 211 * |
| 208 * Controls a stream that only supports a single controller. | 212 * Controls a stream that only supports a single controller. |
| 209 */ | 213 */ |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 // End of addStream stream. | 468 // End of addStream stream. |
| 465 assert(_isAddingStream); | 469 assert(_isAddingStream); |
| 466 _StreamControllerAddStreamState addState = _varData; | 470 _StreamControllerAddStreamState addState = _varData; |
| 467 _varData = addState.varData; | 471 _varData = addState.varData; |
| 468 _state &= ~_STATE_ADDSTREAM; | 472 _state &= ~_STATE_ADDSTREAM; |
| 469 addState.complete(); | 473 addState.complete(); |
| 470 } | 474 } |
| 471 | 475 |
| 472 // _StreamControllerLifeCycle interface | 476 // _StreamControllerLifeCycle interface |
| 473 | 477 |
| 474 StreamSubscription<T> _subscribe(bool cancelOnError) { | 478 StreamSubscription<T> _subscribe( |
| 479 void onData(T data), |
| 480 Function onError, |
| 481 void onDone(), |
| 482 bool cancelOnError) { |
| 475 if (!_isInitialState) { | 483 if (!_isInitialState) { |
| 476 throw new StateError("Stream has already been listened to."); | 484 throw new StateError("Stream has already been listened to."); |
| 477 } | 485 } |
| 478 _ControllerSubscription subscription = | 486 _ControllerSubscription subscription = |
| 479 new _ControllerSubscription(this, cancelOnError); | 487 new _ControllerSubscription(this, onData, onError, onDone, |
| 488 cancelOnError); |
| 480 | 489 |
| 481 _PendingEvents pendingEvents = _pendingEvents; | 490 _PendingEvents pendingEvents = _pendingEvents; |
| 482 _state |= _STATE_SUBSCRIBED; | 491 _state |= _STATE_SUBSCRIBED; |
| 483 if (_isAddingStream) { | 492 if (_isAddingStream) { |
| 484 _StreamControllerAddStreamState addState = _varData; | 493 _StreamControllerAddStreamState addState = _varData; |
| 485 addState.varData = subscription; | 494 addState.varData = subscription; |
| 486 addState.resume(); | 495 addState.resume(); |
| 487 } else { | 496 } else { |
| 488 _varData = subscription; | 497 _varData = subscription; |
| 489 } | 498 } |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 } catch (e, s) { | 655 } catch (e, s) { |
| 647 Zone.current.handleUncaughtError(e, s); | 656 Zone.current.handleUncaughtError(e, s); |
| 648 } | 657 } |
| 649 } | 658 } |
| 650 | 659 |
| 651 class _ControllerStream<T> extends _StreamImpl<T> { | 660 class _ControllerStream<T> extends _StreamImpl<T> { |
| 652 _StreamControllerLifecycle<T> _controller; | 661 _StreamControllerLifecycle<T> _controller; |
| 653 | 662 |
| 654 _ControllerStream(this._controller); | 663 _ControllerStream(this._controller); |
| 655 | 664 |
| 656 StreamSubscription<T> _createSubscription(bool cancelOnError) => | 665 StreamSubscription<T> _createSubscription( |
| 657 _controller._subscribe(cancelOnError); | 666 void onData(T data), |
| 667 Function onError, |
| 668 void onDone(), |
| 669 bool cancelOnError) => |
| 670 _controller._subscribe(onData, onError, onDone, cancelOnError); |
| 658 | 671 |
| 659 // Override == and hashCode so that new streams returned by the same | 672 // Override == and hashCode so that new streams returned by the same |
| 660 // controller are considered equal. The controller returns a new stream | 673 // controller are considered equal. The controller returns a new stream |
| 661 // each time it's queried, but doesn't have to cache the result. | 674 // each time it's queried, but doesn't have to cache the result. |
| 662 | 675 |
| 663 int get hashCode => _controller.hashCode ^ 0x35323532; | 676 int get hashCode => _controller.hashCode ^ 0x35323532; |
| 664 | 677 |
| 665 bool operator==(Object other) { | 678 bool operator==(Object other) { |
| 666 if (identical(this, other)) return true; | 679 if (identical(this, other)) return true; |
| 667 if (other is! _ControllerStream) return false; | 680 if (other is! _ControllerStream) return false; |
| 668 _ControllerStream otherStream = other; | 681 _ControllerStream otherStream = other; |
| 669 return identical(otherStream._controller, this._controller); | 682 return identical(otherStream._controller, this._controller); |
| 670 } | 683 } |
| 671 } | 684 } |
| 672 | 685 |
| 673 class _ControllerSubscription<T> extends _BufferingStreamSubscription<T> { | 686 class _ControllerSubscription<T> extends _BufferingStreamSubscription<T> { |
| 674 final _StreamControllerLifecycle<T> _controller; | 687 final _StreamControllerLifecycle<T> _controller; |
| 675 | 688 |
| 676 _ControllerSubscription(this._controller, bool cancelOnError) | 689 _ControllerSubscription(this._controller, void onData(T data), |
| 677 : super(cancelOnError); | 690 Function onError, void onDone(), bool cancelOnError) |
| 691 : super(onData, onError, onDone, cancelOnError); |
| 678 | 692 |
| 679 Future _onCancel() { | 693 Future _onCancel() { |
| 680 return _controller._recordCancel(this); | 694 return _controller._recordCancel(this); |
| 681 } | 695 } |
| 682 | 696 |
| 683 void _onPause() { | 697 void _onPause() { |
| 684 _controller._recordPause(this); | 698 _controller._recordPause(this); |
| 685 } | 699 } |
| 686 | 700 |
| 687 void _onResume() { | 701 void _onResume() { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 _StreamControllerAddStreamState(_StreamController controller, | 782 _StreamControllerAddStreamState(_StreamController controller, |
| 769 this.varData, | 783 this.varData, |
| 770 Stream source, | 784 Stream source, |
| 771 bool cancelOnError) | 785 bool cancelOnError) |
| 772 : super(controller, source, cancelOnError) { | 786 : super(controller, source, cancelOnError) { |
| 773 if (controller.isPaused) { | 787 if (controller.isPaused) { |
| 774 addSubscription.pause(); | 788 addSubscription.pause(); |
| 775 } | 789 } |
| 776 } | 790 } |
| 777 } | 791 } |
| OLD | NEW |