Skip to content

Commit

Permalink
refactor: use arrow functions as listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
vansergen committed Dec 21, 2020
1 parent f10672c commit e1b592d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 50 deletions.
78 changes: 33 additions & 45 deletions src/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,15 +408,16 @@ export class WebsocketClient extends EventEmitter {
await this.sendMessage({ type: "unsubscribe", subscriptions });
}

private async sendMessage(message: MessageV2): Promise<void> {
private async sendMessage(data: MessageV2): Promise<void> {
const message = JSON.stringify(data);
const { v2: ws } = this.sockets;

if (!ws) {
throw new Error("Websocket is not initialized");
}

await new Promise<void>((resolve, reject) => {
ws.send(JSON.stringify(message), (error) => {
ws.send(message, (error) => {
if (error) {
reject(error);
} else {
Expand All @@ -431,30 +432,41 @@ export class WebsocketClient extends EventEmitter {
url: string | URL,
headers?: Record<string, string>
): Promise<void> {
if (this.sockets[symbol]) {
switch (this.sockets[symbol].readyState) {
case Websocket.CLOSING:
case Websocket.CONNECTING:
throw new Error(
`Could not connect. State: ${this.sockets[symbol].readyState}`
);
case Websocket.OPEN:
return;
default:
break;
}
switch (this.sockets[symbol]?.readyState) {
case Websocket.CLOSING:
case Websocket.CONNECTING:
throw new Error(
`Could not connect. State: ${this.sockets[symbol].readyState}`
);
case Websocket.OPEN:
return;
default:
break;
}

await new Promise<void>((resolve, reject) => {
this.sockets[symbol] = new Websocket(url, { headers: { ...headers } });
this.sockets[symbol].once("open", () => {
resolve();
});
this.sockets[symbol].once("open", resolve);
this.sockets[symbol].once("error", reject);
this.sockets[symbol].on("message", this.onMessage.bind(this, symbol));
this.sockets[symbol].on("open", this.onOpen.bind(this, symbol));
this.sockets[symbol].on("close", this.onClose.bind(this, symbol));
this.sockets[symbol].on("error", this.onError.bind(this, symbol));
this.sockets[symbol].on("message", (data: string) => {
try {
const message = JSON.parse(data) as WSMessage;
this.emit("message", message, symbol);
} catch (error) {
this.emit("error", error, symbol);
}
});
this.sockets[symbol].on("open", () => {
this.emit("open", symbol);
});
this.sockets[symbol].on("close", () => {
this.emit("close", symbol);
});
this.sockets[symbol].on("error", (error) => {
if (error) {
this.emit("error", error, symbol);
}
});
});
}

Expand All @@ -480,30 +492,6 @@ export class WebsocketClient extends EventEmitter {
});
}

private onMessage(symbol: string, data: string): void {
try {
const message = JSON.parse(data) as WSMessage;
this.emit("message", message, symbol);
} catch (error) {
this.onError(symbol, error);
}
}

private onOpen(symbol: string): void {
this.emit("open", symbol);
}

private onClose(symbol: string): void {
this.emit("close", symbol);
}

private onError(symbol: string, error: unknown): void {
if (!error) {
return;
}
this.emit("error", error, symbol);
}

public set nonce(nonce: () => number) {
this.#nonce = nonce;
}
Expand Down
10 changes: 5 additions & 5 deletions test/websocket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,8 @@ suite("WebsocketClient", () => {
await assert.rejects(client.unsubscribe(subscriptions), error);
});

suite(".socket listeners", () => {
suite(".onOpen()", () => {
suite("socket events", () => {
suite("open", () => {
test("emits `open`", async () => {
const clientConnect = new Promise<void>((resolve, reject) => {
client.once("open", (market) => {
Expand All @@ -666,7 +666,7 @@ suite("WebsocketClient", () => {
});
});

suite(".onClose()", () => {
suite("close", () => {
test("emits `close`", async () => {
const clientConnect = new Promise<void>((resolve, reject) => {
client.once("close", (market) => {
Expand All @@ -693,7 +693,7 @@ suite("WebsocketClient", () => {
});
});

suite(".onError()", () => {
suite("error", () => {
test("emits `error`", async () => {
const error = new Error("Something bad happened");
const clientConnect = new Promise<void>((resolve, reject) => {
Expand Down Expand Up @@ -728,7 +728,7 @@ suite("WebsocketClient", () => {
});
});

suite(".onMessage()", () => {
suite("message", () => {
test("emits `error` on bad JSON", async () => {
const error = new SyntaxError(
"Unexpected token N in JSON at position 0"
Expand Down

0 comments on commit e1b592d

Please sign in to comment.