Skip to content

Commit

Permalink
Add the ability to throw an exception from workers
Browse files Browse the repository at this point in the history
  • Loading branch information
nchevobbe committed Jul 31, 2020
1 parent 2620254 commit 8269027
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
14 changes: 13 additions & 1 deletion console-test-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ function spawnWorker({ log = false } = {}) {
delay: 1,
message: "worker created",
});
worker.postMessage({
type: "exception",
});
}

if (document.readyState !== "complete") {
Expand Down Expand Up @@ -196,7 +199,16 @@ function addWorkerElementToWorkerList(worker, url) {
message: `log-${++logCount}`,
});
});
li.append(info, logButton, terminateButton);

const errorButton = document.createElement("button");
errorButton.textContent = "Exception";
errorButton.classList.add("regular");
errorButton.addEventListener("click", () => {
worker.postMessage({
type: "exception",
});
});
li.append(info, logButton, errorButton, terminateButton);
workersList.append(li);
li.scrollIntoView();
}
15 changes: 9 additions & 6 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ onmessage = function (e) {
console.log("⚙️ Message received in worker", e, globalThis);
const { type } = e.data;

if (type == "delay") {
console.info("⚙️ delay, echoing ", e.data, "…");
setTimeout(() => {
console.log("⚙️ …echo");
postMessage(`··${e.data.message}··`);
}, e.data.delay);
switch (type) {
case "delay":
setTimeout(() => {
console.log("⚙️ …echo");
postMessage(`··${e.data.message}··`);
}, e.data.delay);
break;
case "exception":
globalThis.x.y.x;
}
};

0 comments on commit 8269027

Please sign in to comment.