| 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 55 |
| 56 bool operator==(Object other) => other is DoneEvent; | 56 bool operator==(Object other) => other is DoneEvent; |
| 57 | 57 |
| 58 String toString() => "DoneEvent"; | 58 String toString() => "DoneEvent"; |
| 59 } | 59 } |
| 60 | 60 |
| 61 /** Collector of events. */ | 61 /** Collector of events. */ |
| 62 class Events implements EventSink { | 62 class Events implements EventSink { |
| 63 final List<Event> events = []; | 63 final List<Event> events = []; |
| 64 bool trace = false; | 64 bool trace = false; |
| 65 Completer onDoneSignal = new Completer(); |
| 65 | 66 |
| 66 Events(); | 67 Events(); |
| 67 | 68 |
| 68 Events.fromIterable(Iterable iterable) { | 69 Events.fromIterable(Iterable iterable) { |
| 69 for (var value in iterable) add(value); | 70 for (var value in iterable) add(value); |
| 70 close(); | 71 close(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 /** Capture events from a stream into a new [Events] object. */ | 74 /** Capture events from a stream into a new [Events] object. */ |
| 74 factory Events.capture(Stream stream, | 75 factory Events.capture(Stream stream, |
| 75 { bool cancelOnError: false }) = CaptureEvents; | 76 { bool cancelOnError: false }) = CaptureEvents; |
| 76 | 77 |
| 77 // EventSink interface. | 78 // EventSink interface. |
| 78 void add(var value) { | 79 void add(var value) { |
| 79 if (trace) print("Events#$hashCode: add($value)"); | 80 if (trace) print("Events#$hashCode: add($value)"); |
| 80 events.add(new DataEvent(value)); | 81 events.add(new DataEvent(value)); |
| 81 } | 82 } |
| 82 | 83 |
| 83 void addError(error, [StackTrace stackTrace]) { | 84 void addError(error, [StackTrace stackTrace]) { |
| 84 if (trace) print("Events#$hashCode: addError($error)"); | 85 if (trace) print("Events#$hashCode: addError($error)"); |
| 85 events.add(new ErrorEvent(error)); | 86 events.add(new ErrorEvent(error)); |
| 86 } | 87 } |
| 87 | 88 |
| 88 void close() { | 89 void close() { |
| 89 if (trace) print("Events#$hashCode: close()"); | 90 if (trace) print("Events#$hashCode: close()"); |
| 90 events.add(const DoneEvent()); | 91 events.add(const DoneEvent()); |
| 92 onDoneSignal.complete(); |
| 91 } | 93 } |
| 92 | 94 |
| 93 // Error helper for creating errors manually.. | 95 /** |
| 96 * Error shorthand, for writing events manually. |
| 97 */ |
| 94 void error(var value, [StackTrace stackTrace]) { | 98 void error(var value, [StackTrace stackTrace]) { |
| 95 addError(value, stackTrace); | 99 addError(value, stackTrace); |
| 96 } | 100 } |
| 97 | 101 |
| 98 /** Replay the captured events on a sink. */ | 102 /** Replay the captured events on a sink. */ |
| 99 void replay(EventSink sink) { | 103 void replay(EventSink sink) { |
| 100 for (int i = 0; i < events.length; i++) { | 104 for (int i = 0; i < events.length; i++) { |
| 101 events[i].replay(sink); | 105 events[i].replay(sink); |
| 102 } | 106 } |
| 103 } | 107 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 125 throw new StateError("Not capturing events."); | 129 throw new StateError("Not capturing events."); |
| 126 } | 130 } |
| 127 | 131 |
| 128 /** Resumes after a call to [pause]. */ | 132 /** Resumes after a call to [pause]. */ |
| 129 void resume() { | 133 void resume() { |
| 130 throw new StateError("Not capturing events."); | 134 throw new StateError("Not capturing events."); |
| 131 } | 135 } |
| 132 | 136 |
| 133 /** | 137 /** |
| 134 * Sets an action to be called when this [Events] receives a 'done' event. | 138 * Sets an action to be called when this [Events] receives a 'done' event. |
| 139 * |
| 140 * The action will also be called if capturing events from a stream with |
| 141 * `cancelOnError` set to true and receiving an error. |
| 135 */ | 142 */ |
| 136 void onDone(void action()) { | 143 void onDone(void action()) { |
| 137 throw new StateError("Not capturing events."); | 144 onDoneSignal.future.whenComplete(action); |
| 138 } | 145 } |
| 139 } | 146 } |
| 140 | 147 |
| 141 class CaptureEvents extends Events { | 148 class CaptureEvents extends Events { |
| 142 StreamSubscription subscription; | 149 StreamSubscription subscription; |
| 143 Completer onDoneSignal; | |
| 144 bool cancelOnError = false; | 150 bool cancelOnError = false; |
| 145 | 151 |
| 146 CaptureEvents(Stream stream, | 152 CaptureEvents(Stream stream, |
| 147 { bool cancelOnError: false }) | 153 { bool cancelOnError: false }) { |
| 148 : onDoneSignal = new Completer() { | |
| 149 this.cancelOnError = cancelOnError; | 154 this.cancelOnError = cancelOnError; |
| 150 subscription = stream.listen(add, | 155 subscription = stream.listen(add, |
| 151 onError: addError, | 156 onError: addError, |
| 152 onDone: close, | 157 onDone: close, |
| 153 cancelOnError: cancelOnError); | 158 cancelOnError: cancelOnError); |
| 154 } | 159 } |
| 155 | 160 |
| 156 void addError(error) { | 161 void addError(error) { |
| 157 super.addError(error); | 162 super.addError(error); |
| 158 if (cancelOnError) onDoneSignal.complete(null); | 163 if (cancelOnError) { |
| 159 } | 164 onDoneSignal.complete(); |
| 160 | 165 } |
| 161 void close() { | |
| 162 super.close(); | |
| 163 if (onDoneSignal != null) onDoneSignal.complete(null); | |
| 164 } | 166 } |
| 165 | 167 |
| 166 void pause([Future resumeSignal]) { | 168 void pause([Future resumeSignal]) { |
| 167 if (trace) print("Events#$hashCode: pause"); | 169 if (trace) print("Events#$hashCode: pause"); |
| 168 subscription.pause(resumeSignal); | 170 subscription.pause(resumeSignal); |
| 169 } | 171 } |
| 170 | 172 |
| 171 void resume() { | 173 void resume() { |
| 172 if (trace) print("Events#$hashCode: resume"); | 174 if (trace) print("Events#$hashCode: resume"); |
| 173 subscription.resume(); | 175 subscription.resume(); |
| 174 } | 176 } |
| 175 | 177 |
| 176 void onDone(void action()) { | 178 void onDone(void action()) { |
| 177 if (trace) print("Events#$hashCode: onDone"); | 179 if (trace) print("Events#$hashCode: onDone"); |
| 178 onDoneSignal.future.whenComplete(action); | 180 super.onDone(action); |
| 179 } | 181 } |
| 180 } | 182 } |
| OLD | NEW |