mirror of
https://github.com/denoland/deno.git
synced 2025-01-05 05:49:20 -05:00
fix(runtime): early binding to dispatchEvent in workers (#10904)
This commit is contained in:
parent
c84c747ea4
commit
e779ea9565
4 changed files with 56 additions and 2 deletions
43
cli/tests/fix_worker_dispatchevent.ts
Normal file
43
cli/tests/fix_worker_dispatchevent.ts
Normal 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);
|
1
cli/tests/fix_worker_dispatchevent.ts.out
Normal file
1
cli/tests/fix_worker_dispatchevent.ts.out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
success
|
|
@ -3972,6 +3972,11 @@ console.log("finish");
|
||||||
output: "fix_tsc_file_exists.out",
|
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 {
|
itest!(es_private_fields {
|
||||||
args: "run --quiet --reload es_private_fields.js",
|
args: "run --quiet --reload es_private_fields.js",
|
||||||
output: "es_private_fields.js.out",
|
output: "es_private_fields.js.out",
|
||||||
|
|
|
@ -82,7 +82,12 @@ delete Object.prototype.__proto__;
|
||||||
}
|
}
|
||||||
|
|
||||||
let isClosing = false;
|
let isClosing = false;
|
||||||
|
let globalDispatchEvent;
|
||||||
|
|
||||||
async function pollForMessages() {
|
async function pollForMessages() {
|
||||||
|
if (!globalDispatchEvent) {
|
||||||
|
globalDispatchEvent = globalThis.dispatchEvent.bind(globalThis);
|
||||||
|
}
|
||||||
while (!isClosing) {
|
while (!isClosing) {
|
||||||
const bufferMsg = await core.opAsync("op_worker_get_message");
|
const bufferMsg = await core.opAsync("op_worker_get_message");
|
||||||
const data = core.deserialize(bufferMsg);
|
const data = core.deserialize(bufferMsg);
|
||||||
|
@ -96,7 +101,7 @@ delete Object.prototype.__proto__;
|
||||||
if (globalThis.onmessage) {
|
if (globalThis.onmessage) {
|
||||||
await globalThis.onmessage(msgEvent);
|
await globalThis.onmessage(msgEvent);
|
||||||
}
|
}
|
||||||
globalThis.dispatchEvent(msgEvent);
|
globalDispatchEvent(msgEvent);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
let handled = false;
|
let handled = false;
|
||||||
|
|
||||||
|
@ -120,7 +125,7 @@ delete Object.prototype.__proto__;
|
||||||
handled = ret === true;
|
handled = ret === true;
|
||||||
}
|
}
|
||||||
|
|
||||||
globalThis.dispatchEvent(errorEvent);
|
globalDispatchEvent(errorEvent);
|
||||||
if (errorEvent.defaultPrevented) {
|
if (errorEvent.defaultPrevented) {
|
||||||
handled = true;
|
handled = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue