Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of html; | |
| 6 | |
| 7 /** | |
| 8 * Adapter for exposing DOM events as Dart streams. | |
| 9 */ | |
| 10 class _HtmlStreamController<T extends Event> extends StreamController<T> { | |
|
Jennifer Messerly
2013/01/11 21:57:17
High level question: do we want to subclass Stream
floitsch
2013/01/11 22:46:11
If I'm not wrong we can directly forward the regis
blois
2013/01/11 22:51:30
Will take a look at this now.
Jennifer Messerly
2013/01/11 23:30:14
I hope it works -- that would be awesome :)
| |
| 11 final EventTarget _target; | |
| 12 final String _eventType; | |
| 13 final bool _useCapture; | |
| 14 EventListener _eventHandler; | |
| 15 | |
| 16 _HtmlStreamController(this._target, this._eventType, this._useCapture) { | |
| 17 // Stash reference to the method to allow removeEventListener to function. | |
| 18 _eventHandler = _handleEvent; | |
|
Jennifer Messerly
2013/01/11 21:57:17
Can this just be:
_eventHandler = this.add;
blois
2013/01/11 22:51:30
Unfortunately, this fails in dart2js checked mode.
| |
| 19 } | |
| 20 | |
| 21 void onSubscriptionStateChange() { | |
| 22 super.onSubscriptionStateChange(); | |
| 23 if (hasSubscribers) { | |
| 24 _target.$dom_addEventListener(_eventType, _eventHandler, _useCapture); | |
| 25 } else { | |
| 26 _target.$dom_removeEventListener(_eventType, _eventHandler, _useCapture); | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 void _handleEvent(e) { | |
| 31 this.add(e); | |
| 32 } | |
| 33 } | |
| 34 | |
| 35 | |
| 36 /** | |
| 37 * A factory to expose DOM events as Streams. | |
| 38 */ | |
| 39 class HtmlStreamProvider<T extends Event> { | |
| 40 final String _eventType; | |
| 41 | |
| 42 const HtmlStreamProvider(this._eventType); | |
| 43 | |
| 44 /** | |
| 45 * Gets a [Stream] for this event type, on the specified target. | |
| 46 * | |
| 47 * This may be used to capture DOM events: | |
| 48 * | |
| 49 * Element.keyDownEvent.forTarget(element, useCapture: true).listen(...); | |
| 50 * | |
| 51 * Or for listening to an event which will bubble through the DOM tree: | |
| 52 * | |
| 53 * MediaElement.pauseEvent.forTarget(document.body).listen(...); | |
| 54 * | |
| 55 * See also: | |
| 56 * | |
| 57 * [addEventListener](http://docs.webplatform.org/wiki/dom/methods/addEventLis tener) | |
| 58 */ | |
| 59 Stream<T> forTarget(EventTarget e, {bool useCapture: false}) { | |
| 60 return new _HtmlStreamController(e, _eventType, useCapture); | |
| 61 } | |
| 62 } | |
| OLD | NEW |