| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 'package:expect/expect.dart'; | 5 import 'package:expect/expect.dart'; |
| 6 import 'package:async_helper/async_helper.dart'; | 6 import 'package:async_helper/async_helper.dart'; |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 main() { | 9 main() { |
| 10 Completer done = new Completer(); | 10 Completer done = new Completer(); |
| 11 | 11 |
| 12 var valueToCapture; | 12 var valueToCapture; |
| 13 var restoredValue; | 13 var restoredValue; |
| 14 | 14 |
| 15 Expect.identical(Zone.ROOT, Zone.current); | 15 Expect.identical(Zone.ROOT, Zone.current); |
| 16 Zone forked = Zone.current.fork(specification: new ZoneSpecification( | 16 Zone forked = Zone.current.fork(specification: new ZoneSpecification( |
| 17 registerUnaryCallback: (Zone self, ZoneDelegate parent, Zone origin, f(arg
)) { | 17 registerUnaryCallback: |
| 18 // The zone is still the same as when origin.run was invoked, which | 18 (Zone self, ZoneDelegate parent, Zone origin, f(arg)) { |
| 19 // is the root zone. (The origin zone hasn't been set yet). | 19 // The zone is still the same as when origin.run was invoked, which |
| 20 Expect.identical(Zone.current, Zone.ROOT); | 20 // is the root zone. (The origin zone hasn't been set yet). |
| 21 // Note that not forwarding is completely legal, though not encouraged. | 21 Expect.identical(Zone.current, Zone.ROOT); |
| 22 var capturedValue = valueToCapture; | 22 // Note that not forwarding is completely legal, though not encouraged. |
| 23 return parent.registerUnaryCallback(origin, (arg) { | 23 var capturedValue = valueToCapture; |
| 24 restoredValue = capturedValue; | 24 return parent.registerUnaryCallback(origin, (arg) { |
| 25 return f(arg); | 25 restoredValue = capturedValue; |
| 26 }); | 26 return f(arg); |
| 27 })); | 27 }); |
| 28 })); |
| 28 | 29 |
| 29 valueToCapture = 499; | 30 valueToCapture = 499; |
| 30 var fun = (x) => x + 99; | 31 var fun = (x) => x + 99; |
| 31 var registered = forked.registerUnaryCallback(fun); | 32 var registered = forked.registerUnaryCallback(fun); |
| 32 Expect.isFalse(identical(fun, registered)); | 33 Expect.isFalse(identical(fun, registered)); |
| 33 | 34 |
| 34 // It is valid to invoke the callback in a different zone. This is, of course, | 35 // It is valid to invoke the callback in a different zone. This is, of course, |
| 35 // extremely discouraged. | 36 // extremely discouraged. |
| 36 var result = registered(399); | 37 var result = registered(399); |
| 37 Expect.equals(498, result); | 38 Expect.equals(498, result); |
| 38 Expect.equals(499, restoredValue); | 39 Expect.equals(499, restoredValue); |
| 39 } | 40 } |
| OLD | NEW |