2 Copyright 2007, Google Inc.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
7 1. Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9 2. Redistributions in binary form must reproduce the above copyright notice,
10 this list of conditions and the following disclaimer in the documentation
11 and/or other materials provided with the distribution.
12 3. Neither the name of Google Inc. nor the names of its contributors may be
13 used to endorse or promote products derived from this software without
14 specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 <title>Google Gears WorkerPool Demo
</title>
33 <link rel=
"stylesheet" type=
"text/css" href=
"sample.css">
43 <body onload=
"init()" id=
"theBody">
44 <h1>Google Gears WorkerPool Demo
</h1>
45 <div id=
"view-source"> </div>
52 <td valign=top
>Interact with UI:
</td>
53 <td><input type=
"button" value=
"Interact" onclick=
"interact();">
58 <td valign=top
>Run expensive computation:
</td>
60 <input type=
"button" id=
"syncButton"
61 value=
"Synchronous" onclick=
"syncComputation();">
63 <td><i>Note the synchronous computation blocks UI interaction.
</i>
70 <input type=
"button" id=
"asyncButton"
71 value=
"Asynchronous" onclick=
"asyncComputation();">
74 <i>But you can still interact while the asynchronous computation
75 runs in a JavaScript worker.
</i>
80 <div id=
"results" style=
"font-size: 120%";
>
81 <p style=
"font-weight: bold;" class=
"results">Results
84 <!-- ====================================== -->
85 <!-- End HTML code. Begin JavaScript code. -->
87 <script src=
"../gears_init.js"></script>
88 <script src=
"sample.js"></script>
92 insertRow('Hello, threaded JavaScript.');
96 function insertRow(message
) {
97 var results
= getElementById('results');
98 var id
= 'message' + (childNodes(results
).length
+ 1);
99 results
.innerHTML
+= '<p class="results" id="' + id
+ '">';
100 setTextContent(getElementById(id
), message
);
107 var workerPool
= null;
110 function parentInit() {
111 if (!window
.google
|| !google
.gears
) {
117 google
.gears
.factory
.create('beta.workerpool');
119 document
.getElementById('asyncButton').disabled
= true;
120 setError('Could not create workerpool: ' + e
.message
);
124 // set the parent's message handler
125 workerPool
.onmessage
= parentHandler
;
127 // setup the entire body of JavsScript code to run in the worker
128 var childCode
= String(childInit
) +
129 String(childHandler
) +
130 'childInit();'; // runs init function
134 childId
= workerPool
.createWorker(childCode
);
136 setError('Could not create worker: ' + e
.message
);
139 // Child workers will always set onmessage before createWorker() returns.
140 insertRow('Parent initialized.');
143 function parentHandler(msg
, sender
) {
144 insertRow('Asynchronous result: ' + msg
);
148 function childInit() {
149 google
.gears
.workerPool
.onmessage
= childHandler
;
152 function childHandler(msg
, sender
) {
154 var cmd
= msg
.substr(0, 4);
155 var arg
= msg
.substr(5);
157 var result
= fibonacci(arg
);
158 google
.gears
.workerPool
.sendMessage(String(result
), sender
);
159 } else if (cmd
== 'iden') {
160 var result
= identity(arg
);
161 google
.gears
.workerPool
.sendMessage(String(result
), sender
);
163 google
.gears
.workerPool
.sendMessage('Unknown command.', sender
);
166 function fibonacci(n
) {
171 result
+= prevResult
;
178 function identity(n
) {
182 var tempString
= 'abc' + '123'; // for slowdown
191 // UI-related functions
194 var uiToggleState
= 0;
195 function interact() {
197 getElementById('theBody').style
.backgroundColor
= '#FFFFFF';
200 getElementById('theBody').style
.backgroundColor
= '#CCCCCC';
205 function asyncComputation() {
207 var message
= 'iden:2501234';
208 workerPool
.sendMessage(message
, childId
);
212 function syncComputation() {
213 var result
= identity(2501234);
214 insertRow('Synchronous result: ' + String(result
));
216 function identity(n
) {
220 var tempString
= 'abc' + '123'; // for slowdown