From 5fee1d0660d7314a801bf55a687c97a4a1005517 Mon Sep 17 00:00:00 2001 From: teidesu Date: Mon, 26 Apr 2021 23:26:29 +0300 Subject: [PATCH] fix(client): better storage handling i suppose?? i mean, it's definitely better than randomly losing updates state and entities, but this might be much slower? --- packages/client/src/methods/updates.ts | 29 +++++++++++++++----------- packages/core/src/base-client.ts | 3 +-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/client/src/methods/updates.ts b/packages/client/src/methods/updates.ts index 708aafff..8911ef42 100644 --- a/packages/client/src/methods/updates.ts +++ b/packages/client/src/methods/updates.ts @@ -93,19 +93,23 @@ export async function _loadStorage(this: TelegramClient): Promise { export async function _saveStorage(this: TelegramClient): Promise { // save updates state to the session - // before any authorization pts will be undefined - if (this._pts !== undefined) { - await this.storage.setCommonPts([this._pts, this._date]) // , this._seq]) - await this.storage.setManyChannelPts(this._cpts) - } - if (this._userId !== null) { - await this.storage.setSelf({ - userId: this._userId, - isBot: this._isBot, - }) - } + try { + // before any authorization pts will be undefined + if (this._pts !== undefined) { + await this.storage.setCommonPts([this._pts, this._date]) // , this._seq]) + await this.storage.setManyChannelPts(this._cpts) + } + if (this._userId !== null) { + await this.storage.setSelf({ + userId: this._userId, + isBot: this._isBot, + }) + } - await this.storage.save?.() + await this.storage.save?.() + } catch (err) { + this._emitError(err) + } } /** @@ -751,6 +755,7 @@ export function _handleUpdate( }) .catch((err) => this._emitError(err)) .then(() => this._updLock.release()) + .then(() => this._saveStorage()) } /** diff --git a/packages/core/src/base-client.ts b/packages/core/src/base-client.ts index 979974fd..c01eb38b 100644 --- a/packages/core/src/base-client.ts +++ b/packages/core/src/base-client.ts @@ -311,8 +311,6 @@ export class BaseTelegramClient { }) this.primaryConnection.on('usable', async () => { this._keepAliveInterval = setInterval(async () => { - await this._saveStorage() - // according to telethon, "We need to send some content-related request at least hourly // for Telegram to keep delivering updates, otherwise they will just stop even if we're connected. // Do so every 30 minutes" @@ -690,5 +688,6 @@ export class BaseTelegramClient { } await this.storage.updatePeers(parsedPeers) + await this._saveStorage() } }