dom/workers/test/onLine_worker.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/licenses/publicdomain/
     4  */
     6 importScripts("onLine_worker_head.js");
     8 var N_CHILDREN = 3;
     9 var children = [];
    10 var finishedChildrenCount = 0;
    11 var lastTest = false;
    13 for (var event of ["online", "offline"]) {
    14   addEventListener(event,
    15                    makeHandler(
    16                      "addEventListener('%1', ..., false)",
    17                      event, 1, "Parent Worker"),
    18                    false);
    19 }
    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 }
    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);
    36     w.onerror = function(e) {
    37       info("Error creating child " + e.message);
    38     }
    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++;
    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 }
    63 setupChildren(function() {
    64   postMessage({ type: 'ready' });
    65 });

mercurial