| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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; |
| 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; | |
| 65 | 64 |
| 66 Events(); | 65 Events(); |
| 67 | |
| 68 Events.fromIterable(Iterable iterable) { | 66 Events.fromIterable(Iterable iterable) { |
| 69 for (var value in iterable) add(value); | 67 for (var value in iterable) add(value); |
| 70 close(); | 68 close(); |
| 71 } | 69 } |
| 72 | 70 |
| 73 /** Capture events from a stream into a new [Events] object. */ | 71 /** Capture events from a stream into a new [Events] object. */ |
| 74 factory Events.capture(Stream stream, | 72 factory Events.capture(Stream stream, |
| 75 { bool cancelOnError: false }) = CaptureEvents; | 73 { bool cancelOnError: false }) = CaptureEvents; |
| 76 | 74 |
| 77 // EventSink interface. | 75 // EventSink interface. |
| 78 void add(var value) { | 76 void add(var value) { |
| 79 if (trace) print("Events#$hashCode: add($value)"); | |
| 80 events.add(new DataEvent(value)); | 77 events.add(new DataEvent(value)); |
| 81 } | 78 } |
| 82 | 79 |
| 83 void addError(error) { | 80 void addError(error) { |
| 84 if (trace) print("Events#$hashCode: addError($error)"); | |
| 85 events.add(new ErrorEvent(error)); | 81 events.add(new ErrorEvent(error)); |
| 86 } | 82 } |
| 87 | 83 |
| 88 void close() { | 84 void close() { |
| 89 if (trace) print("Events#$hashCode: close()"); | |
| 90 events.add(const DoneEvent()); | 85 events.add(const DoneEvent()); |
| 91 } | 86 } |
| 92 | 87 |
| 93 // Error helper for creating errors manually.. | 88 // Error helper for creating errors manually.. |
| 94 void error(var value) { addError(value); } | 89 void error(var value) { addError(value); } |
| 95 | 90 |
| 96 /** Replay the captured events on a sink. */ | 91 /** Replay the captured events on a sink. */ |
| 97 void replay(EventSink sink) { | 92 void replay(EventSink sink) { |
| 98 for (int i = 0; i < events.length; i++) { | 93 for (int i = 0; i < events.length; i++) { |
| 99 events[i].replay(sink); | 94 events[i].replay(sink); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 super.addError(error); | 150 super.addError(error); |
| 156 if (cancelOnError) onDoneSignal.complete(null); | 151 if (cancelOnError) onDoneSignal.complete(null); |
| 157 } | 152 } |
| 158 | 153 |
| 159 void close() { | 154 void close() { |
| 160 super.close(); | 155 super.close(); |
| 161 if (onDoneSignal != null) onDoneSignal.complete(null); | 156 if (onDoneSignal != null) onDoneSignal.complete(null); |
| 162 } | 157 } |
| 163 | 158 |
| 164 void pause([Future resumeSignal]) { | 159 void pause([Future resumeSignal]) { |
| 165 if (trace) print("Events#$hashCode: pause"); | |
| 166 subscription.pause(resumeSignal); | 160 subscription.pause(resumeSignal); |
| 167 } | 161 } |
| 168 | 162 |
| 169 void resume() { | 163 void resume() { |
| 170 if (trace) print("Events#$hashCode: resume"); | |
| 171 subscription.resume(); | 164 subscription.resume(); |
| 172 } | 165 } |
| 173 | 166 |
| 174 void onDone(void action()) { | 167 void onDone(void action()) { |
| 175 if (trace) print("Events#$hashCode: onDone"); | |
| 176 onDoneSignal.future.whenComplete(action); | 168 onDoneSignal.future.whenComplete(action); |
| 177 } | 169 } |
| 178 } | 170 } |
| OLD | NEW |