Skip to content

Commit bc91eab

Browse files
authored
enhance type safety for websocket.js (#739)
* enhance type safety for websocket.js * correction for linter * revert automatic prettier modifications
1 parent 8125185 commit bc91eab

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

public/websocket.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
/**
2+
* @typedef {string | object} Spec
3+
*/
14

25
export class WebSocketClient extends EventTarget {
36
/** @type {WebSocket} */
47
client;
58

69
constructor(
10+
/** @type {String} */
711
url
812
) {
913
super();
1014
this.client = new WebSocket(url);
1115
this.client.addEventListener("message", this.#messageHandler.bind(this));
16+
17+
/**
18+
* @type {{ search: (spec: Spec) => void, remove: (spec: Spec) => void }}
19+
*/
1220
this.commands = {
1321
search: (spec) => this.send({ commandName: "SEARCH", spec }),
1422
remove: (spec) => this.send({ commandName: "REMOVE", spec })
@@ -20,11 +28,16 @@ export class WebSocketClient extends EventTarget {
2028
};
2129
}
2230

31+
/** @type {(data: Record<string, any>) => void} */
2332
send(data) {
2433
this.client.send(JSON.stringify(data));
2534
}
2635

36+
/**
37+
* @param {MessageEvent} event
38+
*/
2739
#messageHandler(event) {
40+
/** @type {{ status: string, [key: string]: any }} */
2841
const data = JSON.parse(event.data);
2942
if (!data.status) {
3043
console.warn(

0 commit comments

Comments
 (0)