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:
parent
2187b7ed82
commit
241c8609f1
|
|
@ -520,4 +520,9 @@ const api = {
|
||||||
|
|
||||||
export type JournalWorkerAPI = typeof 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);
|
||||||
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue