onLine_worker.js (1510B)
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( 15 event, 16 makeHandler( 17 "addEventListener('%1', ..., false)", 18 event, 19 1, 20 "Parent Worker" 21 ), 22 false 23 ); 24 } 25 26 onmessage = function (e) { 27 if (e.data === "lastTest") { 28 children.forEach(function (w) { 29 w.postMessage({ type: "lastTest" }); 30 }); 31 lastTest = true; 32 } 33 }; 34 35 function setupChildren(cb) { 36 var readyCount = 0; 37 for (var i = 0; i < N_CHILDREN; ++i) { 38 var w = new Worker("onLine_worker_child.js"); 39 children.push(w); 40 41 w.onerror = function (e) { 42 info("Error creating child " + e.message); 43 }; 44 45 w.onmessage = function (e) { 46 if (e.data.type === "ready") { 47 info("Got ready from child"); 48 readyCount++; 49 if (readyCount === N_CHILDREN) { 50 cb(); 51 } 52 } else if (e.data.type === "finished") { 53 finishedChildrenCount++; 54 55 if (lastTest && finishedChildrenCount === N_CHILDREN) { 56 postMessage({ type: "finished" }); 57 children = []; 58 close(); 59 } 60 } else if (e.data.type === "ok") { 61 // Pass on test to page. 62 postMessage(e.data); 63 } 64 }; 65 } 66 } 67 68 setupChildren(function () { 69 postMessage({ type: "ready" }); 70 });