dom/workers/test/onLine_worker.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:9e7e828d2ef1
1 /*
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/licenses/publicdomain/
4 */
5
6 importScripts("onLine_worker_head.js");
7
8 var N_CHILDREN = 3;
9 var children = [];
10 var finishedChildrenCount = 0;
11 var lastTest = false;
12
13 for (var event of ["online", "offline"]) {
14 addEventListener(event,
15 makeHandler(
16 "addEventListener('%1', ..., false)",
17 event, 1, "Parent Worker"),
18 false);
19 }
20
21 onmessage = function(e) {
22 if (e.data === 'lastTest') {
23 children.forEach(function(w) {
24 w.postMessage({ type: 'lastTest' });
25 });
26 lastTest = true;
27 }
28 }
29
30 function setupChildren(cb) {
31 var readyCount = 0;
32 for (var i = 0; i < N_CHILDREN; ++i) {
33 var w = new Worker("onLine_worker_child.js");
34 children.push(w);
35
36 w.onerror = function(e) {
37 info("Error creating child " + e.message);
38 }
39
40 w.onmessage = function(e) {
41 if (e.data.type === 'ready') {
42 info("Got ready from child");
43 readyCount++;
44 if (readyCount === N_CHILDREN) {
45 cb();
46 }
47 } else if (e.data.type === 'finished') {
48 finishedChildrenCount++;
49
50 if (lastTest && finishedChildrenCount === N_CHILDREN) {
51 postMessage({ type: 'finished' });
52 children = [];
53 close();
54 }
55 } else if (e.data.type === 'ok') {
56 // Pass on test to page.
57 postMessage(e.data);
58 }
59 }
60 }
61 }
62
63 setupChildren(function() {
64 postMessage({ type: 'ready' });
65 });

mercurial