From 68fa36a6db1f62f5654e24e0106ba6bbb8cd8fc6 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Wed, 17 Jan 2024 22:47:10 +0700 Subject: [PATCH] fix(packages/api): ready event triggering even when not ready --- packages/api/src/classes/ClientGateway.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/api/src/classes/ClientGateway.ts b/packages/api/src/classes/ClientGateway.ts index 670f67a..1e4a302 100755 --- a/packages/api/src/classes/ClientGateway.ts +++ b/packages/api/src/classes/ClientGateway.ts @@ -10,7 +10,7 @@ import { uncapitalize, } from '@revanced/bot-shared' import type TypedEmitter from 'typed-emitter' -import { type RawData, WebSocket } from 'ws' +import { RawData, WebSocket } from 'ws' /** * The class that handles the WebSocket connection to the server. @@ -41,14 +41,16 @@ export default class ClientGateway { this.#socket.on('open', () => { this.disconnected = false + this.#listen() + this.ready = true + this.#emitter.emit('ready') rs() }) - this.#socket.on('close', () => this.#handleDisconnect(DisconnectReason.Generic)) + const errorHandler = () => this.#handleDisconnect(DisconnectReason.Generic) - this.#listen() - this.ready = true - this.#emitter.emit('ready') + this.#socket.on('close', errorHandler) + this.#socket.on('error', errorHandler) } catch (e) { rj(e) } @@ -112,7 +114,6 @@ export default class ClientGateway { */ disconnect() { this.#throwIfDisconnected('Cannot disconnect when already disconnected from the server') - this.#handleDisconnect(DisconnectReason.Generic) }