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 is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "dart:isolate"; |
| 6 import "dart:async"; |
| 7 import "package:async_helper/async_helper.dart"; |
| 8 |
| 9 void isomain(SendPort replyPort) { |
| 10 RawReceivePort port = new RawReceivePort(); |
| 11 port.handler = (v) { |
| 12 if (v == 0) { |
| 13 // Shut down when receiving the 0 message. |
| 14 port.close(); |
| 15 } else { |
| 16 replyPort.send(v); |
| 17 } |
| 18 }; |
| 19 replyPort.send(port.sendPort); |
| 20 } |
| 21 |
| 22 void main() { |
| 23 asyncStart(); |
| 24 var completer = new Completer(); // Completed by first reply from isolate. |
| 25 RawReceivePort reply = new RawReceivePort(completer.complete); |
| 26 bool mayComplete = false; |
| 27 Isolate.spawn(isomain, reply.sendPort).then((Isolate isolate) { |
| 28 isolate.onExit.then((_) { |
| 29 reply.close(); |
| 30 if (!mayComplete) throw "COMPLETED EARLY"; |
| 31 asyncEnd(); |
| 32 }); |
| 33 return completer.future; |
| 34 }).then((echoPort) { |
| 35 int counter = 4; |
| 36 reply.handler = (v) { |
| 37 if (v != counter) throw "WRONG REPLY"; |
| 38 if (v == 0) throw "REPLY INSTEAD OF SHUTDOWN"; |
| 39 counter--; |
| 40 mayComplete = (counter == 0); |
| 41 echoPort.send(counter); |
| 42 }; |
| 43 echoPort.send(counter); |
| 44 }); |
| 45 } |
OLD | NEW |