1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 12:58:54 -05:00

fix(runtime): early binding to dispatchEvent in workers (#10904)

This commit is contained in:
Kitson Kelly 2021-06-09 22:21:02 +10:00 committed by GitHub
parent c84c747ea4
commit e779ea9565
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 2 deletions

View file

@ -0,0 +1,43 @@
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);

View file

@ -0,0 +1 @@
success

View file

@ -3972,6 +3972,11 @@ console.log("finish");
output: "fix_tsc_file_exists.out",
});
itest!(fix_worker_dispatchevent {
args: "run --quiet --reload fix_worker_dispatchevent.ts",
output: "fix_worker_dispatchevent.ts.out",
});
itest!(es_private_fields {
args: "run --quiet --reload es_private_fields.js",
output: "es_private_fields.js.out",

View file

@ -82,7 +82,12 @@ delete Object.prototype.__proto__;
}
let isClosing = false;
let globalDispatchEvent;
async function pollForMessages() {
if (!globalDispatchEvent) {
globalDispatchEvent = globalThis.dispatchEvent.bind(globalThis);
}
while (!isClosing) {
const bufferMsg = await core.opAsync("op_worker_get_message");
const data = core.deserialize(bufferMsg);
@ -96,7 +101,7 @@ delete Object.prototype.__proto__;
if (globalThis.onmessage) {
await globalThis.onmessage(msgEvent);
}
globalThis.dispatchEvent(msgEvent);
globalDispatchEvent(msgEvent);
} catch (e) {
let handled = false;
@ -120,7 +125,7 @@ delete Object.prototype.__proto__;
handled = ret === true;
}
globalThis.dispatchEvent(errorEvent);
globalDispatchEvent(errorEvent);
if (errorEvent.defaultPrevented) {
handled = true;
}