| 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 * If events are added to the stream (using [_add], [_signalError] or [_done]) | 55 * If events are added to the stream (using [_add], [_signalError] or [_done]) |
| 56 * while the stream is paused, or while another event is firing, events will | 56 * while the stream is paused, or while another event is firing, events will |
| 57 * stored here. | 57 * stored here. |
| 58 * Also supports scheduling the events for later execution. | 58 * Also supports scheduling the events for later execution. |
| 59 */ | 59 */ |
| 60 _PendingEvents _pendingEvents; | 60 _PendingEvents _pendingEvents; |
| 61 | 61 |
| 62 // ------------------------------------------------------------------ | 62 // ------------------------------------------------------------------ |
| 63 // Stream interface. | 63 // Stream interface. |
| 64 | 64 |
| 65 StreamSubscription listen(void onData(T data), | 65 StreamSubscription<T> listen(void onData(T data), |
| 66 { void onError(AsyncError error), | 66 { void onError(AsyncError error), |
| 67 void onDone(), | 67 void onDone(), |
| 68 bool unsubscribeOnError }) { | 68 bool unsubscribeOnError }) { |
| 69 if (_isComplete) { | 69 if (_isComplete) { |
| 70 return new _DoneSubscription(onDone); | 70 return new _DoneSubscription(onDone); |
| 71 } | 71 } |
| 72 if (onData == null) onData = _nullDataHandler; | 72 if (onData == null) onData = _nullDataHandler; |
| 73 if (onError == null) onError = _nullErrorHandler; | 73 if (onError == null) onError = _nullErrorHandler; |
| 74 if (onDone == null) onDone = _nullDoneHandler; | 74 if (onDone == null) onDone = _nullDoneHandler; |
| 75 unsubscribeOnError = identical(true, unsubscribeOnError); | 75 unsubscribeOnError = identical(true, unsubscribeOnError); |
| 76 _StreamListener subscription = | 76 _StreamSubscriptionImpl subscription = |
| 77 _createSubscription(onData, onError, onDone, unsubscribeOnError); | 77 _createSubscription(onData, onError, onDone, unsubscribeOnError); |
| 78 _addListener(subscription); | 78 _addListener(subscription); |
| 79 return subscription; | 79 return subscription; |
| 80 } | 80 } |
| 81 | 81 |
| 82 // ------------------------------------------------------------------ | 82 // ------------------------------------------------------------------ |
| 83 // StreamSink interface-like methods for sending events into the stream. | 83 // StreamSink interface-like methods for sending events into the stream. |
| 84 // It's the responsibility of the caller to ensure that the stream is not | 84 // It's the responsibility of the caller to ensure that the stream is not |
| 85 // paused when adding events. If the stream is paused, the events will be | 85 // paused when adding events. If the stream is paused, the events will be |
| 86 // queued, but it's better to not send events at all. | 86 // queued, but it's better to not send events at all. |
| (...skipping 998 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1085 void _delayDone() { | 1085 void _delayDone() { |
| 1086 assert(_timer == null && _pauseCount == 0); | 1086 assert(_timer == null && _pauseCount == 0); |
| 1087 _timer = new Timer(0, (_) { | 1087 _timer = new Timer(0, (_) { |
| 1088 if (_handler != null) _handler(); | 1088 if (_handler != null) _handler(); |
| 1089 }); | 1089 }); |
| 1090 } | 1090 } |
| 1091 | 1091 |
| 1092 bool get _isComplete => _timer == null && _pauseCount == 0; | 1092 bool get _isComplete => _timer == null && _pauseCount == 0; |
| 1093 | 1093 |
| 1094 void onData(void handleAction(T value)) {} | 1094 void onData(void handleAction(T value)) {} |
| 1095 void onError(void handleError(StateError error)) {} | 1095 |
| 1096 void onDone(void handleDone(T value)) { | 1096 void onError(void handleError(AsyncError error)) {} |
| 1097 |
| 1098 void onDone(void handleDone()) { |
| 1097 _handler = handleDone; | 1099 _handler = handleDone; |
| 1098 } | 1100 } |
| 1099 | 1101 |
| 1100 void pause([Future signal]) { | 1102 void pause([Future signal]) { |
| 1101 if (_isComplete) { | 1103 if (_isComplete) { |
| 1102 throw new StateError("Subscription has been canceled."); | 1104 throw new StateError("Subscription has been canceled."); |
| 1103 } | 1105 } |
| 1104 if (_timer != null) _timer.cancel(); | 1106 if (_timer != null) _timer.cancel(); |
| 1105 _pauseCount++; | 1107 _pauseCount++; |
| 1106 } | 1108 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1159 onError: this._signalError, | 1161 onError: this._signalError, |
| 1160 onDone: this._close); | 1162 onDone: this._close); |
| 1161 } else { | 1163 } else { |
| 1162 // TODO(lrn): Check why this can happen. | 1164 // TODO(lrn): Check why this can happen. |
| 1163 if (_subscription == null) return; | 1165 if (_subscription == null) return; |
| 1164 _subscription.cancel(); | 1166 _subscription.cancel(); |
| 1165 _subscription = null; | 1167 _subscription = null; |
| 1166 } | 1168 } |
| 1167 } | 1169 } |
| 1168 } | 1170 } |
| OLD | NEW |