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); |
| 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 class _CustomKeyEventStreamImpl extends _CustomEventStreamImpl<KeyEvent> |
| 136 implements CustomStream<KeyEvent> { |
| 137 _CustomKeyEventStreamImpl(String type) : super(type); |
| 138 |
| 139 void add(KeyEvent event) { |
| 140 if (event.type == _type) { |
| 141 event.currentTarget.dispatchEvent(event._parent); |
| 142 _streamController.add(event); |
| 143 } |
| 144 } |
| 145 } |
| 146 |
| 147 /** |
93 * A pool of streams whose events are unified and emitted through a central | 148 * A pool of streams whose events are unified and emitted through a central |
94 * stream. | 149 * stream. |
95 */ | 150 */ |
96 // TODO (efortuna): Remove this when Issue 12218 is addressed. | 151 // TODO (efortuna): Remove this when Issue 12218 is addressed. |
97 class _StreamPool<T> { | 152 class _StreamPool<T> { |
98 StreamController<T> _controller; | 153 StreamController<T> _controller; |
99 | 154 |
100 /// Subscriptions to the streams that make up the pool. | 155 /// Subscriptions to the streams that make up the pool. |
101 var _subscriptions = new Map<Stream<T>, StreamSubscription<T>>(); | 156 var _subscriptions = new Map<Stream<T>, StreamSubscription<T>>(); |
102 | 157 |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 | 382 |
328 ElementStream<T> _forElementList(ElementList e, | 383 ElementStream<T> _forElementList(ElementList e, |
329 {bool useCapture: false}) { | 384 {bool useCapture: false}) { |
330 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); | 385 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); |
331 } | 386 } |
332 | 387 |
333 String getEventType(EventTarget target) { | 388 String getEventType(EventTarget target) { |
334 return _eventTypeGetter(target); | 389 return _eventTypeGetter(target); |
335 } | 390 } |
336 } | 391 } |
OLD | NEW |