| 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> { |
| 11 final EventTarget _target; | 11 final EventTarget _target; |
| 12 final String _eventType; | 12 final String _eventType; |
| 13 final bool _useCapture; | 13 final bool _useCapture; |
| 14 | 14 |
| 15 _EventStream(this._target, this._eventType, this._useCapture); | 15 _EventStream(this._target, this._eventType, this._useCapture); |
| 16 | 16 |
| 17 // DOM events are inherently multi-subscribers. | 17 // DOM events are inherently multi-subscribers. |
| 18 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), | 18 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), |
| 19 void onCancel(StreamSubscription subscription)}) | 19 void onCancel(StreamSubscription subscription)}) |
| 20 => this; | 20 => this; |
| 21 bool get isBroadcast => true; | 21 bool get isBroadcast => true; |
| 22 | 22 |
| 23 StreamSubscription<T> listen(void onData(T event), | 23 StreamSubscription<T> listen(void onData(T event), |
| 24 { void onError(error), | 24 { Function onError, |
| 25 void onDone(), | 25 void onDone(), |
| 26 bool cancelOnError}) { | 26 bool cancelOnError}) { |
| 27 | 27 |
| 28 return new _EventStreamSubscription<T>( | 28 return new _EventStreamSubscription<T>( |
| 29 this._target, this._eventType, onData, this._useCapture); | 29 this._target, this._eventType, onData, this._useCapture); |
| 30 } | 30 } |
| 31 } | 31 } |
| 32 | 32 |
| 33 /** A specialized Stream available to [Element]s to enable event delegation. */ | 33 /** A specialized Stream available to [Element]s to enable event delegation. */ |
| 34 abstract class ElementStream<T extends Event> implements Stream<T> { | 34 abstract class ElementStream<T extends Event> implements Stream<T> { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 } | 77 } |
| 78 | 78 |
| 79 Stream<T> matches(String selector) => this.where( | 79 Stream<T> matches(String selector) => this.where( |
| 80 (event) => event.target.matchesWithAncestors(selector)).map((e) { | 80 (event) => event.target.matchesWithAncestors(selector)).map((e) { |
| 81 e._selector = selector; | 81 e._selector = selector; |
| 82 return e; | 82 return e; |
| 83 }); | 83 }); |
| 84 | 84 |
| 85 // Delegate all regular Stream behavor to our wrapped Stream. | 85 // Delegate all regular Stream behavor to our wrapped Stream. |
| 86 StreamSubscription<T> listen(void onData(T event), | 86 StreamSubscription<T> listen(void onData(T event), |
| 87 { void onError(error), | 87 { Function onError, |
| 88 void onDone(), | 88 void onDone(), |
| 89 bool cancelOnError}) => | 89 bool cancelOnError}) => |
| 90 _stream.listen(onData, onError: onError, onDone: onDone, | 90 _stream.listen(onData, onError: onError, onDone: onDone, |
| 91 cancelOnError: cancelOnError); | 91 cancelOnError: cancelOnError); |
| 92 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), | 92 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), |
| 93 void onCancel(StreamSubscription subscription)}) | 93 void onCancel(StreamSubscription subscription)}) |
| 94 => _stream; | 94 => _stream; |
| 95 bool get isBroadcast => true; | 95 bool get isBroadcast => true; |
| 96 } | 96 } |
| 97 | 97 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 113 /** The type of event this stream is providing (e.g. "keydown"). */ | 113 /** The type of event this stream is providing (e.g. "keydown"). */ |
| 114 String _type; | 114 String _type; |
| 115 | 115 |
| 116 _CustomEventStreamImpl(String type) { | 116 _CustomEventStreamImpl(String type) { |
| 117 _type = type; | 117 _type = type; |
| 118 _streamController = new StreamController.broadcast(sync: true); | 118 _streamController = new StreamController.broadcast(sync: true); |
| 119 } | 119 } |
| 120 | 120 |
| 121 // Delegate all regular Stream behavior to our wrapped Stream. | 121 // Delegate all regular Stream behavior to our wrapped Stream. |
| 122 StreamSubscription<T> listen(void onData(T event), | 122 StreamSubscription<T> listen(void onData(T event), |
| 123 { void onError(error), | 123 { Function onError, |
| 124 void onDone(), | 124 void onDone(), |
| 125 bool cancelOnError}) { | 125 bool cancelOnError}) { |
| 126 return _streamController.stream.listen(onData, onError: onError, | 126 return _streamController.stream.listen(onData, onError: onError, |
| 127 onDone: onDone, cancelOnError: cancelOnError); | 127 onDone: onDone, cancelOnError: cancelOnError); |
| 128 } | 128 } |
| 129 | 129 |
| 130 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), | 130 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), |
| 131 void onCancel(StreamSubscription subscription)}) | 131 void onCancel(StreamSubscription subscription)}) |
| 132 => _streamController.stream; | 132 => _streamController.stream; |
| 133 | 133 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 throw new StateError("Subscription has been canceled."); | 242 throw new StateError("Subscription has been canceled."); |
| 243 } | 243 } |
| 244 // Remove current event listener. | 244 // Remove current event listener. |
| 245 _unlisten(); | 245 _unlisten(); |
| 246 | 246 |
| 247 _onData = _wrapZone(handleData); | 247 _onData = _wrapZone(handleData); |
| 248 _tryResume(); | 248 _tryResume(); |
| 249 } | 249 } |
| 250 | 250 |
| 251 /// Has no effect. | 251 /// Has no effect. |
| 252 void onError(void handleError(error)) {} | 252 void onError(Function handleError) {} |
| 253 | 253 |
| 254 /// Has no effect. | 254 /// Has no effect. |
| 255 void onDone(void handleDone()) {} | 255 void onDone(void handleDone()) {} |
| 256 | 256 |
| 257 void pause([Future resumeSignal]) { | 257 void pause([Future resumeSignal]) { |
| 258 if (_canceled) return; | 258 if (_canceled) return; |
| 259 ++_pauseCount; | 259 ++_pauseCount; |
| 260 _unlisten(); | 260 _unlisten(); |
| 261 | 261 |
| 262 if (resumeSignal != null) { | 262 if (resumeSignal != null) { |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 | 388 |
| 389 ElementStream<T> _forElementList(ElementList e, | 389 ElementStream<T> _forElementList(ElementList e, |
| 390 {bool useCapture: false}) { | 390 {bool useCapture: false}) { |
| 391 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); | 391 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); |
| 392 } | 392 } |
| 393 | 393 |
| 394 String getEventType(EventTarget target) { | 394 String getEventType(EventTarget target) { |
| 395 return _eventTypeGetter(target); | 395 return _eventTypeGetter(target); |
| 396 } | 396 } |
| 397 } | 397 } |
| OLD | NEW |