OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015, 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 import "dart:async"; | |
6 import "package:expect/expect.dart"; | |
7 import "package:async_helper/async_helper.dart"; | |
8 | |
9 // Create a stream that produces numbers [1, 2, ... maxCount] | |
10 Stream timedCounter(int maxCount) { | |
floitsch
2015/02/04 12:31:29
class seems unused.
sigurdm
2015/02/05 14:06:06
Removed
| |
11 StreamController controller; | |
12 Timer timer; | |
13 int counter = 0; | |
14 | |
15 void tick(_) { | |
16 counter++; | |
17 controller.add(counter); // Ask stream to send counter values as event. | |
18 if (counter >= maxCount) { | |
19 timer.cancel(); | |
20 controller.close(); // Ask stream to shut down and tell listeners. | |
21 } | |
22 } | |
23 | |
24 void startTimer() { | |
25 timer = new Timer.periodic(const Duration(milliseconds: 10), tick); | |
26 } | |
27 | |
28 void stopTimer() { | |
29 if (timer != null) { | |
30 timer.cancel(); | |
31 timer = null; | |
32 } | |
33 } | |
34 | |
35 controller = new StreamController( | |
36 onListen: startTimer, | |
37 onPause: stopTimer, | |
38 onResume: startTimer, | |
39 onCancel: stopTimer); | |
40 | |
41 return controller.stream; | |
42 } | |
43 | |
44 | |
45 Stream<int> foo1() async* { | |
46 yield 1; | |
47 var p = await new Future.delayed(new Duration(milliseconds: 10), () => 10); | |
48 yield p + 10; | |
49 } | |
50 | |
51 Stream<int> foo2() async* { | |
52 int i = 0; | |
53 while (true) { | |
54 await (new Future.delayed(new Duration(milliseconds: 700), () {})); | |
55 if (i > 10) return; | |
56 yield i; | |
57 i++; | |
58 } | |
59 } | |
60 | |
61 Stream<int> foo3(p) async* { | |
62 int i = 0; | |
63 bool t = false; | |
64 yield null; | |
65 while (true) { | |
66 i++; | |
67 a: for (int i = 0; i < p; i++) { | |
68 if (!t) { | |
69 for (int j = 0; j < 3; j++) { | |
70 yield -1; | |
71 t = true; | |
72 break a; | |
73 } | |
74 } | |
75 await 4; | |
76 yield i; | |
77 } | |
78 } | |
79 } | |
80 | |
81 Completer<bool> finalized = new Completer<bool>(); | |
82 | |
83 Stream<int> foo4() async* { | |
84 int i = 0; | |
85 try { | |
86 while (true) { | |
87 yield i; | |
88 i++; | |
89 } | |
90 } finally { | |
91 // Canceling the stream-subscription should run the finalizer. | |
92 finalized.complete(true); | |
93 } | |
94 } | |
95 | |
96 main () async { | |
floitsch
2015/02/04 12:31:29
you still need the async helper package.
sigurdm
2015/02/05 14:06:06
Done.
| |
97 Expect.listEquals([1, 20], await (foo1().toList())); | |
98 Expect.listEquals([0, 1, 2, 3], await (foo2().take(4).toList())); | |
99 Expect.listEquals([null, -1, 0, 1, 2, 3, 0, 1, 2, 3], await (foo3(4).take(10). toList())); | |
floitsch
2015/02/04 12:31:29
long lines.
sigurdm
2015/02/05 14:06:06
Done.
| |
100 Expect.listEquals([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], await (foo4().take(10).toLis t())); | |
101 Expect.isTrue(await (finalized.future)); | |
102 } | |
OLD | NEW |