OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library event_helper; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 class Event { |
| 10 void replay(StreamSink sink); |
| 11 } |
| 12 |
| 13 class DataEvent implements Event { |
| 14 final data; |
| 15 |
| 16 DataEvent(this.data); |
| 17 |
| 18 void replay(StreamSink sink) { sink.add(data); } |
| 19 |
| 20 int get hashCode => data.hashCode; |
| 21 |
| 22 bool operator==(Object other) { |
| 23 if (other is! DataEvent) return false; |
| 24 DataEvent otherEvent = other; |
| 25 return data == other.data; |
| 26 } |
| 27 |
| 28 String toString() => "DataEvent: $data"; |
| 29 } |
| 30 |
| 31 class ErrorEvent implements Event { |
| 32 final AsyncError error; |
| 33 |
| 34 ErrorEvent(this.error); |
| 35 |
| 36 void replay(StreamSink sink) { sink.signalError(error); } |
| 37 |
| 38 int get hashCode => error.error.hashCode; |
| 39 |
| 40 bool operator==(Object other) { |
| 41 if (other is! ErrorEvent) return false; |
| 42 ErrorEvent otherEvent = other; |
| 43 return error.error == other.error.error; |
| 44 } |
| 45 |
| 46 String toString() => "ErrorEvent: ${error.error}"; |
| 47 } |
| 48 |
| 49 class DoneEvent implements Event { |
| 50 const DoneEvent(); |
| 51 |
| 52 void replay(StreamSink sink) { sink.close(); } |
| 53 |
| 54 int get hashCode => 42; |
| 55 |
| 56 bool operator==(Object other) => other is DoneEvent; |
| 57 |
| 58 String toString() => "DoneEvent"; |
| 59 } |
| 60 |
| 61 /** Collector of events. */ |
| 62 class Events implements StreamSink { |
| 63 final List<Event> events = []; |
| 64 |
| 65 Events(); |
| 66 Events.fromIterable(Iterable iterable) { |
| 67 for (var value in iterable) add(value); |
| 68 close(); |
| 69 } |
| 70 |
| 71 /** Capture events from a stream into a new [Events] object. */ |
| 72 factory Events.capture(Stream stream, |
| 73 { bool unsubscribeOnError: false }) = CaptureEvents; |
| 74 |
| 75 // Sink interface. |
| 76 add(var value) { events.add(new DataEvent(value)); } |
| 77 |
| 78 void signalError(AsyncError error) { |
| 79 events.add(new ErrorEvent(error)); |
| 80 } |
| 81 |
| 82 void close() { |
| 83 events.add(const DoneEvent()); |
| 84 } |
| 85 |
| 86 // Error helper for creating errors manually.. |
| 87 void error(var value) { signalError(new AsyncError(value, null)); } |
| 88 |
| 89 /** Replay the captured events on a sink. */ |
| 90 void replay(StreamSink sink) { |
| 91 for (int i = 0; i < events.length; i++) { |
| 92 events[i].replay(sink); |
| 93 } |
| 94 } |
| 95 |
| 96 /** |
| 97 * Create a new [Events] with the same captured events. |
| 98 * |
| 99 * This does not copy a subscription. |
| 100 */ |
| 101 Events copy() { |
| 102 Events result = new Events(); |
| 103 replay(result); |
| 104 return result; |
| 105 } |
| 106 |
| 107 // Operations that only work when there is a subscription feeding the Events. |
| 108 |
| 109 /** |
| 110 * Pauses the subscription that feeds this [Events]. |
| 111 * |
| 112 * Should only be used when there is a subscription. That is, after a |
| 113 * call to [subscribeTo]. |
| 114 */ |
| 115 void pause([Signal resumeSignal]) { |
| 116 throw new StateError("Not capturing events."); |
| 117 } |
| 118 |
| 119 /** Resumes after a call to [pause]. */ |
| 120 void resume() { |
| 121 throw new StateError("Not capturing events."); |
| 122 } |
| 123 |
| 124 /** Whether the underlying subscription has been paused. */ |
| 125 bool get isPaused => false; |
| 126 |
| 127 /** |
| 128 * Sets an action to be called when this [Events] receives a 'done' event. |
| 129 */ |
| 130 void onDone(void action()) { |
| 131 throw new StateError("Not capturing events."); |
| 132 } |
| 133 } |
| 134 |
| 135 class CaptureEvents extends Events { |
| 136 StreamSubscription subscription; |
| 137 SignalCompleter onDoneSignal; |
| 138 bool unsubscribeOnError = false; |
| 139 |
| 140 CaptureEvents(Stream stream, |
| 141 { bool unsubscribeOnError: false }) |
| 142 : onDoneSignal = new SignalCompleter() { |
| 143 this.unsubscribeOnError = unsubscribeOnError; |
| 144 subscription = stream.listen(add, |
| 145 onError: signalError, |
| 146 onDone: close, |
| 147 unsubscribeOnError: unsubscribeOnError); |
| 148 } |
| 149 |
| 150 void signalError(AsyncError error) { |
| 151 super.signalError(error); |
| 152 if (unsubscribeOnError) onDoneSignal.complete(); |
| 153 } |
| 154 |
| 155 void close() { |
| 156 super.close(); |
| 157 if (onDoneSignal != null) onDoneSignal.complete(); |
| 158 } |
| 159 |
| 160 void pause([Signal resumeSignal]) { |
| 161 subscription.pause(resumeSignal); |
| 162 } |
| 163 |
| 164 void resume() { |
| 165 subscription.resume(); |
| 166 } |
| 167 |
| 168 bool get isPaused => subscription.isPaused; |
| 169 |
| 170 void onDone(void action()) { |
| 171 onDoneSignal.signal.then(action); |
| 172 } |
| 173 } |
OLD | NEW |