Fri, 16 Jan 2015 18:13:44 +0100
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/publicdomain/zero/1.0/ |
michael@0 | 4 | */ |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | function messageListener(event) { |
michael@0 | 8 | var exception; |
michael@0 | 9 | try { |
michael@0 | 10 | event.bubbles = true; |
michael@0 | 11 | } |
michael@0 | 12 | catch(e) { |
michael@0 | 13 | exception = e; |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | if (!(exception instanceof TypeError)) { |
michael@0 | 17 | throw exception; |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | switch (event.data) { |
michael@0 | 21 | case "no-op": |
michael@0 | 22 | break; |
michael@0 | 23 | case "components": |
michael@0 | 24 | postMessage(Components.toString()); |
michael@0 | 25 | break; |
michael@0 | 26 | case "start": |
michael@0 | 27 | for (var i = 0; i < 1000; i++) { } |
michael@0 | 28 | postMessage("started"); |
michael@0 | 29 | break; |
michael@0 | 30 | case "stop": |
michael@0 | 31 | self.postMessage('no-op'); |
michael@0 | 32 | postMessage("stopped"); |
michael@0 | 33 | self.removeEventListener("message", messageListener, false); |
michael@0 | 34 | break; |
michael@0 | 35 | default: |
michael@0 | 36 | throw 'Bad message: ' + event.data; |
michael@0 | 37 | } |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | if (!("DedicatedWorkerGlobalScope" in self)) { |
michael@0 | 41 | throw new Error("DedicatedWorkerGlobalScope should be visible!"); |
michael@0 | 42 | } |
michael@0 | 43 | if (!(self instanceof DedicatedWorkerGlobalScope)) { |
michael@0 | 44 | throw new Error("The global should be a SharedWorkerGlobalScope!"); |
michael@0 | 45 | } |
michael@0 | 46 | if (!(self instanceof WorkerGlobalScope)) { |
michael@0 | 47 | throw new Error("The global should be a WorkerGlobalScope!"); |
michael@0 | 48 | } |
michael@0 | 49 | if ("SharedWorkerGlobalScope" in self) { |
michael@0 | 50 | throw new Error("SharedWorkerGlobalScope should not be visible!"); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | addEventListener("message", { handleEvent: messageListener }); |