Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(466)

Side by Side Diff: tools/dom/src/EventStreamProvider.dart

Issue 2119243002: Reapply zone tasks. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Fix test and update status files for IE. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/dom/scripts/htmlrenamer.py ('k') | tools/dom/src/shared_html.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 * is first processed for the event target and then bubbles upward. 111 * is first processed for the event target and then bubbles upward.
112 * 112 *
113 * ## Other resources 113 * ## Other resources
114 * 114 *
115 * * [Event Capture](http://www.w3.org/TR/DOM-Level-2-Events/events.html#Event s-flow-capture) 115 * * [Event Capture](http://www.w3.org/TR/DOM-Level-2-Events/events.html#Event s-flow-capture)
116 * from the W3C DOM Events specification. 116 * from the W3C DOM Events specification.
117 */ 117 */
118 StreamSubscription<T> capture(void onData(T event)); 118 StreamSubscription<T> capture(void onData(T event));
119 } 119 }
120 120
121 /// Task specification for DOM Events.
122 ///
123 /// *Experimental*. May disappear without notice.
124 class EventSubscriptionSpecification<T extends Event>
125 implements TaskSpecification {
126 @override
127 final String name;
128 @override
129 final bool isOneShot;
130
131 final EventTarget target;
132 /// The event-type of the event. For example 'click' for click events.
133 final String eventType;
134 // TODO(floitsch): the first generic argument should be 'void'.
135 final ZoneUnaryCallback<dynamic, T> onData;
136 final bool useCapture;
137
138 EventSubscriptionSpecification({this.name, this.isOneShot, this.target,
139 this.eventType, void this.onData(T event), this.useCapture});
140
141 /// Returns a copy of this instance, with every non-null argument replaced
142 /// by the given value.
143 EventSubscriptionSpecification<T> replace(
144 {String name, bool isOneShot, EventTarget target,
145 String eventType, void onData(T event), bool useCapture}) {
146 return new EventSubscriptionSpecification<T>(
147 name: name ?? this.name,
148 isOneShot: isOneShot ?? this.isOneShot,
149 target: target ?? this.target,
150 eventType: eventType ?? this.eventType,
151 onData: onData ?? this.onData,
152 useCapture: useCapture ?? this.useCapture);
153 }
154 }
155
121 /** 156 /**
122 * Adapter for exposing DOM events as Dart streams. 157 * Adapter for exposing DOM events as Dart streams.
123 */ 158 */
124 class _EventStream<T extends Event> extends Stream<T> { 159 class _EventStream<T extends Event> extends Stream<T> {
125 final EventTarget _target; 160 final EventTarget _target;
126 final String _eventType; 161 final String _eventType;
127 final bool _useCapture; 162 final bool _useCapture;
163 /// The name that is used in the task specification.
164 final String _name;
165 /// Whether the stream can trigger multiple times.
166 final bool _isOneShot;
128 167
129 _EventStream(this._target, this._eventType, this._useCapture); 168 _EventStream(this._target, String eventType, this._useCapture,
169 {String name, bool isOneShot: false})
170 : _eventType = eventType,
171 _isOneShot = isOneShot,
172 _name = name ?? "dart.html.event.$eventType";
130 173
131 // DOM events are inherently multi-subscribers. 174 // DOM events are inherently multi-subscribers.
132 Stream<T> asBroadcastStream({void onListen(StreamSubscription<T> subscription) , 175 Stream<T> asBroadcastStream({void onListen(StreamSubscription<T> subscription) ,
133 void onCancel(StreamSubscription<T> subscription) }) 176 void onCancel(StreamSubscription<T> subscription) })
134 => this; 177 => this;
135 bool get isBroadcast => true; 178 bool get isBroadcast => true;
136 179
180 StreamSubscription<T> _listen(
181 void onData(T event), {bool useCapture}) {
182
183 if (identical(Zone.current, Zone.ROOT)) {
184 return new _EventStreamSubscription<T>(
185 this._target, this._eventType, onData, this._useCapture,
186 Zone.current);
187 }
188
189 var specification = new EventSubscriptionSpecification<T>(
190 name: this._name, isOneShot: this._isOneShot,
191 target: this._target, eventType: this._eventType,
192 onData: onData, useCapture: useCapture);
193 // We need to wrap the _createStreamSubscription call, since a tear-off
194 // would not bind the generic type 'T'.
195 return Zone.current.createTask((spec, Zone zone) {
196 return _createStreamSubscription/*<T>*/(spec, zone);
197 }, specification);
198 }
199
137 StreamSubscription<T> listen(void onData(T event), 200 StreamSubscription<T> listen(void onData(T event),
138 { Function onError, 201 { Function onError,
139 void onDone(), 202 void onDone(),
140 bool cancelOnError}) { 203 bool cancelOnError}) {
141 204 return _listen(onData, useCapture: this._useCapture);
142 return new _EventStreamSubscription<T>(
143 this._target, this._eventType, onData, this._useCapture);
144 } 205 }
145 } 206 }
146 207
147 bool _matchesWithAncestors(Event event, String selector) { 208 bool _matchesWithAncestors(Event event, String selector) {
148 var target = event.target; 209 var target = event.target;
149 return target is Element ? target.matchesWithAncestors(selector) : false; 210 return target is Element ? target.matchesWithAncestors(selector) : false;
150 } 211 }
151 212
152 /** 213 /**
153 * Adapter for exposing DOM Element events as streams, while also allowing 214 * Adapter for exposing DOM Element events as streams, while also allowing
154 * event delegation. 215 * event delegation.
155 */ 216 */
156 class _ElementEventStreamImpl<T extends Event> extends _EventStream<T> 217 class _ElementEventStreamImpl<T extends Event> extends _EventStream<T>
157 implements ElementStream<T> { 218 implements ElementStream<T> {
158 _ElementEventStreamImpl(target, eventType, useCapture) : 219 _ElementEventStreamImpl(target, eventType, useCapture,
159 super(target, eventType, useCapture); 220 {String name, bool isOneShot: false}) :
221 super(target, eventType, useCapture, name: name, isOneShot: isOneShot);
160 222
161 Stream<T> matches(String selector) => this.where( 223 Stream<T> matches(String selector) => this.where(
162 (event) => _matchesWithAncestors(event, selector)).map((e) { 224 (event) => _matchesWithAncestors(event, selector)).map((e) {
163 e._selector = selector; 225 e._selector = selector;
164 return e; 226 return e;
165 }); 227 });
166 228
167 StreamSubscription<T> capture(void onData(T event)) => 229 StreamSubscription<T> capture(void onData(T event)) {
168 new _EventStreamSubscription<T>( 230 return _listen(onData, useCapture: true);
169 this._target, this._eventType, onData, true); 231 }
170 } 232 }
171 233
172 /** 234 /**
173 * Adapter for exposing events on a collection of DOM Elements as streams, 235 * Adapter for exposing events on a collection of DOM Elements as streams,
174 * while also allowing event delegation. 236 * while also allowing event delegation.
175 */ 237 */
176 class _ElementListEventStreamImpl<T extends Event> extends Stream<T> 238 class _ElementListEventStreamImpl<T extends Event> extends Stream<T>
177 implements ElementStream<T> { 239 implements ElementStream<T> {
178 final Iterable<Element> _targetList; 240 final Iterable<Element> _targetList;
179 final bool _useCapture; 241 final bool _useCapture;
(...skipping 28 matching lines...) Expand all
208 } 270 }
209 return pool.stream.listen(onData); 271 return pool.stream.listen(onData);
210 } 272 }
211 273
212 Stream<T> asBroadcastStream({void onListen(StreamSubscription<T> subscription) , 274 Stream<T> asBroadcastStream({void onListen(StreamSubscription<T> subscription) ,
213 void onCancel(StreamSubscription<T> subscription) }) 275 void onCancel(StreamSubscription<T> subscription) })
214 => this; 276 => this;
215 bool get isBroadcast => true; 277 bool get isBroadcast => true;
216 } 278 }
217 279
218 // We would like this to just be EventListener<T> but that typdef cannot 280 StreamSubscription/*<T>*/ _createStreamSubscription/*<T>*/(
281 EventSubscriptionSpecification/*<T>*/ spec, Zone zone) {
282 return new _EventStreamSubscription/*<T>*/(spec.target, spec.eventType,
283 spec.onData, spec.useCapture, zone);
284 }
285
286 // We would like this to just be EventListener<T> but that typedef cannot
219 // use generics until dartbug/26276 is fixed. 287 // use generics until dartbug/26276 is fixed.
220 typedef _EventListener<T extends Event>(T event); 288 typedef _EventListener<T extends Event>(T event);
221 289
222 class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> { 290 class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> {
223 int _pauseCount = 0; 291 int _pauseCount = 0;
224 EventTarget _target; 292 EventTarget _target;
225 final String _eventType; 293 final String _eventType;
226 EventListener _onData; 294 EventListener _onData;
295 EventListener _domCallback;
227 final bool _useCapture; 296 final bool _useCapture;
297 final Zone _zone;
228 298
229 // TODO(jacobr): for full strong mode correctness we should write 299 // TODO(jacobr): for full strong mode correctness we should write
230 // _onData = onData == null ? null : _wrapZone/*<Event, dynamic>*/((e) => onDa ta(e as T)) 300 // _onData = onData == null ? null : _wrapZone/*<dynamic, Event>*/((e) => onDa ta(e as T))
231 // but that breaks 114 co19 tests as well as multiple html tests as it is reas onable 301 // but that breaks 114 co19 tests as well as multiple html tests as it is reas onable
232 // to pass the wrong type of event object to an event listener as part of a 302 // to pass the wrong type of event object to an event listener as part of a
233 // test. 303 // test.
234 _EventStreamSubscription(this._target, this._eventType, void onData(T event), 304 _EventStreamSubscription(this._target, this._eventType, void onData(T event),
235 this._useCapture) : _onData = _wrapZone/*<Event, dynamic>*/(onData) { 305 this._useCapture, Zone zone)
306 : _zone = zone,
307 _onData = _registerZone/*<dynamic, Event>*/(zone, onData) {
236 _tryResume(); 308 _tryResume();
237 } 309 }
238 310
239 Future cancel() { 311 Future cancel() {
240 if (_canceled) return null; 312 if (_canceled) return null;
241 313
242 _unlisten(); 314 _unlisten();
243 // Clear out the target to indicate this is complete. 315 // Clear out the target to indicate this is complete.
244 _target = null; 316 _target = null;
245 _onData = null; 317 _onData = null;
246 return null; 318 return null;
247 } 319 }
248 320
249 bool get _canceled => _target == null; 321 bool get _canceled => _target == null;
250 322
251 void onData(void handleData(T event)) { 323 void onData(void handleData(T event)) {
252 if (_canceled) { 324 if (_canceled) {
253 throw new StateError("Subscription has been canceled."); 325 throw new StateError("Subscription has been canceled.");
254 } 326 }
255 // Remove current event listener. 327 // Remove current event listener.
256 _unlisten(); 328 _unlisten();
257 _onData = _wrapZone/*<Event, dynamic>*/(handleData); 329 _onData = _registerZone/*<dynamic, Event>*/(_zone, handleData);
258 _tryResume(); 330 _tryResume();
259 } 331 }
260 332
261 /// Has no effect. 333 /// Has no effect.
262 void onError(Function handleError) {} 334 void onError(Function handleError) {}
263 335
264 /// Has no effect. 336 /// Has no effect.
265 void onDone(void handleDone()) {} 337 void onDone(void handleDone()) {}
266 338
267 void pause([Future resumeSignal]) { 339 void pause([Future resumeSignal]) {
268 if (_canceled) return; 340 if (_canceled) return;
269 ++_pauseCount; 341 ++_pauseCount;
270 _unlisten(); 342 _unlisten();
271 343
272 if (resumeSignal != null) { 344 if (resumeSignal != null) {
273 resumeSignal.whenComplete(resume); 345 resumeSignal.whenComplete(resume);
274 } 346 }
275 } 347 }
276 348
277 bool get isPaused => _pauseCount > 0; 349 bool get isPaused => _pauseCount > 0;
278 350
279 void resume() { 351 void resume() {
280 if (_canceled || !isPaused) return; 352 if (_canceled || !isPaused) return;
281 --_pauseCount; 353 --_pauseCount;
282 _tryResume(); 354 _tryResume();
283 } 355 }
284 356
285 void _tryResume() { 357 void _tryResume() {
286 if (_onData != null && !isPaused) { 358 if (_onData == null || isPaused) return;
287 _target.addEventListener(_eventType, _onData, _useCapture); 359 if (identical(_zone, Zone.ROOT)) {
360 _domCallback = _onData;
361 } else {
362 _domCallback = (event) {
363 _zone.runTask(_runEventNotification, this, event);
364 };
288 } 365 }
366 _target.addEventListener(_eventType, _domCallback, _useCapture);
367 }
368
369 static void _runEventNotification/*<T>*/(
370 _EventStreamSubscription/*<T>*/ subscription, /*=T*/ event) {
371 subscription._onData(event);
289 } 372 }
290 373
291 void _unlisten() { 374 void _unlisten() {
292 if (_onData != null) { 375 if (_onData != null) {
293 _target.removeEventListener(_eventType, _onData, _useCapture); 376 _target.removeEventListener(_eventType, _domCallback, _useCapture);
294 } 377 }
295 } 378 }
296 379
297 Future/*<E>*/ asFuture/*<E>*/([var/*=E*/ futureValue]) { 380 Future/*<E>*/ asFuture/*<E>*/([var/*=E*/ futureValue]) {
298 // We just need a future that will never succeed or fail. 381 // We just need a future that will never succeed or fail.
299 var completer = new Completer/*<E>*/(); 382 var completer = new Completer/*<E>*/();
300 return completer.future; 383 return completer.future;
301 } 384 }
302 } 385 }
303 386
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 return new _ElementListEventStreamImpl<T>(e, _eventTypeGetter(e), useCapture ); 520 return new _ElementListEventStreamImpl<T>(e, _eventTypeGetter(e), useCapture );
438 } 521 }
439 522
440 String getEventType(EventTarget target) { 523 String getEventType(EventTarget target) {
441 return _eventTypeGetter(target); 524 return _eventTypeGetter(target);
442 } 525 }
443 526
444 String get _eventType => 527 String get _eventType =>
445 throw new UnsupportedError('Access type through getEventType method.'); 528 throw new UnsupportedError('Access type through getEventType method.');
446 } 529 }
OLDNEW
« no previous file with comments | « tools/dom/scripts/htmlrenamer.py ('k') | tools/dom/src/shared_html.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698