| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, 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 'package:async_helper/async_helper.dart'; | |
| 6 import "package:expect/expect.dart"; | |
| 7 import 'dart:async'; | |
| 8 import 'catch_errors.dart'; | |
| 9 | |
| 10 main() { | |
| 11 asyncStart(); | |
| 12 Completer done = new Completer(); | |
| 13 | |
| 14 var events = []; | |
| 15 StreamController controller; | |
| 16 Stream stream; | |
| 17 // Test that the first listen on a `asBroadcastStream` determines the | |
| 18 // zone the subscription lives in. In this case the outer listen happens first | |
| 19 // and the error reaches `handleError`. | |
| 20 catchErrors(() { | |
| 21 catchErrors(() { | |
| 22 controller = new StreamController(); | |
| 23 | |
| 24 // Assign to the "global" `stream`. | |
| 25 stream = controller.stream.map((x) { | |
| 26 events.add("map $x"); | |
| 27 return x + 100; | |
| 28 }).transform( | |
| 29 new StreamTransformer.fromHandlers(handleError: (e, st, sink) { | |
| 30 sink.add("error $e"); | |
| 31 })).asBroadcastStream(); | |
| 32 | |
| 33 // Listen to the `stream` in the inner zone (but wait in a microtask). | |
| 34 scheduleMicrotask(() { | |
| 35 stream.listen((x) { | |
| 36 events.add("stream $x"); | |
| 37 if (x == "error 2") done.complete(true); | |
| 38 }); | |
| 39 }); | |
| 40 }) | |
| 41 .listen((x) { | |
| 42 events.add(x); | |
| 43 }) | |
| 44 .asFuture() | |
| 45 .then((_) { | |
| 46 Expect.fail("Unexpected callback"); | |
| 47 }); | |
| 48 | |
| 49 // Listen to `stream` from the outer zone. | |
| 50 stream.listen((x) { | |
| 51 events.add("stream2 $x"); | |
| 52 }); | |
| 53 | |
| 54 // Feed the controller, but wait in a microtask. | |
| 55 scheduleMicrotask(() { | |
| 56 controller.add(1); | |
| 57 controller.addError(2); | |
| 58 controller.close(); | |
| 59 }); | |
| 60 }).listen((x) { | |
| 61 events.add("outer: $x"); | |
| 62 }, onDone: () { | |
| 63 Expect.fail("Unexpected callback"); | |
| 64 }); | |
| 65 | |
| 66 done.future.whenComplete(() { | |
| 67 // Give handlers time to complete. | |
| 68 Timer.run(() { | |
| 69 Expect.listEquals([ | |
| 70 "map 1", | |
| 71 "stream2 101", | |
| 72 "stream 101", | |
| 73 "stream2 error 2", | |
| 74 "stream error 2", | |
| 75 ], events); | |
| 76 asyncEnd(); | |
| 77 }); | |
| 78 }); | |
| 79 } | |
| OLD | NEW |