feat(packages/api): allow setting new options for ClientWebsocketManager

This commit is contained in:
PalmDevs
2024-08-05 00:43:05 +07:00
parent de8bef6520
commit c567ef25c6

View File

@@ -17,7 +17,7 @@ import { type RawData, WebSocket } from 'ws'
* This is the only relevant class for the time being. But in the future, there may be more classes to handle different protocols of the API.
*/
export class ClientWebSocketManager {
readonly url: string
url: string
timeout: number
connecting = false
@@ -33,6 +33,21 @@ export class ClientWebSocketManager {
this.timeout = options.timeout ?? 10000
}
/**
* Sets the URL to connect to
*
* **Requires a reconnect to take effect**
*/
async setOptions({ url, timeout }: Partial<ClientWebSocketManagerOptions>, autoReconnect = true) {
if (url) this.url = url
this.timeout = timeout ?? this.timeout
if (autoReconnect) {
this.disconnect(true)
await this.connect()
}
}
/**
* Connects to the WebSocket API
* @returns A promise that resolves when the client is ready