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

Side by Side Diff: sdk/lib/html/dart2js/html_dart2js.dart

Issue 16123029: Switching DOM events over to using StreamController. Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/html/dartium/html_dartium.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /// The Dart HTML library. 1 /// The Dart HTML library.
2 library dart.dom.html; 2 library dart.dom.html;
3 3
4 import 'dart:async'; 4 import 'dart:async';
5 import 'dart:collection'; 5 import 'dart:collection';
6 import 'dart:_collection-dev' hide Symbol; 6 import 'dart:_collection-dev' hide Symbol;
7 import 'dart:html_common'; 7 import 'dart:html_common';
8 import 'dart:indexed_db'; 8 import 'dart:indexed_db';
9 import 'dart:isolate'; 9 import 'dart:isolate';
10 import 'dart:json' as json; 10 import 'dart:json' as json;
(...skipping 26039 matching lines...) Expand 10 before | Expand all | Expand 10 after
26050 // for details. All rights reserved. Use of this source code is governed by a 26050 // for details. All rights reserved. Use of this source code is governed by a
26051 // BSD-style license that can be found in the LICENSE file. 26051 // BSD-style license that can be found in the LICENSE file.
26052 26052
26053 26053
26054 typedef EventListener(Event event); 26054 typedef EventListener(Event event);
26055 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 26055 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
26056 // for details. All rights reserved. Use of this source code is governed by a 26056 // for details. All rights reserved. Use of this source code is governed by a
26057 // BSD-style license that can be found in the LICENSE file. 26057 // BSD-style license that can be found in the LICENSE file.
26058 26058
26059 26059
26060 /**
26061 * Adapter for exposing DOM events as Dart streams.
26062 */
26063 class _EventStream<T extends Event> extends Stream<T> {
26064 final EventTarget _target;
26065 final String _eventType;
26066 final bool _useCapture;
26067
26068 _EventStream(this._target, this._eventType, this._useCapture);
26069
26070 // DOM events are inherently multi-subscribers.
26071 Stream<T> asBroadcastStream() => this;
26072 bool get isBroadcast => true;
26073
26074 StreamSubscription<T> listen(void onData(T event),
26075 { void onError(error),
26076 void onDone(),
26077 bool cancelOnError}) {
26078
26079 return new _EventStreamSubscription<T>(
26080 this._target, this._eventType, onData, this._useCapture);
26081 }
26082 }
26083
26084 class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> {
26085 int _pauseCount = 0;
26086 EventTarget _target;
26087 final String _eventType;
26088 var _onData;
26089 final bool _useCapture;
26090
26091 _EventStreamSubscription(this._target, this._eventType, this._onData,
26092 this._useCapture) {
26093 _tryResume();
26094 }
26095
26096 void cancel() {
26097 if (_canceled) return;
26098
26099 _unlisten();
26100 // Clear out the target to indicate this is complete.
26101 _target = null;
26102 _onData = null;
26103 }
26104
26105 bool get _canceled => _target == null;
26106
26107 void onData(void handleData(T event)) {
26108 if (_canceled) {
26109 throw new StateError("Subscription has been canceled.");
26110 }
26111 // Remove current event listener.
26112 _unlisten();
26113
26114 _onData = handleData;
26115 _tryResume();
26116 }
26117
26118 /// Has no effect.
26119 void onError(void handleError(error)) {}
26120
26121 /// Has no effect.
26122 void onDone(void handleDone()) {}
26123
26124 void pause([Future resumeSignal]) {
26125 if (_canceled) return;
26126 ++_pauseCount;
26127 _unlisten();
26128
26129 if (resumeSignal != null) {
26130 resumeSignal.whenComplete(resume);
26131 }
26132 }
26133
26134 bool get isPaused => _pauseCount > 0;
26135
26136 void resume() {
26137 if (_canceled || !isPaused) return;
26138 --_pauseCount;
26139 _tryResume();
26140 }
26141
26142 void _tryResume() {
26143 if (_onData != null && !isPaused) {
26144 _target.$dom_addEventListener(_eventType, _onData, _useCapture);
26145 }
26146 }
26147
26148 void _unlisten() {
26149 if (_onData != null) {
26150 _target.$dom_removeEventListener(_eventType, _onData, _useCapture);
26151 }
26152 }
26153
26154 Future asFuture([var futureValue]) {
26155 // We just need a future that will never succeed or fail.
26156 Completer completer = new Completer();
26157 return completer.future;
26158 }
26159 }
26160
26161 26060
26162 /** 26061 /**
26163 * A factory to expose DOM events as Streams. 26062 * A factory to expose DOM events as Streams.
26164 */ 26063 */
26165 class EventStreamProvider<T extends Event> { 26064 class EventStreamProvider<T extends Event> {
26166 final String _eventType; 26065 final String _eventType;
26167 26066
26168 const EventStreamProvider(this._eventType); 26067 const EventStreamProvider(this._eventType);
26169 26068
26170 /** 26069 /**
26171 * Gets a [Stream] for this event type, on the specified target. 26070 * Gets a [Stream] for this event type, on the specified target.
26172 * 26071 *
26173 * This will always return a broadcast stream so multiple listeners can be 26072 * This will always return a broadcast stream so multiple listeners can be
26174 * used simultaneously. 26073 * used simultaneously.
26175 * 26074 *
26176 * This may be used to capture DOM events: 26075 * This may be used to capture DOM events:
26177 * 26076 *
26178 * Element.keyDownEvent.forTarget(element, useCapture: true).listen(...); 26077 * Element.keyDownEvent.forTarget(element, useCapture: true).listen(...);
26179 * 26078 *
26180 * Or for listening to an event which will bubble through the DOM tree: 26079 * Or for listening to an event which will bubble through the DOM tree:
26181 * 26080 *
26182 * MediaElement.pauseEvent.forTarget(document.body).listen(...); 26081 * MediaElement.pauseEvent.forTarget(document.body).listen(...);
26183 * 26082 *
26184 * See also: 26083 * See also:
26185 * 26084 *
26186 * [addEventListener](http://docs.webplatform.org/wiki/dom/methods/addEventLis tener) 26085 * [addEventListener](http://docs.webplatform.org/wiki/dom/methods/addEventLis tener)
26187 */ 26086 */
26188 Stream<T> forTarget(EventTarget e, {bool useCapture: false}) { 26087 Stream<T> forTarget(EventTarget e, {bool useCapture: false}) {
26189 return new _EventStream(e, _eventType, useCapture); 26088 var controller;
26089
26090 void onData(data) {
26091 controller.add(data);
26092 }
26093
26094 controller = new StreamController<T>(
26095 onListen: () {
26096 e.$dom_addEventListener(_eventType, onData, useCapture);
26097 },
26098 onCancel: () {
26099 e.$dom_removeEventListener(_eventType, onData, useCapture);
26100 },
26101 sync: true);
26102
26103 return controller.stream;
26190 } 26104 }
26191 26105
26192 /** 26106 /**
26193 * Gets the type of the event which this would listen for on the specified 26107 * Gets the type of the event which this would listen for on the specified
26194 * event target. 26108 * event target.
26195 * 26109 *
26196 * The target is necessary because some browsers may use different event names 26110 * The target is necessary because some browsers may use different event names
26197 * for the same purpose and the target allows differentiating browser support. 26111 * for the same purpose and the target allows differentiating browser support.
26198 */ 26112 */
26199 String getEventType(EventTarget target) { 26113 String getEventType(EventTarget target) {
26200 return _eventType; 26114 return _eventType;
26201 } 26115 }
26202 } 26116 }
26203 26117
26204 /** 26118 /**
26205 * A factory to expose DOM events as streams, where the DOM event name has to 26119 * A factory to expose DOM events as streams, where the DOM event name has to
26206 * be determined on the fly (for example, mouse wheel events). 26120 * be determined on the fly (for example, mouse wheel events).
26207 */ 26121 */
26208 class _CustomEventStreamProvider<T extends Event> 26122 class _CustomEventStreamProvider<T extends Event>
26209 implements EventStreamProvider<T> { 26123 implements EventStreamProvider<T> {
26210 26124
26211 final _eventTypeGetter; 26125 final _eventTypeGetter;
26212 const _CustomEventStreamProvider(this._eventTypeGetter); 26126 const _CustomEventStreamProvider(this._eventTypeGetter);
26213 26127
26214 Stream<T> forTarget(EventTarget e, {bool useCapture: false}) { 26128 Stream<T> forTarget(EventTarget e, {bool useCapture: false}) {
26215 return new _EventStream(e, _eventTypeGetter(e), useCapture); 26129 var controller;
26130
26131 var eventType = _eventTypeGetter(e);
26132
26133 void onData(data) {
26134 controller.add(data);
26135 }
26136
26137 controller = new StreamController<T>(
26138 onListen: () {
26139 e.$dom_addEventListener(eventType, onData, useCapture);
26140 },
26141 onCancel: () {
26142 e.$dom_removeEventListener(eventType, onData, useCapture);
26143 },
26144 sync: true);
26145
26146 return controller.stream;
26216 } 26147 }
26217 26148
26218 String getEventType(EventTarget target) { 26149 String getEventType(EventTarget target) {
26219 return _eventTypeGetter(target); 26150 return _eventTypeGetter(target);
26220 } 26151 }
26221 } 26152 }
26222 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 26153 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
26223 // for details. All rights reserved. Use of this source code is governed by a 26154 // for details. All rights reserved. Use of this source code is governed by a
26224 // BSD-style license that can be found in the LICENSE file. 26155 // BSD-style license that can be found in the LICENSE file.
26225 26156
(...skipping 3660 matching lines...) Expand 10 before | Expand all | Expand 10 after
29886 _position = nextPosition; 29817 _position = nextPosition;
29887 return true; 29818 return true;
29888 } 29819 }
29889 _current = null; 29820 _current = null;
29890 _position = _array.length; 29821 _position = _array.length;
29891 return false; 29822 return false;
29892 } 29823 }
29893 29824
29894 T get current => _current; 29825 T get current => _current;
29895 } 29826 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/html/dartium/html_dartium.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698