Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions cli/tests/fix_worker_dispatchevent.ts
@@ -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 change: 1 addition & 0 deletions cli/tests/fix_worker_dispatchevent.ts.out
@@ -0,0 +1 @@
success
5 changes: 5 additions & 0 deletions cli/tests/integration_tests.rs
Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions runtime/js/99_main.js
Expand Up @@ -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);
Expand All @@ -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;

Expand All @@ -120,7 +125,7 @@ delete Object.prototype.__proto__;
handled = ret === true;
}

globalThis.dispatchEvent(errorEvent);
globalDispatchEvent(errorEvent);
if (errorEvent.defaultPrevented) {
handled = true;
}
Expand Down