MCPcopy Create free account
hub / github.com/achrafl0/multi-window-article / constructor

Method constructor

src/workerHandler.ts:14–41  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12 worker: SharedWorker;
13
14 constructor() {
15 this.worker = new SharedWorker(new URL("worker.ts", import.meta.url));
16 const connectedMessage: WorkerMessage = {
17 action: "connected",
18 payload: { state: this.currentWindow },
19 };
20 this.onSyncCallback = this.onSyncCallback.bind(this);
21 this.worker.port.postMessage(connectedMessage);
22 this.worker.port.onmessage = (ev: MessageEvent<WorkerMessage>) => {
23 const msg = ev.data;
24 switch (msg.action) {
25 case "attributedId": {
26 this.id = msg.payload.id;
27 break;
28 }
29 case "sync": {
30 this.onSyncCallback(msg.payload.allWindows);
31 break;
32 }
33 }
34 };
35 window.addEventListener("beforeunload", () => {
36 this.worker.port.postMessage({
37 action: "windowUnloaded",
38 payload: { id: this.id },
39 } satisfies WorkerMessage);
40 });
41 }
42
43 private onSyncCallback(allWindows: StateWithId[]) {
44 this.currentWindow = getCurrentWindowState();

Callers

nothing calls this directly

Calls 1

onSyncCallbackMethod · 0.95

Tested by

no test coverage detected