| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library event_helper; | 5 library event_helper; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 class Event { | 9 class Event { |
| 10 void replay(StreamSink sink); | 10 void replay(StreamSink sink); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 } | 105 } |
| 106 | 106 |
| 107 // Operations that only work when there is a subscription feeding the Events. | 107 // Operations that only work when there is a subscription feeding the Events. |
| 108 | 108 |
| 109 /** | 109 /** |
| 110 * Pauses the subscription that feeds this [Events]. | 110 * Pauses the subscription that feeds this [Events]. |
| 111 * | 111 * |
| 112 * Should only be used when there is a subscription. That is, after a | 112 * Should only be used when there is a subscription. That is, after a |
| 113 * call to [subscribeTo]. | 113 * call to [subscribeTo]. |
| 114 */ | 114 */ |
| 115 void pause([Signal resumeSignal]) { | 115 void pause([Future resumeSignal]) { |
| 116 throw new StateError("Not capturing events."); | 116 throw new StateError("Not capturing events."); |
| 117 } | 117 } |
| 118 | 118 |
| 119 /** Resumes after a call to [pause]. */ | 119 /** Resumes after a call to [pause]. */ |
| 120 void resume() { | 120 void resume() { |
| 121 throw new StateError("Not capturing events."); | 121 throw new StateError("Not capturing events."); |
| 122 } | 122 } |
| 123 | 123 |
| 124 /** Whether the underlying subscription has been paused. */ | 124 /** Whether the underlying subscription has been paused. */ |
| 125 bool get isPaused => false; | 125 bool get isPaused => false; |
| 126 | 126 |
| 127 /** | 127 /** |
| 128 * Sets an action to be called when this [Events] receives a 'done' event. | 128 * Sets an action to be called when this [Events] receives a 'done' event. |
| 129 */ | 129 */ |
| 130 void onDone(void action()) { | 130 void onDone(void action()) { |
| 131 throw new StateError("Not capturing events."); | 131 throw new StateError("Not capturing events."); |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 | 134 |
| 135 class CaptureEvents extends Events { | 135 class CaptureEvents extends Events { |
| 136 StreamSubscription subscription; | 136 StreamSubscription subscription; |
| 137 SignalCompleter onDoneSignal; | 137 Completer onDoneSignal; |
| 138 bool unsubscribeOnError = false; | 138 bool unsubscribeOnError = false; |
| 139 | 139 |
| 140 CaptureEvents(Stream stream, | 140 CaptureEvents(Stream stream, |
| 141 { bool unsubscribeOnError: false }) | 141 { bool unsubscribeOnError: false }) |
| 142 : onDoneSignal = new SignalCompleter() { | 142 : onDoneSignal = new Completer() { |
| 143 this.unsubscribeOnError = unsubscribeOnError; | 143 this.unsubscribeOnError = unsubscribeOnError; |
| 144 subscription = stream.listen(add, | 144 subscription = stream.listen(add, |
| 145 onError: signalError, | 145 onError: signalError, |
| 146 onDone: close, | 146 onDone: close, |
| 147 unsubscribeOnError: unsubscribeOnError); | 147 unsubscribeOnError: unsubscribeOnError); |
| 148 } | 148 } |
| 149 | 149 |
| 150 void signalError(AsyncError error) { | 150 void signalError(AsyncError error) { |
| 151 super.signalError(error); | 151 super.signalError(error); |
| 152 if (unsubscribeOnError) onDoneSignal.complete(); | 152 if (unsubscribeOnError) onDoneSignal.complete(null); |
| 153 } | 153 } |
| 154 | 154 |
| 155 void close() { | 155 void close() { |
| 156 super.close(); | 156 super.close(); |
| 157 if (onDoneSignal != null) onDoneSignal.complete(); | 157 if (onDoneSignal != null) onDoneSignal.complete(null); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void pause([Signal resumeSignal]) { | 160 void pause([Future resumeSignal]) { |
| 161 subscription.pause(resumeSignal); | 161 subscription.pause(resumeSignal); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void resume() { | 164 void resume() { |
| 165 subscription.resume(); | 165 subscription.resume(); |
| 166 } | 166 } |
| 167 | 167 |
| 168 bool get isPaused => subscription.isPaused; | 168 bool get isPaused => subscription.isPaused; |
| 169 | 169 |
| 170 void onDone(void action()) { | 170 void onDone(void action()) { |
| 171 onDoneSignal.signal.then(action); | 171 onDoneSignal.future.whenComplete(action); |
| 172 } | 172 } |
| 173 } | 173 } |
| OLD | NEW |