OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 import "dart:async"; | 5 import "dart:async"; |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 import "package:async_helper/async_helper.dart"; | 7 import "package:async_helper/async_helper.dart"; |
8 | 8 |
9 class Trace { | 9 class Trace { |
10 String trace = ""; | 10 String trace = ""; |
11 record(x) { | 11 record(x) { |
12 trace += x.toString(); | 12 trace += x.toString(); |
13 } | 13 } |
14 toString() => trace; | 14 toString() => trace; |
15 } | 15 } |
16 | 16 |
17 | 17 |
18 Stream makeMeAStream() { | 18 Stream makeMeAStream() { |
19 return timedCounter(5); | 19 return timedCounter(5); |
20 } | 20 } |
21 | 21 |
22 Trace t1 = new Trace(); | 22 Trace t1 = new Trace(); |
23 | 23 |
24 consumeOne() async { | 24 consumeOne() async { |
25 // Equivalent to await for (x in makeMeAStream()) { ... } | 25 // Equivalent to await for (x in makeMeAStream()) { ... } |
26 var s = makeMeAStream(); | 26 var s = makeMeAStream(); |
27 var it = new StreamIterator(s); | 27 var it = new StreamIterator(s); |
28 while (await it.moveNext()) { | 28 while (await (it.moveNext())) { |
floitsch
2015/02/02 22:00:12
why this change?
sigurdm
2015/02/03 16:59:33
This is because of Issue 22183. Hopefully it will
| |
29 var x = it.current; | 29 var x = it.current; |
30 t1.record(x); | 30 t1.record(x); |
31 } | 31 } |
32 t1.record("X"); | 32 t1.record("X"); |
33 } | 33 } |
34 | 34 |
35 Trace t2 = new Trace(); | 35 Trace t2 = new Trace(); |
36 | 36 |
37 consumeTwo() async { | 37 consumeTwo() async { |
38 await for (var x in makeMeAStream()) { | 38 await for (var x in makeMeAStream()) { |
39 t2.record(x); | 39 t2.record(x); |
40 } | 40 } |
41 t2.record("X"); | 41 t2.record("Y"); |
42 } | |
43 | |
44 Trace t3 = new Trace(); | |
45 | |
46 consumeNested() async { | |
47 await for (var x in makeMeAStream()) { | |
48 t3.record(x); | |
49 await for (var y in makeMeAStream()) { | |
50 t3.record(y); | |
51 } | |
52 t3.record("|"); | |
53 } | |
54 t3.record("Z"); | |
55 } | |
56 | |
57 Trace t4 = new Trace(); | |
58 | |
59 consumeSomeOfInfinite() async { | |
60 int i = 0; | |
61 await for (var x in infiniteStream()) { | |
62 i++; | |
63 if (i > 10) break; | |
64 t4.record(x); | |
65 } | |
66 t4.record("U"); | |
42 } | 67 } |
43 | 68 |
44 main() { | 69 main() { |
45 var f1 = consumeOne(); | 70 var f1 = consumeOne(); |
46 t1.record("T1:"); | 71 t1.record("T1:"); |
47 | 72 |
48 var f2 = consumeTwo(); | 73 var f2 = consumeTwo(); |
49 t2.record("T2:"); | 74 t2.record("T2:"); |
75 | |
76 var f3 = consumeNested(); | |
77 t3.record("T3:"); | |
78 | |
79 var f4 = consumeSomeOfInfinite(); | |
80 t4.record("T4:"); | |
50 | 81 |
51 asyncStart(); | 82 asyncStart(); |
52 Future.wait([f1, f2]).then((_) { | 83 Future.wait([f1, f2, f3, f4]).then((_) { |
53 print("Trace 1: $t1"); | |
54 print("Trace 2: $t2"); | |
55 Expect.equals("T1:12345X", t1.toString()); | 84 Expect.equals("T1:12345X", t1.toString()); |
56 Expect.equals("T2:12345X", t2.toString()); | 85 Expect.equals("T2:12345Y", t2.toString()); |
86 Expect.equals("T3:112345|212345|312345|412345|512345|Z", t3.toString()); | |
87 Expect.equals("T4:12345678910U", t4.toString()); | |
57 asyncEnd(); | 88 asyncEnd(); |
58 }); | 89 }); |
59 } | 90 } |
60 | 91 |
61 | |
62 // Create a stream that produces numbers [1, 2, ... maxCount] | 92 // Create a stream that produces numbers [1, 2, ... maxCount] |
63 Stream timedCounter(int maxCount) { | 93 Stream timedCounter(int maxCount) { |
64 StreamController controller; | 94 StreamController controller; |
65 Timer timer; | 95 Timer timer; |
66 int counter = 0; | 96 int counter = 0; |
67 | 97 |
68 void tick(_) { | 98 void tick(_) { |
69 counter++; | 99 counter++; |
70 controller.add(counter); // Ask stream to send counter values as event. | 100 controller.add(counter); // Ask stream to send counter values as event. |
71 if (counter >= maxCount) { | 101 if (counter >= maxCount) { |
(...skipping 14 matching lines...) Expand all Loading... | |
86 } | 116 } |
87 | 117 |
88 controller = new StreamController( | 118 controller = new StreamController( |
89 onListen: startTimer, | 119 onListen: startTimer, |
90 onPause: stopTimer, | 120 onPause: stopTimer, |
91 onResume: startTimer, | 121 onResume: startTimer, |
92 onCancel: stopTimer); | 122 onCancel: stopTimer); |
93 | 123 |
94 return controller.stream; | 124 return controller.stream; |
95 } | 125 } |
126 | |
127 // Create a stream that produces numbers [1, 2, ... ] | |
128 Stream infiniteStream() { | |
129 StreamController controller; | |
130 Timer timer; | |
131 int counter = 0; | |
132 | |
133 void tick(_) { | |
134 counter++; | |
135 controller.add(counter); // Ask stream to send counter values as event. | |
136 } | |
137 | |
138 void startTimer() { | |
139 timer = new Timer.periodic(const Duration(milliseconds: 10), tick); | |
140 } | |
141 | |
142 void stopTimer() { | |
143 if (timer != null) { | |
144 timer.cancel(); | |
145 timer = null; | |
146 } | |
147 } | |
148 | |
149 controller = new StreamController( | |
150 onListen: startTimer, | |
151 onPause: stopTimer, | |
152 onResume: startTimer, | |
153 onCancel: stopTimer); | |
154 | |
155 return controller.stream; | |
156 } | |
OLD | NEW |