| 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 part of html; | 5 part of html; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Adapter for exposing DOM events as Dart streams. | 8 * Adapter for exposing DOM events as Dart streams. |
| 9 */ | 9 */ |
| 10 class _EventStream<T extends Event> extends Stream<T> { | 10 class _EventStream<T extends Event> extends Stream<T> { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 final String _eventType; | 34 final String _eventType; |
| 35 var _onData; | 35 var _onData; |
| 36 final bool _useCapture; | 36 final bool _useCapture; |
| 37 | 37 |
| 38 _EventStreamSubscription(this._target, this._eventType, this._onData, | 38 _EventStreamSubscription(this._target, this._eventType, this._onData, |
| 39 this._useCapture) { | 39 this._useCapture) { |
| 40 _tryResume(); | 40 _tryResume(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void cancel() { | 43 void cancel() { |
| 44 if (_canceled) { | 44 if (_canceled) return; |
| 45 throw new StateError("Subscription has been canceled."); | |
| 46 } | |
| 47 | 45 |
| 48 _unlisten(); | 46 _unlisten(); |
| 49 // Clear out the target to indicate this is complete. | 47 // Clear out the target to indicate this is complete. |
| 50 _target = null; | 48 _target = null; |
| 51 _onData = null; | 49 _onData = null; |
| 52 } | 50 } |
| 53 | 51 |
| 54 bool get _canceled => _target == null; | 52 bool get _canceled => _target == null; |
| 55 | 53 |
| 56 void onData(void handleData(T event)) { | 54 void onData(void handleData(T event)) { |
| 57 if (_canceled) { | 55 if (_canceled) { |
| 58 throw new StateError("Subscription has been canceled."); | 56 throw new StateError("Subscription has been canceled."); |
| 59 } | 57 } |
| 60 // Remove current event listener. | 58 // Remove current event listener. |
| 61 _unlisten(); | 59 _unlisten(); |
| 62 | 60 |
| 63 _onData = handleData; | 61 _onData = handleData; |
| 64 _tryResume(); | 62 _tryResume(); |
| 65 } | 63 } |
| 66 | 64 |
| 67 /// Has no effect. | 65 /// Has no effect. |
| 68 void onError(void handleError(AsyncError error)) {} | 66 void onError(void handleError(AsyncError error)) {} |
| 69 | 67 |
| 70 /// Has no effect. | 68 /// Has no effect. |
| 71 void onDone(void handleDone()) {} | 69 void onDone(void handleDone()) {} |
| 72 | 70 |
| 73 void pause([Future resumeSignal]) { | 71 void pause([Future resumeSignal]) { |
| 74 if (_canceled) { | 72 if (_canceled) return; |
| 75 throw new StateError("Subscription has been canceled."); | |
| 76 } | |
| 77 ++_pauseCount; | 73 ++_pauseCount; |
| 78 _unlisten(); | 74 _unlisten(); |
| 79 | 75 |
| 80 if (resumeSignal != null) { | 76 if (resumeSignal != null) { |
| 81 resumeSignal.whenComplete(resume); | 77 resumeSignal.whenComplete(resume); |
| 82 } | 78 } |
| 83 } | 79 } |
| 84 | 80 |
| 85 bool get _paused => _pauseCount > 0; | 81 bool get _paused => _pauseCount > 0; |
| 86 | 82 |
| 87 void resume() { | 83 void resume() { |
| 88 if (_canceled) { | 84 if (_canceled || !_paused) return; |
| 89 throw new StateError("Subscription has been canceled."); | |
| 90 } | |
| 91 if (!_paused) { | |
| 92 throw new StateError("Subscription is not paused."); | |
| 93 } | |
| 94 --_pauseCount; | 85 --_pauseCount; |
| 95 _tryResume(); | 86 _tryResume(); |
| 96 } | 87 } |
| 97 | 88 |
| 98 void _tryResume() { | 89 void _tryResume() { |
| 99 if (_onData != null && !_paused) { | 90 if (_onData != null && !_paused) { |
| 100 _target.$dom_addEventListener(_eventType, _onData, _useCapture); | 91 _target.$dom_addEventListener(_eventType, _onData, _useCapture); |
| 101 } | 92 } |
| 102 } | 93 } |
| 103 | 94 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 const _CustomEventStreamProvider(this._eventTypeGetter); | 150 const _CustomEventStreamProvider(this._eventTypeGetter); |
| 160 | 151 |
| 161 Stream<T> forTarget(EventTarget e, {bool useCapture: false}) { | 152 Stream<T> forTarget(EventTarget e, {bool useCapture: false}) { |
| 162 return new _EventStream(e, _eventTypeGetter(e), useCapture); | 153 return new _EventStream(e, _eventTypeGetter(e), useCapture); |
| 163 } | 154 } |
| 164 | 155 |
| 165 String getEventType(EventTarget target) { | 156 String getEventType(EventTarget target) { |
| 166 return _eventTypeGetter(target); | 157 return _eventTypeGetter(target); |
| 167 } | 158 } |
| 168 } | 159 } |
| OLD | NEW |