1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00
denoland-deno/cli/tests/testdata/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);