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

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

Issue 2802973005: Migrate async tests to strong (Closed)
Patch Set: Created 3 years, 8 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
OLDNEW
(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 abstract class Event {
10 void replay(EventSink sink);
11 }
12
13 class DataEvent implements Event {
14 final data;
15
16 DataEvent(this.data);
17
18 void replay(EventSink 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 == otherEvent.data;
26 }
27
28 String toString() => "DataEvent: $data";
29 }
30
31 class ErrorEvent implements Event {
32 final error;
33
34 ErrorEvent(this.error);
35
36 void replay(EventSink sink) { sink.addError(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 == otherEvent.error;
44 }
45
46 String toString() => "ErrorEvent: ${error}";
47 }
48
49 class DoneEvent implements Event {
50 const DoneEvent();
51
52 void replay(EventSink 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 EventSink {
63 final List<Event> events = [];
64 bool trace = false;
65 Completer onDoneSignal = new Completer();
66
67 Events();
68
69 Events.fromIterable(Iterable iterable) {
70 for (var value in iterable) add(value);
71 close();
72 }
73
74 /** Capture events from a stream into a new [Events] object. */
75 factory Events.capture(Stream stream,
76 { bool cancelOnError }) = CaptureEvents;
77
78 // EventSink interface.
79 void add(var value) {
80 if (trace) print("Events#$hashCode: add($value)");
81 events.add(new DataEvent(value));
82 }
83
84 void addError(error, [StackTrace stackTrace]) {
85 if (trace) print("Events#$hashCode: addError($error)");
86 events.add(new ErrorEvent(error));
87 }
88
89 void close() {
90 if (trace) print("Events#$hashCode: close()");
91 events.add(const DoneEvent());
92 onDoneSignal.complete();
93 }
94
95 /**
96 * Error shorthand, for writing events manually.
97 */
98 void error(var value, [StackTrace stackTrace]) {
99 addError(value, stackTrace);
100 }
101
102 /** Replay the captured events on a sink. */
103 void replay(EventSink sink) {
104 for (int i = 0; i < events.length; i++) {
105 events[i].replay(sink);
106 }
107 }
108
109 /**
110 * Create a new [Events] with the same captured events.
111 *
112 * This does not copy a subscription.
113 */
114 Events copy() {
115 Events result = new Events();
116 replay(result);
117 return result;
118 }
119
120 // Operations that only work when there is a subscription feeding the Events.
121
122 /**
123 * Pauses the subscription that feeds this [Events].
124 *
125 * Should only be used when there is a subscription. That is, after a
126 * call to [subscribeTo].
127 */
128 void pause([Future resumeSignal]) {
129 throw new StateError("Not capturing events.");
130 }
131
132 /** Resumes after a call to [pause]. */
133 void resume() {
134 throw new StateError("Not capturing events.");
135 }
136
137 /**
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.
142 */
143 void onDone(void action()) {
144 onDoneSignal.future.whenComplete(action);
145 }
146 }
147
148 class CaptureEvents extends Events {
149 StreamSubscription subscription;
150 bool cancelOnError = false;
151
152 CaptureEvents(Stream stream,
153 { bool cancelOnError: false }) {
154 this.cancelOnError = cancelOnError;
155 subscription = stream.listen(add,
156 onError: addError,
157 onDone: close,
158 cancelOnError: cancelOnError);
159 }
160
161 void addError(error, [stackTrace]) {
162 super.addError(error, stackTrace);
163 if (cancelOnError) {
164 onDoneSignal.complete();
165 }
166 }
167
168 void pause([Future resumeSignal]) {
169 if (trace) print("Events#$hashCode: pause");
170 subscription.pause(resumeSignal);
171 }
172
173 void resume() {
174 if (trace) print("Events#$hashCode: resume");
175 subscription.resume();
176 }
177
178 void onDone(void action()) {
179 if (trace) print("Events#$hashCode: onDone");
180 super.onDone(action);
181 }
182 }
OLDNEW
« no previous file with comments | « tests/lib_strong/async/dart2js_uncaught_error_test.dart ('k') | tests/lib_strong/async/first_regression_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698