From be0f4f7d07546e9478cabe608745adbca587cae3 Mon Sep 17 00:00:00 2001 From: ZeEarth Date: Tue, 7 Apr 2026 09:26:56 +0200 Subject: [PATCH 1/3] enhance type safety for websocket.js --- public/websocket.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/public/websocket.js b/public/websocket.js index bdf382d8..b8eb30c2 100644 --- a/public/websocket.js +++ b/public/websocket.js @@ -1,17 +1,23 @@ +/** + * @typedef {string | object} Spec + */ export class WebSocketClient extends EventTarget { /** @type {WebSocket} */ client; constructor( + /** @type {String} */ url ) { super(); this.client = new WebSocket(url); this.client.addEventListener("message", this.#messageHandler.bind(this)); this.commands = { - search: (spec) => this.send({ commandName: "SEARCH", spec }), - remove: (spec) => this.send({ commandName: "REMOVE", spec }) + search: /** @param {Spec} spec */ (spec) => + this.send({ commandName: "SEARCH", spec }), + remove: /** @param {Spec} spec */ (spec) => + this.send({ commandName: "REMOVE", spec }), }; window.socket = this; @@ -20,25 +26,25 @@ export class WebSocketClient extends EventTarget { }; } + /** @type {(data: Record) => void} */ send(data) { this.client.send(JSON.stringify(data)); } + /** + * @param {MessageEvent} event + */ #messageHandler(event) { + /** @type {{ status: string, [key: string]: any }} */ const data = JSON.parse(event.data); if (!data.status) { - console.warn( - "[WEBSOCKET] Received data without status:", - data - ); + console.warn("[WEBSOCKET] Received data without status:", data); return; } console.log(`[WEBSOCKET] data status = '${data.status}'`); - this.dispatchEvent( - new CustomEvent(data.status, { detail: data }) - ); + this.dispatchEvent(new CustomEvent(data.status, { detail: data })); } close() { From 3617d3dd6b88e6cf19be1cec54f04ace4006f473 Mon Sep 17 00:00:00 2001 From: ZeEarth Date: Tue, 7 Apr 2026 23:52:20 +0200 Subject: [PATCH 2/3] correction for linter --- public/websocket.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/public/websocket.js b/public/websocket.js index b8eb30c2..b65f4475 100644 --- a/public/websocket.js +++ b/public/websocket.js @@ -13,11 +13,13 @@ export class WebSocketClient extends EventTarget { super(); this.client = new WebSocket(url); this.client.addEventListener("message", this.#messageHandler.bind(this)); + + /** + * @type {{ search: (spec: Spec) => void, remove: (spec: Spec) => void }} + */ this.commands = { - search: /** @param {Spec} spec */ (spec) => - this.send({ commandName: "SEARCH", spec }), - remove: /** @param {Spec} spec */ (spec) => - this.send({ commandName: "REMOVE", spec }), + search: (spec) => this.send({ commandName: "SEARCH", spec }), + remove: (spec) => this.send({ commandName: "REMOVE", spec }) }; window.socket = this; From 86272549f3fc0de1ebca742238e0506d46e1d2e1 Mon Sep 17 00:00:00 2001 From: ZeEarth Date: Thu, 9 Apr 2026 08:02:56 +0200 Subject: [PATCH 3/3] revert automatic prettier modifications --- public/websocket.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/public/websocket.js b/public/websocket.js index b65f4475..195aef07 100644 --- a/public/websocket.js +++ b/public/websocket.js @@ -40,13 +40,18 @@ export class WebSocketClient extends EventTarget { /** @type {{ status: string, [key: string]: any }} */ const data = JSON.parse(event.data); if (!data.status) { - console.warn("[WEBSOCKET] Received data without status:", data); + console.warn( + "[WEBSOCKET] Received data without status:", + data + ); return; } console.log(`[WEBSOCKET] data status = '${data.status}'`); - this.dispatchEvent(new CustomEvent(data.status, { detail: data })); + this.dispatchEvent( + new CustomEvent(data.status, { detail: data }) + ); } close() {