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(ext/node): support multiple message listeners on MessagePort #23600

Merged
merged 1 commit into from
Apr 30, 2024
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
12 changes: 10 additions & 2 deletions ext/node/polyfills/worker_threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,9 +484,17 @@ function webMessagePortToNodeMessagePort(port: MessagePort) {
// deno-lint-ignore no-explicit-any
const _listener = (ev: any) => listener(ev.data);
if (name == "message") {
port.onmessage = _listener;
if (port.onmessage === null) {
port.onmessage = _listener;
} else {
port.addEventListener("message", _listener);
}
} else if (name == "messageerror") {
port.onmessageerror = _listener;
if (port.onmessageerror === null) {
port.onmessageerror = _listener;
} else {
port.addEventListener("messageerror", _listener);
}
} else if (name == "close") {
port.addEventListener("close", _listener);
} else {
Expand Down
21 changes: 21 additions & 0 deletions tests/unit_node/worker_threads_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,24 @@ Deno.test({
await worker.terminate();
},
});

Deno.test({
name:
"[node/worker_threads] MessagePort.on all message listeners are invoked",
async fn() {
const output: string[] = [];
const deferred = Promise.withResolvers<void>();
const { port1, port2 } = new workerThreads.MessageChannel();
port1.on("message", (msg) => output.push(msg));
port1.on("message", (msg) => output.push(msg + 2));
port1.on("message", (msg) => {
output.push(msg + 3);
deferred.resolve();
});
port2.postMessage("hi!");
await deferred.promise;
assertEquals(output, ["hi!", "hi!2", "hi!3"]);
port2.close();
port1.close();
},
});