| 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 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 | 149 |
| 150 class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> { | 150 class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> { |
| 151 int _pauseCount = 0; | 151 int _pauseCount = 0; |
| 152 EventTarget _target; | 152 EventTarget _target; |
| 153 final String _eventType; | 153 final String _eventType; |
| 154 var _onData; | 154 var _onData; |
| 155 final bool _useCapture; | 155 final bool _useCapture; |
| 156 | 156 |
| 157 _EventStreamSubscription(this._target, this._eventType, this._onData, | 157 _EventStreamSubscription(this._target, this._eventType, onData, |
| 158 this._useCapture) { | 158 this._useCapture) : _onData = _wrapZone(onData) { |
| 159 _tryResume(); | 159 _tryResume(); |
| 160 } | 160 } |
| 161 | 161 |
| 162 static _wrapZone(callback) { |
| 163 // For performance reasons avoid wrapping if we are in the root zone. |
| 164 if (Zone.current == Zone.ROOT) return callback; |
| 165 return Zone.current.bindUnaryCallback(callback, runGuarded: true); |
| 166 } |
| 167 |
| 162 void cancel() { | 168 void cancel() { |
| 163 if (_canceled) return; | 169 if (_canceled) return; |
| 164 | 170 |
| 165 _unlisten(); | 171 _unlisten(); |
| 166 // Clear out the target to indicate this is complete. | 172 // Clear out the target to indicate this is complete. |
| 167 _target = null; | 173 _target = null; |
| 168 _onData = null; | 174 _onData = null; |
| 169 } | 175 } |
| 170 | 176 |
| 171 bool get _canceled => _target == null; | 177 bool get _canceled => _target == null; |
| 172 | 178 |
| 173 void onData(void handleData(T event)) { | 179 void onData(void handleData(T event)) { |
| 174 if (_canceled) { | 180 if (_canceled) { |
| 175 throw new StateError("Subscription has been canceled."); | 181 throw new StateError("Subscription has been canceled."); |
| 176 } | 182 } |
| 177 // Remove current event listener. | 183 // Remove current event listener. |
| 178 _unlisten(); | 184 _unlisten(); |
| 179 | 185 |
| 180 _onData = handleData; | 186 _onData = _wrapZone(handleData); |
| 181 _tryResume(); | 187 _tryResume(); |
| 182 } | 188 } |
| 183 | 189 |
| 184 /// Has no effect. | 190 /// Has no effect. |
| 185 void onError(void handleError(error)) {} | 191 void onError(void handleError(error)) {} |
| 186 | 192 |
| 187 /// Has no effect. | 193 /// Has no effect. |
| 188 void onDone(void handleDone()) {} | 194 void onDone(void handleDone()) {} |
| 189 | 195 |
| 190 void pause([Future resumeSignal]) { | 196 void pause([Future resumeSignal]) { |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 | 327 |
| 322 ElementStream<T> _forElementList(ElementList e, | 328 ElementStream<T> _forElementList(ElementList e, |
| 323 {bool useCapture: false}) { | 329 {bool useCapture: false}) { |
| 324 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); | 330 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); |
| 325 } | 331 } |
| 326 | 332 |
| 327 String getEventType(EventTarget target) { | 333 String getEventType(EventTarget target) { |
| 328 return _eventTypeGetter(target); | 334 return _eventTypeGetter(target); |
| 329 } | 335 } |
| 330 } | 336 } |
| OLD | NEW |