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 bool cancelled; | 9 bool canceled; |
10 | 10 |
11 test1() async { | 11 test1() async { |
12 cancelled = false; | 12 canceled = false; |
13 try { | 13 try { |
14 StreamController controller = infiniteStreamController(); | 14 StreamController controller = infiniteStreamController(); |
15 outer: while(true) { | 15 outer: while(true) { |
16 await for (var x in controller.stream) { | 16 await for (var x in controller.stream) { |
17 for (int j = 0; j < 10; j++) { | 17 for (int j = 0; j < 10; j++) { |
18 if (j == 5) break outer; | 18 if (j == 5) break outer; |
19 } | 19 } |
20 } | 20 } |
21 } | 21 } |
22 } finally { | 22 } finally { |
23 Expect.isTrue(cancelled); | 23 Expect.isTrue(canceled); |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 test2() async { | 27 test2() async { |
28 cancelled = false; | 28 canceled = false; |
29 try { | 29 try { |
30 StreamController controller = infiniteStreamController(); | 30 StreamController controller = infiniteStreamController(); |
31 bool first = true; | 31 bool first = true; |
32 outer: while(true) { | 32 outer: while(true) { |
33 if (first) { | 33 if (first) { |
34 first = false; | 34 first = false; |
35 } else { | 35 } else { |
36 break; | 36 break; |
37 } | 37 } |
38 await for (var x in controller.stream) { | 38 await for (var x in controller.stream) { |
39 for (int j = 0; j < 10; j++) { | 39 for (int j = 0; j < 10; j++) { |
40 if (j == 5) continue outer; | 40 if (j == 5) continue outer; |
41 } | 41 } |
42 } | 42 } |
43 } | 43 } |
44 } finally { | 44 } finally { |
45 Expect.isTrue(cancelled); | 45 Expect.isTrue(canceled); |
46 } | 46 } |
47 } | 47 } |
48 | 48 |
49 test() async { | 49 test() async { |
50 await test1(); | 50 await test1(); |
51 await test2(); | 51 await test2(); |
52 } | 52 } |
53 | 53 |
54 main() { | 54 main() { |
55 asyncStart(); | 55 asyncStart(); |
56 test().then((_) { | 56 test().then((_) { |
57 asyncEnd(); | 57 asyncEnd(); |
58 }); | 58 }); |
59 } | 59 } |
60 | 60 |
61 | 61 |
62 // Create a stream that produces numbers [1, 2, ... ] | 62 // Create a stream that produces numbers [1, 2, ... ] |
63 StreamController infiniteStreamController() { | 63 StreamController infiniteStreamController() { |
64 StreamController controller; | 64 StreamController controller; |
65 Timer timer; | 65 Timer timer; |
66 int counter = 0; | 66 int counter = 0; |
67 | 67 |
68 void tick(_) { | 68 void tick() { |
| 69 if (controller.isPaused) { |
| 70 return; |
| 71 } |
| 72 if (canceled) { |
| 73 return; |
| 74 } |
69 counter++; | 75 counter++; |
70 controller.add(counter); // Ask stream to send counter values as event. | 76 controller.add(counter); // Ask stream to send counter values as event. |
| 77 Timer.run(tick); |
71 } | 78 } |
72 | 79 |
73 void startTimer() { | 80 void startTimer() { |
74 timer = new Timer.periodic(const Duration(milliseconds: 10), tick); | 81 Timer.run(tick); |
75 } | |
76 | |
77 void stopTimer() { | |
78 if (timer != null) { | |
79 timer.cancel(); | |
80 timer = null; | |
81 } | |
82 } | 82 } |
83 | 83 |
84 controller = new StreamController( | 84 controller = new StreamController( |
85 onListen: startTimer, | 85 onListen: startTimer, |
86 onPause: stopTimer, | |
87 onResume: startTimer, | 86 onResume: startTimer, |
88 onCancel: () { | 87 onCancel: () { |
89 cancelled = true; | 88 canceled = true; |
90 stopTimer(); | |
91 }); | 89 }); |
92 | 90 |
93 return controller; | 91 return controller; |
94 } | 92 } |
OLD | NEW |