Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: samples/isolate_html/isolate_sample.dart

Issue 23868034: Remove spawnDomFunction from dart:html (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/isolate_html/isolate_sample.css ('k') | samples/isolate_html/isolate_sample.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library isolate_sample;
6
7 import 'dart:html';
8 import 'dart:isolate';
9
10 /*
11 * This is a simple sample application showing how to create two isolates
12 * and send messages to them, and receive replies back.
13 */
14
15 /**
16 * These are the messages we are going to send to the isolates that we create.
17 */
18 class MessageId {
19 static const INIT = 'init';
20 static const GREETING = 'greeting';
21 }
22
23 /**
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').
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
28 * what.
29 */
30 Future<SendPort> createIsolate(String name) {
31 return spawnDomFunction(isolateMain).then((sendPort) {
32 var message = {
33 'id' : MessageId.INIT,
34 'args' : [name, port.toSendPort()]
35 };
36 sendPort.send(message, null);
37 return sendPort;
38 });
39 }
40
41 // TODO(mattsh) get this off the System object once it's available
42 // see http://dartbug.com/3357
43 bool isVm() => 1234567890123456789 % 2 > 0;
44
45 /**
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
48 * UI thread, though, so that they can share DOM state.
49 */
50 void isolateMain() {
51 Element div;
52 String isolateName;
53 SendPort chirpPort;
54
55 void init(String isolateName_, SendPort chirpPort_) {
56 isolateName = isolateName_;
57 chirpPort = chirpPort_;
58 div = new DivElement()
59 ..classes = ['isolate', 'isolate${isolateName}']
60 ..innerHtml = query('#isolateTemplate').innerHtml
61 ..query('.isolateName').text = isolateName
62 ..query('.chirpButton').onClick.listen((event) {
63 chirpPort.send(
64 'this is a chirp message from isolate $isolateName', null);
65 });
66 query('#isolateParent').nodes.add(div);
67 }
68
69 /**
70 * Display the message we received, and send back a simple reply (unless
71 * the user has unchecked the reply checkbox).
72 */
73 void greeting(String message, SendPort replyTo) {
74 div.query('.messageBox').innerHtml =
75 'received message: <span class="messageText">"${message}"</span>';
76 if (div.query('input.replyCheckbox').checked) {
77 InputElement element = div.query('.delayTextbox');
78 int millis = int.parse(element.value);
79 new Timer(new Duration(milliseconds: millis), () {
80 replyTo.send('this is a reply from isolate "${isolateName}"', null);
81 });
82 }
83 }
84
85 port.receive((message, SendPort replyTo) {
86 switch(message['id']) {
87 case MessageId.INIT:
88 init(message['args'][0], message['args'][1]);
89 break;
90 case MessageId.GREETING:
91 greeting(message['args'][0], replyTo);
92 break;
93 }
94 });
95 }
96
97 main() {
98 //Map from isolate name to the port used to send messages to that isolate.
99 final Map<String, SendPort> ports = new Map();
100
101 // Do initialization and set up event handlers.
102 query('#appTitle').text = 'Hello, isolates.';
103 query('#vmStatus').text = '${isVm()}';
104
105 var replyElement = query('.isolateMain .replyText');
106
107 createIsolate('A').then((sendPortA) {
108 ports['A'] = sendPortA;
109 createIsolate('B').then((sendPortB) {
110 ports['B'] = sendPortB;
111
112 for (var element in queryAll('.sendButton')) {
113 element.onClick.listen((Event e) {
114 replyElement.text = 'waiting for reply...';
115
116 var isolateName =
117 (e.currentTarget as Element).attributes['data-isolate-name'];
118 var greeting = query('input#greetingText').value;
119 var message = {'id': MessageId.GREETING, 'args': [greeting]};
120 ports[isolateName].call(message).then((var msg) {
121 replyElement.text = msg;
122 });
123 });
124 }
125
126 port.receive((var message, SendPort replyTo) {
127 replyElement.text = message;
128 });
129 });
130 });
131 }
OLDNEW
« no previous file with comments | « samples/isolate_html/isolate_sample.css ('k') | samples/isolate_html/isolate_sample.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698