| 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 default zone. |
| 164 final zone = currentZoneExperimental; |
| 165 if (zone.isDefault) return callback; |
| 166 |
| 167 zone.expectCallback(); |
| 168 return (e) => zone.executePeriodicCallbackGuarded(() => callback(e)); |
| 169 } |
| 170 |
| 162 void cancel() { | 171 void cancel() { |
| 163 if (_canceled) return; | 172 if (_canceled) return; |
| 164 | 173 |
| 165 _unlisten(); | 174 _unlisten(); |
| 166 // Clear out the target to indicate this is complete. | 175 // Clear out the target to indicate this is complete. |
| 167 _target = null; | 176 _target = null; |
| 168 _onData = null; | 177 _onData = null; |
| 178 |
| 179 final zone = currentZoneExperimental; |
| 180 if (!zone.isDefault) zone.cancelCallbackExpectation(); |
| 169 } | 181 } |
| 170 | 182 |
| 171 bool get _canceled => _target == null; | 183 bool get _canceled => _target == null; |
| 172 | 184 |
| 173 void onData(void handleData(T event)) { | 185 void onData(void handleData(T event)) { |
| 174 if (_canceled) { | 186 if (_canceled) { |
| 175 throw new StateError("Subscription has been canceled."); | 187 throw new StateError("Subscription has been canceled."); |
| 176 } | 188 } |
| 177 // Remove current event listener. | 189 // Remove current event listener. |
| 178 _unlisten(); | 190 _unlisten(); |
| 179 | 191 |
| 180 _onData = handleData; | 192 _onData = _wrapZone(handleData); |
| 181 _tryResume(); | 193 _tryResume(); |
| 182 } | 194 } |
| 183 | 195 |
| 184 /// Has no effect. | 196 /// Has no effect. |
| 185 void onError(void handleError(error)) {} | 197 void onError(void handleError(error)) {} |
| 186 | 198 |
| 187 /// Has no effect. | 199 /// Has no effect. |
| 188 void onDone(void handleDone()) {} | 200 void onDone(void handleDone()) {} |
| 189 | 201 |
| 190 void pause([Future resumeSignal]) { | 202 void pause([Future resumeSignal]) { |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 | 333 |
| 322 ElementStream<T> _forElementList(ElementList e, | 334 ElementStream<T> _forElementList(ElementList e, |
| 323 {bool useCapture: false}) { | 335 {bool useCapture: false}) { |
| 324 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); | 336 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); |
| 325 } | 337 } |
| 326 | 338 |
| 327 String getEventType(EventTarget target) { | 339 String getEventType(EventTarget target) { |
| 328 return _eventTypeGetter(target); | 340 return _eventTypeGetter(target); |
| 329 } | 341 } |
| 330 } | 342 } |
| OLD | NEW |