| 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 import 'dart:math'; | 9 import 'dart:math'; |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 /** | 24 /** |
| 25 * Create a new isolate with a given name. (In this sample app | 25 * Create a new isolate with a given name. (In this sample app |
| 26 * we only have two isolates, and they are named 'A' and 'B'). | 26 * we only have two isolates, and they are named 'A' and 'B'). |
| 27 * Note, isolates aren't normally named, but it's useful to give | 27 * Note, isolates aren't normally named, but it's useful to give |
| 28 * them names in this app so we can show which isolate is doing | 28 * them names in this app so we can show which isolate is doing |
| 29 * what. | 29 * what. |
| 30 */ | 30 */ |
| 31 SendPort createIsolate(String name) { | 31 SendPort createIsolate(String name) { |
| 32 var sendPort = spawnDomFunction(isolateMain); | 32 var sendPort = spawnDomFunction(isolateMain); |
| 33 var message = { | 33 var message = { |
| 34 'id' : MessageId.INIT, | 34 'id' : MessageId.INIT, |
| 35 'args' : [name, port.toSendPort()] | 35 'args' : [name, port.toSendPort()] |
| 36 }; | 36 }; |
| 37 sendPort.send(message, null); | 37 sendPort.send(message, null); |
| 38 return sendPort; | 38 return sendPort; |
| 39 } | 39 } |
| 40 | 40 |
| 41 // 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 |
| 42 // see http://dartbug.com/3357 | 42 // see http://dartbug.com/3357 |
| 43 bool isVm() => 1234567890123456789 % 2 > 0; | 43 bool isVm() => 1234567890123456789 % 2 > 0; |
| 44 | 44 |
| 45 /** | 45 /** |
| 46 * This function will run in a separate isolate, which shares almost | 46 * This function will run in a separate isolate, which shares almost |
| 47 * 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 |
| 48 * UI thread, though, so that they can share DOM state. | 48 * UI thread, though, so that they can share DOM state. |
| 49 */ | 49 */ |
| 50 void isolateMain() { | 50 void isolateMain() { |
| 51 Element div; | 51 Element div; |
| 52 String isolateName; | 52 String isolateName; |
| 53 SendPort chirpPort; | 53 SendPort chirpPort; |
| 54 | 54 |
| 55 void init(String isolateName_, SendPort chirpPort_) { | 55 void init(String isolateName_, SendPort chirpPort_) { |
| 56 isolateName = isolateName_; | 56 isolateName = isolateName_; |
| 57 chirpPort = chirpPort_; | 57 chirpPort = chirpPort_; |
| 58 div = new DivElement() | 58 div = new DivElement() |
| 59 ..classes = ['isolate', 'isolate${isolateName}'] | 59 ..classes = ['isolate', 'isolate${isolateName}'] |
| 60 ..innerHTML = query('#isolateTemplate').innerHTML | 60 ..innerHtml = query('#isolateTemplate').innerHtml |
| 61 ..query('.isolateName').text = isolateName | 61 ..query('.isolateName').text = isolateName |
| 62 ..query('.chirpButton').on.click.add((event) { | 62 ..query('.chirpButton').on.click.add((event) { |
| 63 chirpPort.send( | 63 chirpPort.send( |
| 64 'this is a chirp message from isolate $isolateName', null); | 64 'this is a chirp message from isolate $isolateName', null); |
| 65 }); | 65 }); |
| 66 query('#isolateParent').nodes.add(div); | 66 query('#isolateParent').nodes.add(div); |
| 67 } | 67 } |
| 68 | 68 |
| 69 /** | 69 /** |
| 70 * Display the message we received, and send back a simple reply (unless | 70 * Display the message we received, and send back a simple reply (unless |
| 71 * the user has unchecked the reply checkbox). | 71 * the user has unchecked the reply checkbox). |
| 72 */ | 72 */ |
| 73 void greeting(String message, SendPort replyTo) { | 73 void greeting(String message, SendPort replyTo) { |
| 74 div.query('.messageBox').innerHTML = | 74 div.query('.messageBox').innerHtml = |
| 75 'received message: <span class="messageText">"${message}"</span>'; | 75 'received message: <span class="messageText">"${message}"</span>'; |
| 76 if (div.query('input.replyCheckbox').checked) { | 76 if (div.query('input.replyCheckbox').checked) { |
| 77 InputElement element = div.query('.delayTextbox'); | 77 InputElement element = div.query('.delayTextbox'); |
| 78 int millis = parseInt(element.value); | 78 int millis = parseInt(element.value); |
| 79 // TODO(justinfagnani): use Timer when it works in isolates in dart2js | 79 // TODO(justinfagnani): use Timer when it works in isolates in dart2js |
| 80 // see: http://dartbug.com/4997 | 80 // see: http://dartbug.com/4997 |
| 81 window.setTimeout(() { | 81 window.setTimeout(() { |
| 82 replyTo.send('this is a reply from isolate "${isolateName}"', null); | 82 replyTo.send('this is a reply from isolate "${isolateName}"', null); |
| 83 }, millis); | 83 }, millis); |
| 84 } | 84 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 106 | 106 |
| 107 var replyElement = query('.isolateMain .replyText'); | 107 var replyElement = query('.isolateMain .replyText'); |
| 108 | 108 |
| 109 ports['A'] = createIsolate('A'); | 109 ports['A'] = createIsolate('A'); |
| 110 ports['B'] = createIsolate('B'); | 110 ports['B'] = createIsolate('B'); |
| 111 | 111 |
| 112 for (var element in queryAll('.sendButton')) { | 112 for (var element in queryAll('.sendButton')) { |
| 113 element.on.click.add((Event e) { | 113 element.on.click.add((Event e) { |
| 114 replyElement.text = 'waiting for reply...'; | 114 replyElement.text = 'waiting for reply...'; |
| 115 | 115 |
| 116 var isolateName = | 116 var isolateName = |
| 117 (e.currentTarget as Element).attributes['data-isolate-name']; | 117 (e.currentTarget as Element).attributes['data-isolate-name']; |
| 118 var greeting = query('input#greetingText').value; | 118 var greeting = query('input#greetingText').value; |
| 119 var message = {'id': MessageId.GREETING, 'args': [greeting]}; | 119 var message = {'id': MessageId.GREETING, 'args': [greeting]}; |
| 120 ports[isolateName].call(message).then((var msg) { | 120 ports[isolateName].call(message).then((var msg) { |
| 121 replyElement.text = msg; | 121 replyElement.text = msg; |
| 122 }); | 122 }); |
| 123 }); | 123 }); |
| 124 } | 124 } |
| 125 | 125 |
| 126 port.receive((var message, SendPort replyTo) { | 126 port.receive((var message, SendPort replyTo) { |
| 127 replyElement.text = message; | 127 replyElement.text = message; |
| 128 }); | 128 }); |
| 129 } | 129 } |
| OLD | NEW |