Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library isolate_sample; | 5 library isolate_sample; |
| 6 | 6 |
| 7 import 'dart:html'; | 7 import 'dart:html'; |
| 8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
| 9 | 9 |
| 10 /* | 10 /* |
| 11 * This is a simple sample application showing how to create two isolates | 11 * This is a simple sample application showing how to create two isolates |
| 12 * and send messages to them, and receive replies back. | 12 * and send messages to them, and receive replies back. |
| 13 */ | 13 */ |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * These are the messages we are going to send to the isolates that we create. | 16 * These are the messages we are going to send to the isolates that we create. |
| 17 */ | 17 */ |
| 18 class MessageId { | 18 class MessageId { |
| 19 static const INIT = 'init'; | 19 static const INIT = 'init'; |
| 20 static const GREETING = 'greeting'; | 20 static const GREETING = 'greeting'; |
| 21 } | 21 } |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * Create a new isolate with a given name. (In this sample app | 24 * Create a new isolate with a given name. (In this sample app |
| 25 * we only have two isolates, and they are named 'A' and 'B'). | 25 * we only have two isolates, and they are named 'A' and 'B'). |
| 26 * Note, isolates aren't normally named, but it's useful to give | 26 * Note, isolates aren't normally named, but it's useful to give |
| 27 * them names in this app so we can show which isolate is doing | 27 * them names in this app so we can show which isolate is doing |
| 28 * what. | 28 * what. |
| 29 */ | 29 */ |
| 30 SendPort createIsolate(String name) { | 30 Future<SendPort> createIsolate(String name) { |
| 31 var sendPort = spawnDomFunction(isolateMain); | 31 return spawnDomFunction(isolateMain).transform((sendPort) { |
|
vsm
2013/03/26 19:13:37
transform -> then?
Anton Muhin
2013/03/26 19:15:37
I have to return a future as well, hence I used tr
vsm
2013/03/26 19:21:39
Future.then returns a Future. I don't see transfo
Anton Muhin
2013/03/27 08:41:46
You're absolutely right, sorry, got confused.
On
| |
| 32 var message = { | 32 var message = { |
| 33 'id' : MessageId.INIT, | 33 'id' : MessageId.INIT, |
| 34 'args' : [name, port.toSendPort()] | 34 'args' : [name, port.toSendPort()] |
| 35 }; | 35 }; |
| 36 sendPort.send(message, null); | 36 sendPort.send(message, null); |
| 37 return sendPort; | 37 return sendPort; |
| 38 }); | |
| 38 } | 39 } |
| 39 | 40 |
| 40 // TODO(mattsh) get this off the System object once it's available | 41 // TODO(mattsh) get this off the System object once it's available |
| 41 // see http://dartbug.com/3357 | 42 // see http://dartbug.com/3357 |
| 42 bool isVm() => 1234567890123456789 % 2 > 0; | 43 bool isVm() => 1234567890123456789 % 2 > 0; |
| 43 | 44 |
| 44 /** | 45 /** |
| 45 * This function will run in a separate isolate, which shares almost | 46 * This function will run in a separate isolate, which shares almost |
| 46 * no state with the main isolate. They will both run in the main | 47 * no state with the main isolate. They will both run in the main |
| 47 * UI thread, though, so that they can share DOM state. | 48 * UI thread, though, so that they can share DOM state. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 main() { | 97 main() { |
| 97 //Map from isolate name to the port used to send messages to that isolate. | 98 //Map from isolate name to the port used to send messages to that isolate. |
| 98 final Map<String, SendPort> ports = new Map(); | 99 final Map<String, SendPort> ports = new Map(); |
| 99 | 100 |
| 100 // Do initialization and set up event handlers. | 101 // Do initialization and set up event handlers. |
| 101 query('#appTitle').text = 'Hello, isolates.'; | 102 query('#appTitle').text = 'Hello, isolates.'; |
| 102 query('#vmStatus').text = '${isVm()}'; | 103 query('#vmStatus').text = '${isVm()}'; |
| 103 | 104 |
| 104 var replyElement = query('.isolateMain .replyText'); | 105 var replyElement = query('.isolateMain .replyText'); |
| 105 | 106 |
| 106 ports['A'] = createIsolate('A'); | 107 createIsolate('A').then((sendPortA) { |
| 107 ports['B'] = createIsolate('B'); | 108 ports['A'] = sendPortA; |
| 109 createIsolate('B').then((sendPortB) { | |
| 110 ports['B'] = sendPortB; | |
| 108 | 111 |
| 109 for (var element in queryAll('.sendButton')) { | 112 for (var element in queryAll('.sendButton')) { |
| 110 element.onClick.listen((Event e) { | 113 element.onClick.listen((Event e) { |
| 111 replyElement.text = 'waiting for reply...'; | 114 replyElement.text = 'waiting for reply...'; |
| 112 | 115 |
| 113 var isolateName = | 116 var isolateName = |
| 114 (e.currentTarget as Element).attributes['data-isolate-name']; | 117 (e.currentTarget as Element).attributes['data-isolate-name']; |
| 115 var greeting = query('input#greetingText').value; | 118 var greeting = query('input#greetingText').value; |
| 116 var message = {'id': MessageId.GREETING, 'args': [greeting]}; | 119 var message = {'id': MessageId.GREETING, 'args': [greeting]}; |
| 117 ports[isolateName].call(message).then((var msg) { | 120 ports[isolateName].call(message).then((var msg) { |
| 118 replyElement.text = msg; | 121 replyElement.text = msg; |
| 122 }); | |
| 123 }); | |
| 124 } | |
| 125 | |
| 126 port.receive((var message, SendPort replyTo) { | |
| 127 replyElement.text = message; | |
| 119 }); | 128 }); |
| 120 }); | 129 }); |
| 121 } | |
| 122 | |
| 123 port.receive((var message, SendPort replyTo) { | |
| 124 replyElement.text = message; | |
| 125 }); | 130 }); |
| 126 } | 131 } |
| OLD | NEW |