| 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 11 matching lines...) Expand all Loading... |
| 22 bool operator==(Object other) { | 22 bool operator==(Object other) { |
| 23 if (other is! DataEvent) return false; | 23 if (other is! DataEvent) return false; |
| 24 DataEvent otherEvent = other; | 24 DataEvent otherEvent = other; |
| 25 return data == other.data; | 25 return data == other.data; |
| 26 } | 26 } |
| 27 | 27 |
| 28 String toString() => "DataEvent: $data"; | 28 String toString() => "DataEvent: $data"; |
| 29 } | 29 } |
| 30 | 30 |
| 31 class ErrorEvent implements Event { | 31 class ErrorEvent implements Event { |
| 32 final AsyncError error; | 32 final error; |
| 33 | 33 |
| 34 ErrorEvent(this.error); | 34 ErrorEvent(this.error); |
| 35 | 35 |
| 36 void replay(EventSink sink) { sink.addError(error); } | 36 void replay(EventSink sink) { sink.addError(error); } |
| 37 | 37 |
| 38 int get hashCode => error.error.hashCode; | 38 int get hashCode => error.error.hashCode; |
| 39 | 39 |
| 40 bool operator==(Object other) { | 40 bool operator==(Object other) { |
| 41 if (other is! ErrorEvent) return false; | 41 if (other is! ErrorEvent) return false; |
| 42 ErrorEvent otherEvent = other; | 42 ErrorEvent otherEvent = other; |
| 43 return error.error == other.error.error; | 43 return error == other.error; |
| 44 } | 44 } |
| 45 | 45 |
| 46 String toString() => "ErrorEvent: ${error.error}"; | 46 String toString() => "ErrorEvent: ${error}"; |
| 47 } | 47 } |
| 48 | 48 |
| 49 class DoneEvent implements Event { | 49 class DoneEvent implements Event { |
| 50 const DoneEvent(); | 50 const DoneEvent(); |
| 51 | 51 |
| 52 void replay(EventSink sink) { sink.close(); } | 52 void replay(EventSink sink) { sink.close(); } |
| 53 | 53 |
| 54 int get hashCode => 42; | 54 int get hashCode => 42; |
| 55 | 55 |
| 56 bool operator==(Object other) => other is DoneEvent; | 56 bool operator==(Object other) => other is DoneEvent; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 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 cancelOnError: 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(error) { |
| 81 events.add(new ErrorEvent(error)); | 81 events.add(new ErrorEvent(error)); |
| 82 } | 82 } |
| 83 | 83 |
| 84 void close() { | 84 void close() { |
| 85 events.add(const DoneEvent()); | 85 events.add(const DoneEvent()); |
| 86 } | 86 } |
| 87 | 87 |
| 88 // Error helper for creating errors manually.. | 88 // Error helper for creating errors manually.. |
| 89 void error(var value) { addError(new AsyncError(value, null)); } | 89 void error(var value) { addError(value); } |
| 90 | 90 |
| 91 /** Replay the captured events on a sink. */ | 91 /** Replay the captured events on a sink. */ |
| 92 void replay(EventSink sink) { | 92 void replay(EventSink sink) { |
| 93 for (int i = 0; i < events.length; i++) { | 93 for (int i = 0; i < events.length; i++) { |
| 94 events[i].replay(sink); | 94 events[i].replay(sink); |
| 95 } | 95 } |
| 96 } | 96 } |
| 97 | 97 |
| 98 /** | 98 /** |
| 99 * Create a new [Events] with the same captured events. | 99 * Create a new [Events] with the same captured events. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 CaptureEvents(Stream stream, | 139 CaptureEvents(Stream stream, |
| 140 { bool cancelOnError: false }) | 140 { bool cancelOnError: false }) |
| 141 : onDoneSignal = new Completer() { | 141 : onDoneSignal = new Completer() { |
| 142 this.cancelOnError = cancelOnError; | 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 cancelOnError: cancelOnError); | 146 cancelOnError: cancelOnError); |
| 147 } | 147 } |
| 148 | 148 |
| 149 void addError(AsyncError error) { | 149 void addError(error) { |
| 150 super.addError(error); | 150 super.addError(error); |
| 151 if (cancelOnError) 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 |