refactor: expose Comlink API via MessagePort in SharedWorker

Update the worker to use the 'onconnect' event to handle incoming
MessagePorts, ensuring each connecting tab receives its own port.
This commit is contained in:
hypercross 2026-07-12 10:14:36 +08:00
parent 2187b7ed82
commit 241c8609f1
1 changed files with 6 additions and 1 deletions

View File

@ -520,4 +520,9 @@ const api = {
export type JournalWorkerAPI = typeof api;
Comlink.expose(api);
// SharedWorkers communicate via MessagePort from the 'connect' event,
// not via self.onmessage. Each connecting tab gets its own port.
(self as unknown as { onconnect: ((e: MessageEvent) => void) | null }).onconnect = (e: MessageEvent) => {
const port = e.ports[0];
Comlink.expose(api, port);
};