| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 Stream timedStream(int n) { | 9 Stream timedStream(int n) { |
| 10 int i = 0; | 10 int i = 0; |
| 11 StreamController controller; | 11 StreamController controller; |
| 12 tick() { | 12 tick() { |
| 13 if (i >= n) { | 13 if (i >= n) { |
| 14 controller.close(); | 14 controller.close(); |
| 15 return; | 15 return; |
| 16 } | 16 } |
| 17 controller.add(i); | 17 controller.add(i); |
| 18 i++; | 18 i++; |
| 19 new Future.delayed(new Duration(milliseconds: 10), tick); | 19 new Future.delayed(new Duration(milliseconds: 0), tick); |
| 20 } | 20 } |
| 21 controller = new StreamController(onListen: tick); | 21 controller = new StreamController(onListen: tick); |
| 22 return controller.stream; | 22 return controller.stream; |
| 23 } | 23 } |
| 24 | 24 |
| 25 void main() { | 25 void main() { |
| 26 asyncStart(); | 26 asyncStart(); |
| 27 StreamIterator iterator = new StreamIterator(timedStream(10)); | 27 StreamIterator iterator = new StreamIterator(timedStream(10)); |
| 28 helper(more) { | 28 helper(more) { |
| 29 if (!more) { | 29 if (!more) { |
| 30 // Canceling the already closed iterator should not lead to a crash. | 30 // Canceling the already closed iterator should not lead to a crash. |
| 31 iterator.cancel(); | 31 iterator.cancel(); |
| 32 asyncEnd(); | 32 asyncEnd(); |
| 33 } else { | 33 } else { |
| 34 iterator.moveNext().then(helper); | 34 iterator.moveNext().then(helper); |
| 35 } | 35 } |
| 36 } | 36 } |
| 37 iterator.moveNext().then(helper); | 37 iterator.moveNext().then(helper); |
| 38 } | 38 } |
| OLD | NEW |