Chromium Code Reviews| 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 bool cancelOnError}) => | 83 bool cancelOnError}) => |
| 84 _stream.listen(onData, onError: onError, onDone: onDone, | 84 _stream.listen(onData, onError: onError, onDone: onDone, |
| 85 cancelOnError: cancelOnError); | 85 cancelOnError: cancelOnError); |
| 86 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), | 86 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), |
| 87 void onCancel(StreamSubscription subscription)}) | 87 void onCancel(StreamSubscription subscription)}) |
| 88 => _stream; | 88 => _stream; |
| 89 bool get isBroadcast => true; | 89 bool get isBroadcast => true; |
| 90 } | 90 } |
| 91 | 91 |
| 92 /** | 92 /** |
| 93 * A stream of custom events, which enables the user to "fire" (add) their own | |
| 94 * custom events to a stream. | |
| 95 */ | |
| 96 abstract class CustomStream<T extends Event> implements Stream<T> { | |
| 97 /** | |
| 98 * Add the following custom event to the stream for dispatching to interested | |
| 99 * listeners. | |
| 100 */ | |
| 101 void add(T event); | |
|
strom
2013/09/12 15:42:41
I understand using add() here, especially since it
| |
| 102 } | |
| 103 | |
| 104 class _CustomEventStreamImpl<T extends Event> extends Stream<T> | |
| 105 implements CustomStream<T> { | |
| 106 StreamController<T> _streamController; | |
| 107 /** The type of event this stream is providing (e.g. "keydown"). */ | |
| 108 String _type; | |
| 109 | |
| 110 _CustomEventStreamImpl(String type) { | |
| 111 _type = type; | |
| 112 _streamController = new StreamController.broadcast(sync: true); | |
| 113 } | |
| 114 | |
| 115 // Delegate all regular Stream behavior to our wrapped Stream. | |
| 116 StreamSubscription<T> listen(void onData(T event), | |
| 117 { void onError(error), | |
| 118 void onDone(), | |
| 119 bool cancelOnError}) { | |
| 120 return _streamController.stream.listen(onData, onError: onError, | |
| 121 onDone: onDone, cancelOnError: cancelOnError); | |
| 122 } | |
| 123 | |
| 124 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), | |
| 125 void onCancel(StreamSubscription subscription)}) | |
| 126 => _streamController.stream; | |
| 127 | |
| 128 bool get isBroadcast => true; | |
| 129 | |
| 130 void add(T event) { | |
| 131 if (event.type == _type) _streamController.add(event); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 /** | |
| 93 * A pool of streams whose events are unified and emitted through a central | 136 * A pool of streams whose events are unified and emitted through a central |
| 94 * stream. | 137 * stream. |
| 95 */ | 138 */ |
| 96 // TODO (efortuna): Remove this when Issue 12218 is addressed. | 139 // TODO (efortuna): Remove this when Issue 12218 is addressed. |
| 97 class _StreamPool<T> { | 140 class _StreamPool<T> { |
| 98 StreamController<T> _controller; | 141 StreamController<T> _controller; |
| 99 | 142 |
| 100 /// Subscriptions to the streams that make up the pool. | 143 /// Subscriptions to the streams that make up the pool. |
| 101 var _subscriptions = new Map<Stream<T>, StreamSubscription<T>>(); | 144 var _subscriptions = new Map<Stream<T>, StreamSubscription<T>>(); |
| 102 | 145 |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 321 | 364 |
| 322 ElementStream<T> _forElementList(ElementList e, | 365 ElementStream<T> _forElementList(ElementList e, |
| 323 {bool useCapture: false}) { | 366 {bool useCapture: false}) { |
| 324 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); | 367 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); |
| 325 } | 368 } |
| 326 | 369 |
| 327 String getEventType(EventTarget target) { | 370 String getEventType(EventTarget target) { |
| 328 return _eventTypeGetter(target); | 371 return _eventTypeGetter(target); |
| 329 } | 372 } |
| 330 } | 373 } |
| OLD | NEW |