diff --git a/packages/core/src/base-client.ts b/packages/core/src/base-client.ts index c863d814..9016418a 100644 --- a/packages/core/src/base-client.ts +++ b/packages/core/src/base-client.ts @@ -327,13 +327,15 @@ export class BaseTelegramClient { // on reconnection we need to call updates.getState so Telegram // knows we still want the updates if (!this._disableUpdates) { - try { - await this.call({ _: 'updates.getState' }) - } catch (e) { - if (!(e instanceof RpcError)) { - this.primaryConnection.reconnect() + setTimeout(async () => { + try { + await this.call({ _: 'updates.getState' }) + } catch (e) { + if (!(e instanceof RpcError)) { + this.primaryConnection.reconnect() + } } - } + }, 0) } }) this.primaryConnection.on('update', (update) => { @@ -406,6 +408,15 @@ export class BaseTelegramClient { this.primaryConnection.connect() } + /** + * Wait until this client is usable (i.e. connection is fully ready) + */ + async waitUntilUsable(): Promise { + return new Promise((resolve) => { + this.primaryConnection.once('usable', resolve) + }) + } + /** * Close all connections and finalize the client. */