0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-01 09:24:20 -04:00
denoland-deno/cli/tests/testdata/run/fix_worker_dispatchevent.ts

44 lines
931 B
TypeScript
Raw Normal View History

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);