| 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 // States shared by single/multi stream implementations. | 7 // States shared by single/multi stream implementations. |
| 8 | 8 |
| 9 /// Initial and default state where the stream can receive and send events. | 9 /// Initial and default state where the stream can receive and send events. |
| 10 const int _STREAM_OPEN = 0; | 10 const int _STREAM_OPEN = 0; |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 | 209 |
| 210 /** | 210 /** |
| 211 * Record that a listener wants a pause from events. | 211 * Record that a listener wants a pause from events. |
| 212 * | 212 * |
| 213 * This methods is called from [_StreamListener.pause()]. | 213 * This methods is called from [_StreamListener.pause()]. |
| 214 * Subclasses can override this method, along with [isPaused] and | 214 * Subclasses can override this method, along with [isPaused] and |
| 215 * [createSubscription], if they want to do a different handling of paused | 215 * [createSubscription], if they want to do a different handling of paused |
| 216 * subscriptions, e.g., a filtering stream pausing its own source if all its | 216 * subscriptions, e.g., a filtering stream pausing its own source if all its |
| 217 * subscribers are paused. | 217 * subscribers are paused. |
| 218 */ | 218 */ |
| 219 void _pause(_StreamListener<T> listener, Signal resumeSignal) { | 219 void _pause(_StreamListener<T> listener, Future resumeSignal) { |
| 220 assert(identical(listener._source, this)); | 220 assert(identical(listener._source, this)); |
| 221 if (!listener._isSubscribed) { | 221 if (!listener._isSubscribed) { |
| 222 throw new StateError("Subscription has been canceled."); | 222 throw new StateError("Subscription has been canceled."); |
| 223 } | 223 } |
| 224 assert(!_isComplete); // There can be no subscribers when complete. | 224 assert(!_isComplete); // There can be no subscribers when complete. |
| 225 bool wasPaused = _isPaused; | 225 bool wasPaused = _isPaused; |
| 226 _incrementPauseCount(listener); | 226 _incrementPauseCount(listener); |
| 227 if (resumeSignal != null) { | 227 if (resumeSignal != null) { |
| 228 resumeSignal.then(() { this._resume(listener, true); }); | 228 resumeSignal.whenComplete(() { this._resume(listener, true); }); |
| 229 } | 229 } |
| 230 if (!wasPaused) { | 230 if (!wasPaused) { |
| 231 _onPauseStateChange(); | 231 _onPauseStateChange(); |
| 232 } | 232 } |
| 233 } | 233 } |
| 234 | 234 |
| 235 /** Stops pausing due to one request from the given listener. */ | 235 /** Stops pausing due to one request from the given listener. */ |
| 236 void _resume(_StreamListener<T> listener, bool fromEvent) { | 236 void _resume(_StreamListener<T> listener, bool fromEvent) { |
| 237 if (!listener.isPaused) return; | 237 if (!listener.isPaused) return; |
| 238 assert(listener._isSubscribed); | 238 assert(listener._isSubscribed); |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 } | 677 } |
| 678 | 678 |
| 679 void _sendDone() { | 679 void _sendDone() { |
| 680 _onDone(); | 680 _onDone(); |
| 681 } | 681 } |
| 682 | 682 |
| 683 void cancel() { | 683 void cancel() { |
| 684 _source._cancel(this); | 684 _source._cancel(this); |
| 685 } | 685 } |
| 686 | 686 |
| 687 void pause([Signal resumeSignal]) { | 687 void pause([Future resumeSignal]) { |
| 688 _source._pause(this, resumeSignal); | 688 _source._pause(this, resumeSignal); |
| 689 } | 689 } |
| 690 | 690 |
| 691 void resume() { | 691 void resume() { |
| 692 if (!isPaused) { | 692 if (!isPaused) { |
| 693 throw new StateError("Resuming unpaused subscription"); | 693 throw new StateError("Resuming unpaused subscription"); |
| 694 } | 694 } |
| 695 _source._resume(this, false); | 695 _source._resume(this, false); |
| 696 } | 696 } |
| 697 } | 697 } |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 976 } | 976 } |
| 977 | 977 |
| 978 bool get _isComplete => _timer == null && _pauseCount == 0; | 978 bool get _isComplete => _timer == null && _pauseCount == 0; |
| 979 | 979 |
| 980 void onData(void handleAction(T value)) {} | 980 void onData(void handleAction(T value)) {} |
| 981 void onError(void handleError(StateError error)) {} | 981 void onError(void handleError(StateError error)) {} |
| 982 void onDone(void handleDone(T value)) { | 982 void onDone(void handleDone(T value)) { |
| 983 _handler = handleDone; | 983 _handler = handleDone; |
| 984 } | 984 } |
| 985 | 985 |
| 986 void pause([Signal signal]) { | 986 void pause([Future signal]) { |
| 987 if (_isComplete) { | 987 if (_isComplete) { |
| 988 throw new StateError("Subscription has been canceled."); | 988 throw new StateError("Subscription has been canceled."); |
| 989 } | 989 } |
| 990 if (_timer != null) _timer.cancel(); | 990 if (_timer != null) _timer.cancel(); |
| 991 _pauseCount++; | 991 _pauseCount++; |
| 992 } | 992 } |
| 993 | 993 |
| 994 void resume() { | 994 void resume() { |
| 995 if (_isComplete) { | 995 if (_isComplete) { |
| 996 throw new StateError("Subscription has been canceled."); | 996 throw new StateError("Subscription has been canceled."); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1008 if (_isComplete) { | 1008 if (_isComplete) { |
| 1009 throw new StateError("Subscription has been canceled."); | 1009 throw new StateError("Subscription has been canceled."); |
| 1010 } | 1010 } |
| 1011 if (_timer != null) { | 1011 if (_timer != null) { |
| 1012 _timer.cancel(); | 1012 _timer.cancel(); |
| 1013 _timer = null; | 1013 _timer = null; |
| 1014 } | 1014 } |
| 1015 _pauseCount = 0; | 1015 _pauseCount = 0; |
| 1016 } | 1016 } |
| 1017 } | 1017 } |
| OLD | NEW |