simpleThread_worker.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. "use strict";
  6. function messageListener(event) {
  7. var exception;
  8. try {
  9. event.bubbles = true;
  10. }
  11. catch(e) {
  12. exception = e;
  13. }
  14. if (!(exception instanceof TypeError)) {
  15. throw exception;
  16. }
  17. switch (event.data) {
  18. case "no-op":
  19. break;
  20. case "components":
  21. postMessage(Components.toString());
  22. break;
  23. case "start":
  24. for (var i = 0; i < 1000; i++) { }
  25. postMessage("started");
  26. break;
  27. case "stop":
  28. self.postMessage('no-op');
  29. postMessage("stopped");
  30. self.removeEventListener("message", messageListener, false);
  31. break;
  32. default:
  33. throw 'Bad message: ' + event.data;
  34. }
  35. }
  36. if (!("DedicatedWorkerGlobalScope" in self)) {
  37. throw new Error("DedicatedWorkerGlobalScope should be visible!");
  38. }
  39. if (!(self instanceof DedicatedWorkerGlobalScope)) {
  40. throw new Error("The global should be a SharedWorkerGlobalScope!");
  41. }
  42. if (!(self instanceof WorkerGlobalScope)) {
  43. throw new Error("The global should be a WorkerGlobalScope!");
  44. }
  45. if ("SharedWorkerGlobalScope" in self) {
  46. throw new Error("SharedWorkerGlobalScope should not be visible!");
  47. }
  48. addEventListener("message", { handleEvent: messageListener });