mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 07:14:47 -05:00
44 lines
931 B
TypeScript
44 lines
931 B
TypeScript
|
const code = `
|
||
|
addEventListener("message", () => {
|
||
|
postMessage("pong");
|
||
|
});
|
||
|
|
||
|
const context = new EventTarget();
|
||
|
|
||
|
Object.defineProperty(globalThis, "dispatchEvent", {
|
||
|
value: context.dispatchEvent.bind(context),
|
||
|
writable: true,
|
||
|
enumerable: true,
|
||
|
configurable: true,
|
||
|
});
|
||
|
|
||
|
postMessage("start");
|
||
|
`;
|
||
|
|
||
|
const blob = new Blob([code], { type: "application/javascript" });
|
||
|
|
||
|
const url = URL.createObjectURL(blob);
|
||
|
|
||
|
const worker = new Worker(url, { type: "module" });
|
||
|
|
||
|
let terminated = false;
|
||
|
|
||
|
worker.addEventListener("message", (evt) => {
|
||
|
if (evt.data === "start") {
|
||
|
worker.postMessage("ping");
|
||
|
} else if (evt.data === "pong") {
|
||
|
worker.terminate();
|
||
|
terminated = true;
|
||
|
console.log("success");
|
||
|
} else {
|
||
|
throw new Error("unexpected message from worker");
|
||
|
}
|
||
|
});
|
||
|
|
||
|
setTimeout(() => {
|
||
|
if (!terminated) {
|
||
|
worker.terminate();
|
||
|
throw new Error("did not receive message from worker in time");
|
||
|
}
|
||
|
}, 2000);
|