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 * A factory to expose DOM events as Streams. | 8 * A factory to expose DOM events as Streams. |
9 */ | 9 */ |
10 class EventStreamProvider<T extends Event> { | 10 class EventStreamProvider<T extends Event> { |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 | 218 |
219 class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> { | 219 class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> { |
220 int _pauseCount = 0; | 220 int _pauseCount = 0; |
221 EventTarget _target; | 221 EventTarget _target; |
222 final String _eventType; | 222 final String _eventType; |
223 EventListener _onData; | 223 EventListener _onData; |
224 final bool _useCapture; | 224 final bool _useCapture; |
225 | 225 |
226 // TODO(leafp): It would be better to write this as | 226 // TODO(leafp): It would be better to write this as |
227 // _onData = onData == null ? null : | 227 // _onData = onData == null ? null : |
228 // onData is _wrapZoneCallback<Event, dynamic> | 228 // onData is void Function(Event) |
229 // ? _wrapZone/*<Event, dynamic>*/(onData) | 229 // ? _wrapZone<Event>(onData) |
230 // : _wrapZone/*<Event, dynamic>*/((e) => onData(e as T)) | 230 // : _wrapZone<Event>((e) => onData(e as T)) |
231 // In order to support existing tests which pass the wrong type of events but | 231 // In order to support existing tests which pass the wrong type of events but |
232 // use a more general listener, without causing as much slowdown for things | 232 // use a more general listener, without causing as much slowdown for things |
233 // which are typed correctly. But this currently runs afoul of restrictions | 233 // which are typed correctly. But this currently runs afoul of restrictions |
234 // on is checks for compatibility with the VM. | 234 // on is checks for compatibility with the VM. |
235 _EventStreamSubscription( | 235 _EventStreamSubscription( |
236 this._target, this._eventType, void onData(T event), this._useCapture) | 236 this._target, this._eventType, void onData(T event), this._useCapture) |
237 : _onData = onData == null | 237 : _onData = onData == null |
238 ? null | 238 ? null |
239 : _wrapZone<Event, dynamic>((e) => (onData as dynamic)(e)) { | 239 : _wrapZone<Event>((e) => (onData as dynamic)(e)) { |
240 _tryResume(); | 240 _tryResume(); |
241 } | 241 } |
242 | 242 |
243 Future cancel() { | 243 Future cancel() { |
244 if (_canceled) return null; | 244 if (_canceled) return null; |
245 | 245 |
246 _unlisten(); | 246 _unlisten(); |
247 // Clear out the target to indicate this is complete. | 247 // Clear out the target to indicate this is complete. |
248 _target = null; | 248 _target = null; |
249 _onData = null; | 249 _onData = null; |
250 return null; | 250 return null; |
251 } | 251 } |
252 | 252 |
253 bool get _canceled => _target == null; | 253 bool get _canceled => _target == null; |
254 | 254 |
255 void onData(void handleData(T event)) { | 255 void onData(void handleData(T event)) { |
256 if (_canceled) { | 256 if (_canceled) { |
257 throw new StateError("Subscription has been canceled."); | 257 throw new StateError("Subscription has been canceled."); |
258 } | 258 } |
259 // Remove current event listener. | 259 // Remove current event listener. |
260 _unlisten(); | 260 _unlisten(); |
261 _onData = _wrapZone/*<Event, dynamic>*/(handleData); | 261 _onData = _wrapZone<Event>(handleData); |
262 _tryResume(); | 262 _tryResume(); |
263 } | 263 } |
264 | 264 |
265 /// Has no effect. | 265 /// Has no effect. |
266 void onError(Function handleError) {} | 266 void onError(Function handleError) {} |
267 | 267 |
268 /// Has no effect. | 268 /// Has no effect. |
269 void onDone(void handleDone()) {} | 269 void onDone(void handleDone()) {} |
270 | 270 |
271 void pause([Future resumeSignal]) { | 271 void pause([Future resumeSignal]) { |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 e, _eventTypeGetter(e), useCapture); | 438 e, _eventTypeGetter(e), useCapture); |
439 } | 439 } |
440 | 440 |
441 String getEventType(EventTarget target) { | 441 String getEventType(EventTarget target) { |
442 return _eventTypeGetter(target); | 442 return _eventTypeGetter(target); |
443 } | 443 } |
444 | 444 |
445 String get _eventType => | 445 String get _eventType => |
446 throw new UnsupportedError('Access type through getEventType method.'); | 446 throw new UnsupportedError('Access type through getEventType method.'); |
447 } | 447 } |
OLD | NEW |