| 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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 * | 127 * |
| 128 * MediaElement.pauseEvent.forTarget(document.body).listen(...); | 128 * MediaElement.pauseEvent.forTarget(document.body).listen(...); |
| 129 * | 129 * |
| 130 * See also: | 130 * See also: |
| 131 * | 131 * |
| 132 * [addEventListener](http://docs.webplatform.org/wiki/dom/methods/addEventLis
tener) | 132 * [addEventListener](http://docs.webplatform.org/wiki/dom/methods/addEventLis
tener) |
| 133 */ | 133 */ |
| 134 Stream<T> forTarget(EventTarget e, {bool useCapture: false}) { | 134 Stream<T> forTarget(EventTarget e, {bool useCapture: false}) { |
| 135 return new _EventStream(e, _eventType, useCapture); | 135 return new _EventStream(e, _eventType, useCapture); |
| 136 } | 136 } |
| 137 |
| 138 /** |
| 139 * Gets a single event [Stream] which is the combinations of event streams |
| 140 * for this event for each target. |
| 141 * |
| 142 * See also: |
| 143 * |
| 144 * * [forTarget] |
| 145 */ |
| 146 Stream<T> forTargets(List<EventTarget> targets, {bool useCapture: false}) { |
| 147 // Clone so modifications to the list don't affect this. |
| 148 targets = new List.from(targets); |
| 149 |
| 150 // Keep track of subscriptions so we unsubscribe from all when the stream |
| 151 // we return is unsubscribed. |
| 152 var subscriptions = []; |
| 153 var controller; |
| 154 controller = new StreamController<T>(onSubscriptionStateChange: () { |
| 155 if (controller.hasSubscribers) { |
| 156 assert(subscriptions.isEmpty); |
| 157 for (var target in targets) { |
| 158 subscriptions.add(forTarget(target, useCapture: useCapture).listen( |
| 159 (e) { |
| 160 controller.add(e); |
| 161 })); |
| 162 } |
| 163 } else { |
| 164 assert(subscriptions.length == targets.length); |
| 165 for (var subscription in subscriptions) { |
| 166 subscription.cancel(); |
| 167 } |
| 168 subscriptions.clear(); |
| 169 } |
| 170 }); |
| 171 return controller.stream; |
| 172 } |
| 137 } | 173 } |
| 138 | 174 |
| 139 /** | 175 /** |
| 140 * A factory to expose DOM events as streams, where the DOM event name has to | 176 * A factory to expose DOM events as streams, where the DOM event name has to |
| 141 * be determined on the fly (for example, mouse wheel events). | 177 * be determined on the fly (for example, mouse wheel events). |
| 142 */ | 178 */ |
| 143 class _CustomEventStreamProvider<T extends Event> | 179 class _CustomEventStreamProvider<T extends Event> |
| 144 implements EventStreamProvider<T> { | 180 implements EventStreamProvider<T> { |
| 145 | 181 |
| 146 final _eventTypeGetter; | 182 final _eventTypeGetter; |
| 147 const _CustomEventStreamProvider(this._eventTypeGetter); | 183 const _CustomEventStreamProvider(this._eventTypeGetter); |
| 148 | 184 |
| 149 Stream<T> forTarget(EventTarget e, {bool useCapture: false}) { | 185 Stream<T> forTarget(EventTarget e, {bool useCapture: false}) { |
| 150 return new _EventStream(e, _eventTypeGetter(e), useCapture); | 186 return new _EventStream(e, _eventTypeGetter(e), useCapture); |
| 151 } | 187 } |
| 152 } | 188 } |
| OLD | NEW |