diff --git a/src/Client.ts b/src/Client.ts index 3119525c..bb3de279 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -199,6 +199,8 @@ export class Client extends AsyncEventEmitter { readonly connectionFailureCount: Accessor; #setConnectionFailureCount: Setter; #reconnectTimeout: number | undefined; + #configurationPromise: Promise | undefined; + #connectAttempt = 0; /** * Create Stoat.js Client @@ -253,7 +255,7 @@ export class Client extends AsyncEventEmitter { this.configured = configured; this.#setConfigured = setConfigured; - this.#fetchConfiguration(); + this.#fetchConfiguration().catch((error) => this.emit("error", error)); const [ready, setReady] = createSignal(false); this.ready = ready; @@ -329,11 +331,24 @@ export class Client extends AsyncEventEmitter { * Connect to Revolt */ connect(): void { + this.#connect().catch((error) => this.emit("error", error)); + } + + async #connect(): Promise { + const attempt = ++this.#connectAttempt; + clearTimeout(this.#reconnectTimeout); this.events.disconnect(); this.#setReady(false); + + await this.#fetchConfiguration(); + + if (attempt !== this.#connectAttempt) { + return; + } + this.events.connect( - this.configuration?.ws ?? "wss://stoat.chat/events", + this.configuration?.ws || "wss://stoat.chat/events", typeof this.#session === "string" ? this.#session : this.#session!.token, ); } @@ -342,10 +357,23 @@ export class Client extends AsyncEventEmitter { * Fetches the configuration of the server if it has not been already fetched. */ async #fetchConfiguration(): Promise { - if (!this.configuration) { - this.configuration = await this.api.get("/"); - this.#setConfigured(true); + if (this.configuration) { + return; } + + if (!this.#configurationPromise) { + this.#configurationPromise = this.api + .get("/") + .then((configuration) => { + this.configuration = configuration; + this.#setConfigured(true); + }) + .finally(() => { + this.#configurationPromise = undefined; + }); + } + + await this.#configurationPromise; } /**