Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: tests/lib/async/event_helper.dart

Issue 12610006: Renamed StreamSink to EventSink. Renamed signalError to addError. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Changed inheritance back! Now create StreamSink instead of EventSink where we create them. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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(StreamSink sink); 10 void replay(EventSink sink);
11 } 11 }
12 12
13 class DataEvent implements Event { 13 class DataEvent implements Event {
14 final data; 14 final data;
15 15
16 DataEvent(this.data); 16 DataEvent(this.data);
17 17
18 void replay(StreamSink sink) { sink.add(data); } 18 void replay(EventSink sink) { sink.add(data); }
19 19
20 int get hashCode => data.hashCode; 20 int get hashCode => data.hashCode;
21 21
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 AsyncError error;
33 33
34 ErrorEvent(this.error); 34 ErrorEvent(this.error);
35 35
36 void replay(StreamSink sink) { sink.signalError(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.error == other.error.error;
44 } 44 }
45 45
46 String toString() => "ErrorEvent: ${error.error}"; 46 String toString() => "ErrorEvent: ${error.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(StreamSink 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;
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 StreamSink { 62 class Events implements EventSink {
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 unsubscribeOnError: false }) = CaptureEvents;
74 74
75 // StreamSink interface. 75 // EventSink interface.
76 add(var value) { events.add(new DataEvent(value)); } 76 void add(var value) {
77 events.add(new DataEvent(value));
78 }
77 79
78 void signalError(AsyncError error) { 80 void addError(AsyncError error) {
79 events.add(new ErrorEvent(error)); 81 events.add(new ErrorEvent(error));
80 } 82 }
81 83
82 void close() { 84 void close() {
83 events.add(const DoneEvent()); 85 events.add(const DoneEvent());
84 } 86 }
85 87
86 // Error helper for creating errors manually.. 88 // Error helper for creating errors manually..
87 void error(var value) { signalError(new AsyncError(value, null)); } 89 void error(var value) { addError(new AsyncError(value, null)); }
88 90
89 /** Replay the captured events on a sink. */ 91 /** Replay the captured events on a sink. */
90 void replay(StreamSink sink) { 92 void replay(EventSink sink) {
91 for (int i = 0; i < events.length; i++) { 93 for (int i = 0; i < events.length; i++) {
92 events[i].replay(sink); 94 events[i].replay(sink);
93 } 95 }
94 } 96 }
95 97
96 /** 98 /**
97 * Create a new [Events] with the same captured events. 99 * Create a new [Events] with the same captured events.
98 * 100 *
99 * This does not copy a subscription. 101 * This does not copy a subscription.
100 */ 102 */
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 class CaptureEvents extends Events { 134 class CaptureEvents extends Events {
133 StreamSubscription subscription; 135 StreamSubscription subscription;
134 Completer onDoneSignal; 136 Completer onDoneSignal;
135 bool unsubscribeOnError = false; 137 bool unsubscribeOnError = false;
136 138
137 CaptureEvents(Stream stream, 139 CaptureEvents(Stream stream,
138 { bool unsubscribeOnError: false }) 140 { bool unsubscribeOnError: false })
139 : onDoneSignal = new Completer() { 141 : onDoneSignal = new Completer() {
140 this.unsubscribeOnError = unsubscribeOnError; 142 this.unsubscribeOnError = unsubscribeOnError;
141 subscription = stream.listen(add, 143 subscription = stream.listen(add,
142 onError: signalError, 144 onError: addError,
143 onDone: close, 145 onDone: close,
144 unsubscribeOnError: unsubscribeOnError); 146 unsubscribeOnError: unsubscribeOnError);
145 } 147 }
146 148
147 void signalError(AsyncError error) { 149 void addError(AsyncError error) {
148 super.signalError(error); 150 super.addError(error);
149 if (unsubscribeOnError) onDoneSignal.complete(null); 151 if (unsubscribeOnError) onDoneSignal.complete(null);
150 } 152 }
151 153
152 void close() { 154 void close() {
153 super.close(); 155 super.close();
154 if (onDoneSignal != null) onDoneSignal.complete(null); 156 if (onDoneSignal != null) onDoneSignal.complete(null);
155 } 157 }
156 158
157 void pause([Future resumeSignal]) { 159 void pause([Future resumeSignal]) {
158 subscription.pause(resumeSignal); 160 subscription.pause(resumeSignal);
159 } 161 }
160 162
161 void resume() { 163 void resume() {
162 subscription.resume(); 164 subscription.resume();
163 } 165 }
164 166
165 void onDone(void action()) { 167 void onDone(void action()) {
166 onDoneSignal.future.whenComplete(action); 168 onDoneSignal.future.whenComplete(action);
167 } 169 }
168 } 170 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698