dom/workers/test/onLine_worker.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial