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 _EventStream<T extends Event> extends Stream<T> { |
| 11 final EventTarget _target; |
| 12 final String _eventType; |
| 13 final bool _useCapture; |
| 14 |
| 15 _EventStream(this._target, this._eventType, this._useCapture); |
| 16 |
| 17 // DOM events are inherently multi-subscribers. |
| 18 Stream<T> asMultiSubscriberStream() => this; |
| 19 |
| 20 StreamSubscription<T> listen(void onData(T event), |
| 21 { void onError(AsyncError error), |
| 22 void onDone(), |
| 23 bool unsubscribeOnError}) { |
| 24 |
| 25 return new _EventStreamSubscription<T>( |
| 26 this._target, this._eventType, onData, this._useCapture); |
| 27 } |
| 28 } |
| 29 |
| 30 class _EventStreamSubscription<T extends Event> extends StreamSubscription<T> { |
| 31 int _pauseCount = 0; |
| 32 EventTarget _target; |
| 33 final String _eventType; |
| 34 var _onData; |
| 35 final bool _useCapture; |
| 36 |
| 37 _EventStreamSubscription(this._target, this._eventType, this._onData, |
| 38 this._useCapture) { |
| 39 _tryResume(); |
| 40 } |
| 41 |
| 42 void cancel() { |
| 43 if (_canceled) { |
| 44 throw new StateError("Subscription has been canceled."); |
| 45 } |
| 46 |
| 47 _unlisten(); |
| 48 // Clear out the target to indicate this is complete. |
| 49 _target = null; |
| 50 _onData = null; |
| 51 } |
| 52 |
| 53 bool get _canceled => _target == null; |
| 54 |
| 55 void onData(void handleData(T event)) { |
| 56 if (_canceled) { |
| 57 throw new StateError("Subscription has been canceled."); |
| 58 } |
| 59 // Remove current event listener. |
| 60 _unlisten(); |
| 61 |
| 62 _onData = handleData; |
| 63 _tryResume(); |
| 64 } |
| 65 |
| 66 /// Has no effect. |
| 67 void onError(void handleError(AsyncError error)) {} |
| 68 |
| 69 /// Has no effect. |
| 70 void onDone(void handleDone()) {} |
| 71 |
| 72 void pause([Future resumeSignal]) { |
| 73 if (_canceled) { |
| 74 throw new StateError("Subscription has been canceled."); |
| 75 } |
| 76 ++_pauseCount; |
| 77 _unlisten(); |
| 78 |
| 79 if (resumeSignal != null) { |
| 80 resumeSignal.whenComplete(resume); |
| 81 } |
| 82 } |
| 83 |
| 84 bool get _paused => _pauseCount > 0; |
| 85 |
| 86 void resume() { |
| 87 if (_canceled) { |
| 88 throw new StateError("Subscription has been canceled."); |
| 89 } |
| 90 if (!_paused) { |
| 91 throw new StateError("Subscription is not paused."); |
| 92 } |
| 93 --_pauseCount; |
| 94 _tryResume(); |
| 95 } |
| 96 |
| 97 void _tryResume() { |
| 98 if (_onData != null && !_paused) { |
| 99 _target.$dom_addEventListener(_eventType, _onData, _useCapture); |
| 100 } |
| 101 } |
| 102 |
| 103 void _unlisten() { |
| 104 if (_onData != null) { |
| 105 _target.$dom_removeEventListener(_eventType, _onData, _useCapture); |
| 106 } |
| 107 } |
| 108 } |
| 109 |
| 110 |
| 111 /** |
| 112 * A factory to expose DOM events as Streams. |
| 113 */ |
| 114 class EventStreamProvider<T extends Event> { |
| 115 final String _eventType; |
| 116 |
| 117 const EventStreamProvider(this._eventType); |
| 118 |
| 119 /** |
| 120 * Gets a [Stream] for this event type, on the specified target. |
| 121 * |
| 122 * This may be used to capture DOM events: |
| 123 * |
| 124 * Element.keyDownEvent.forTarget(element, useCapture: true).listen(...); |
| 125 * |
| 126 * Or for listening to an event which will bubble through the DOM tree: |
| 127 * |
| 128 * MediaElement.pauseEvent.forTarget(document.body).listen(...); |
| 129 * |
| 130 * See also: |
| 131 * |
| 132 * [addEventListener](http://docs.webplatform.org/wiki/dom/methods/addEventLis
tener) |
| 133 */ |
| 134 Stream<T> forTarget(EventTarget e, {bool useCapture: false}) { |
| 135 return new _EventStream(e, _eventType, useCapture); |
| 136 } |
| 137 } |
OLD | NEW |