onLine_worker.js 1.5 KB

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