dom/workers/test/onLine_worker.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/workers/test/onLine_worker.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,65 @@
     1.4 +/*
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/licenses/publicdomain/
     1.7 + */
     1.8 +
     1.9 +importScripts("onLine_worker_head.js");
    1.10 +
    1.11 +var N_CHILDREN = 3;
    1.12 +var children = [];
    1.13 +var finishedChildrenCount = 0;
    1.14 +var lastTest = false;
    1.15 +
    1.16 +for (var event of ["online", "offline"]) {
    1.17 +  addEventListener(event,
    1.18 +                   makeHandler(
    1.19 +                     "addEventListener('%1', ..., false)",
    1.20 +                     event, 1, "Parent Worker"),
    1.21 +                   false);
    1.22 +}
    1.23 +
    1.24 +onmessage = function(e) {
    1.25 +  if (e.data === 'lastTest') {
    1.26 +    children.forEach(function(w) {
    1.27 +      w.postMessage({ type: 'lastTest' });
    1.28 +    });
    1.29 +    lastTest = true;
    1.30 +  }
    1.31 +}
    1.32 +
    1.33 +function setupChildren(cb) {
    1.34 +  var readyCount = 0;
    1.35 +  for (var i = 0; i < N_CHILDREN; ++i) {
    1.36 +    var w = new Worker("onLine_worker_child.js");
    1.37 +    children.push(w);
    1.38 +
    1.39 +    w.onerror = function(e) {
    1.40 +      info("Error creating child " + e.message);
    1.41 +    }
    1.42 +
    1.43 +    w.onmessage = function(e) {
    1.44 +      if (e.data.type === 'ready') {
    1.45 +        info("Got ready from child");
    1.46 +        readyCount++;
    1.47 +        if (readyCount === N_CHILDREN) {
    1.48 +          cb();
    1.49 +        }
    1.50 +      } else if (e.data.type === 'finished') {
    1.51 +        finishedChildrenCount++;
    1.52 +
    1.53 +        if (lastTest && finishedChildrenCount === N_CHILDREN) {
    1.54 +          postMessage({ type: 'finished' });
    1.55 +          children = [];
    1.56 +          close();
    1.57 +        }
    1.58 +      } else if (e.data.type === 'ok') {
    1.59 +        // Pass on test to page.
    1.60 +        postMessage(e.data);
    1.61 +      }
    1.62 +    }
    1.63 +  }
    1.64 +}
    1.65 +
    1.66 +setupChildren(function() {
    1.67 +  postMessage({ type: 'ready' });
    1.68 +});

mercurial