Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // | |
| 5 // VMOptions=--enable_async | |
| 6 | |
| 7 import "dart:async"; | |
| 8 import "package:expect/expect.dart"; | |
| 9 | |
| 10 class Trace { | |
| 11 String trace = ""; | |
| 12 record(x) { | |
| 13 trace += x.toString(); | |
| 14 } | |
| 15 toString() => trace; | |
| 16 } | |
| 17 | |
| 18 | |
| 19 Stream makeMeAStream() { | |
| 20 return timedCounter(5); | |
| 21 } | |
| 22 | |
| 23 Trace t1 = new Trace(); | |
| 24 | |
| 25 consumeOne() async { | |
| 26 // Equivalent to await for (x in makeMeAStream()) { ... } | |
| 27 var s = makeMeAStream(); | |
| 28 var it = new StreamIterator(s); | |
| 29 while (await it.moveNext()) { | |
| 30 var x = it.current; | |
| 31 t1.record(x); | |
| 32 } | |
| 33 t1.record("X"); | |
| 34 } | |
| 35 | |
| 36 Trace t2 = new Trace(); | |
| 37 | |
| 38 consumeTwo() async { | |
| 39 await for (var x in makeMeAStream()) { | |
| 40 t2.record(x); | |
| 41 } | |
| 42 t2.record("X"); | |
| 43 } | |
| 44 | |
| 45 main() { | |
| 46 var f1 = consumeOne(); | |
| 47 t1.record("T1:"); | |
| 48 | |
| 49 var f2 = consumeTwo(); | |
| 50 t2.record("T2:"); | |
| 51 | |
| 52 Future.wait([f1, f2]).then((_) { | |
|
floitsch
2014/10/16 19:03:56
You should use the async-helper package to make su
hausner
2014/10/16 21:39:27
I added the asyncStart() and asyncEnd() calls. Not
floitsch
2014/10/17 09:27:30
It might not make a difference for the VM (althoug
| |
| 53 print("Trace 1: $t1"); | |
| 54 print("Trace 2: $t2"); | |
| 55 Expect.equals("T1:12345X", t1.toString()); | |
| 56 Expect.equals("T2:12345X", t2.toString()); | |
| 57 }); | |
| 58 } | |
| 59 | |
| 60 | |
| 61 // Create a stream that produces numbers [1, 2, ... maxCount] | |
| 62 Stream timedCounter(int maxCount) { | |
| 63 StreamController controller; | |
| 64 Timer timer; | |
| 65 int counter = 0; | |
| 66 | |
| 67 void tick(_) { | |
| 68 counter++; | |
| 69 controller.add(counter); // Ask stream to send counter values as event. | |
| 70 if (counter >= maxCount) { | |
| 71 timer.cancel(); | |
| 72 controller.close(); // Ask stream to shut down and tell listeners. | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 void startTimer() { | |
| 77 timer = new Timer.periodic(const Duration(milliseconds: 200), tick); | |
|
floitsch
2014/10/16 19:03:56
waiting 200 milliseconds for every tick is a lot.
hausner
2014/10/16 21:39:27
Shortened to 10ms.
| |
| 78 } | |
| 79 | |
| 80 void stopTimer() { | |
| 81 if (timer != null) { | |
| 82 timer.cancel(); | |
| 83 timer = null; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 controller = new StreamController( | |
| 88 onListen: startTimer, | |
| 89 onPause: stopTimer, | |
| 90 onResume: startTimer, | |
| 91 onCancel: stopTimer); | |
| 92 | |
| 93 return controller.stream; | |
| 94 } | |
| OLD | NEW |