| 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 abstract class Event { | 9 abstract class Event { |
| 10 void replay(EventSink sink); | 10 void replay(EventSink sink); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 final List<Event> events = []; | 63 final List<Event> events = []; |
| 64 | 64 |
| 65 Events(); | 65 Events(); |
| 66 Events.fromIterable(Iterable iterable) { | 66 Events.fromIterable(Iterable iterable) { |
| 67 for (var value in iterable) add(value); | 67 for (var value in iterable) add(value); |
| 68 close(); | 68 close(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 /** Capture events from a stream into a new [Events] object. */ | 71 /** Capture events from a stream into a new [Events] object. */ |
| 72 factory Events.capture(Stream stream, | 72 factory Events.capture(Stream stream, |
| 73 { bool unsubscribeOnError: false }) = CaptureEvents; | 73 { bool cancelOnError: false }) = CaptureEvents; |
| 74 | 74 |
| 75 // EventSink interface. | 75 // EventSink interface. |
| 76 void add(var value) { | 76 void add(var value) { |
| 77 events.add(new DataEvent(value)); | 77 events.add(new DataEvent(value)); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void addError(AsyncError error) { | 80 void addError(AsyncError error) { |
| 81 events.add(new ErrorEvent(error)); | 81 events.add(new ErrorEvent(error)); |
| 82 } | 82 } |
| 83 | 83 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 * Sets an action to be called when this [Events] receives a 'done' event. | 127 * Sets an action to be called when this [Events] receives a 'done' event. |
| 128 */ | 128 */ |
| 129 void onDone(void action()) { | 129 void onDone(void action()) { |
| 130 throw new StateError("Not capturing events."); | 130 throw new StateError("Not capturing events."); |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 | 133 |
| 134 class CaptureEvents extends Events { | 134 class CaptureEvents extends Events { |
| 135 StreamSubscription subscription; | 135 StreamSubscription subscription; |
| 136 Completer onDoneSignal; | 136 Completer onDoneSignal; |
| 137 bool unsubscribeOnError = false; | 137 bool cancelOnError = false; |
| 138 | 138 |
| 139 CaptureEvents(Stream stream, | 139 CaptureEvents(Stream stream, |
| 140 { bool unsubscribeOnError: false }) | 140 { bool cancelOnError: false }) |
| 141 : onDoneSignal = new Completer() { | 141 : onDoneSignal = new Completer() { |
| 142 this.unsubscribeOnError = unsubscribeOnError; | 142 this.cancelOnError = cancelOnError; |
| 143 subscription = stream.listen(add, | 143 subscription = stream.listen(add, |
| 144 onError: addError, | 144 onError: addError, |
| 145 onDone: close, | 145 onDone: close, |
| 146 unsubscribeOnError: unsubscribeOnError); | 146 cancelOnError: cancelOnError); |
| 147 } | 147 } |
| 148 | 148 |
| 149 void addError(AsyncError error) { | 149 void addError(AsyncError error) { |
| 150 super.addError(error); | 150 super.addError(error); |
| 151 if (unsubscribeOnError) onDoneSignal.complete(null); | 151 if (cancelOnError) onDoneSignal.complete(null); |
| 152 } | 152 } |
| 153 | 153 |
| 154 void close() { | 154 void close() { |
| 155 super.close(); | 155 super.close(); |
| 156 if (onDoneSignal != null) onDoneSignal.complete(null); | 156 if (onDoneSignal != null) onDoneSignal.complete(null); |
| 157 } | 157 } |
| 158 | 158 |
| 159 void pause([Future resumeSignal]) { | 159 void pause([Future resumeSignal]) { |
| 160 subscription.pause(resumeSignal); | 160 subscription.pause(resumeSignal); |
| 161 } | 161 } |
| 162 | 162 |
| 163 void resume() { | 163 void resume() { |
| 164 subscription.resume(); | 164 subscription.resume(); |
| 165 } | 165 } |
| 166 | 166 |
| 167 void onDone(void action()) { | 167 void onDone(void action()) { |
| 168 onDoneSignal.future.whenComplete(action); | 168 onDoneSignal.future.whenComplete(action); |
| 169 } | 169 } |
| 170 } | 170 } |
| OLD | NEW |