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

Unified Diff: tools/dom/src/HtmlStreamController.dart

Issue 11824072: Adding streams to dart:html. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: tools/dom/src/HtmlStreamController.dart
diff --git a/tools/dom/src/HtmlStreamController.dart b/tools/dom/src/HtmlStreamController.dart
new file mode 100644
index 0000000000000000000000000000000000000000..34328264a4b4e1e295d303a32ab13f318248a46f
--- /dev/null
+++ b/tools/dom/src/HtmlStreamController.dart
@@ -0,0 +1,62 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+part of html;
+
+/**
+ * Adapter for exposing DOM events as Dart streams.
+ */
+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 :)
+ final EventTarget _target;
+ final String _eventType;
+ final bool _useCapture;
+ EventListener _eventHandler;
+
+ _HtmlStreamController(this._target, this._eventType, this._useCapture) {
+ // Stash reference to the method to allow removeEventListener to function.
+ _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.
+ }
+
+ void onSubscriptionStateChange() {
+ super.onSubscriptionStateChange();
+ if (hasSubscribers) {
+ _target.$dom_addEventListener(_eventType, _eventHandler, _useCapture);
+ } else {
+ _target.$dom_removeEventListener(_eventType, _eventHandler, _useCapture);
+ }
+ }
+
+ void _handleEvent(e) {
+ this.add(e);
+ }
+}
+
+
+/**
+ * A factory to expose DOM events as Streams.
+ */
+class HtmlStreamProvider<T extends Event> {
+ final String _eventType;
+
+ const HtmlStreamProvider(this._eventType);
+
+ /**
+ * Gets a [Stream] for this event type, on the specified target.
+ *
+ * This may be used to capture DOM events:
+ *
+ * Element.keyDownEvent.forTarget(element, useCapture: true).listen(...);
+ *
+ * Or for listening to an event which will bubble through the DOM tree:
+ *
+ * MediaElement.pauseEvent.forTarget(document.body).listen(...);
+ *
+ * See also:
+ *
+ * [addEventListener](http://docs.webplatform.org/wiki/dom/methods/addEventListener)
+ */
+ Stream<T> forTarget(EventTarget e, {bool useCapture: false}) {
+ return new _HtmlStreamController(e, _eventType, useCapture);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698