fix(client): disable catch up by default, also save storage after catching up

This commit is contained in:
teidesu 2021-04-26 22:41:19 +03:00
parent a4f6c42bf5
commit b4a26c6f5b
2 changed files with 22 additions and 12 deletions

View file

@ -111,29 +111,40 @@ export async function start(
/** /**
* Whether to "catch up" (load missed updates). * Whether to "catch up" (load missed updates).
* Note: you should register your handlers * Only applicable if the saved session already
* before calling `start()` * contained authorization and updates state.
* *
* Defaults to true. * Note: you should register your handlers
* before calling `start()`, otherwise they will
* not be called.
*
* Note: In case the storage was not properly
* closed the last time, "catching up" might
* result in duplicate updates.
*
* Defaults to `false`.
*/ */
catchUp?: boolean catchUp?: boolean
} }
): Promise<User> { ): Promise<User> {
if (!params.phone && !params.botToken)
throw new MtCuteArgumentError(
'Neither phone nor bot token were provided'
)
try { try {
const me = await this.getMe() const me = await this.getMe()
// user is already authorized // user is already authorized
if (params.catchUp !== false && !this._disableUpdates)
if (!this._disableUpdates && params.catchUp)
await this.catchUp() await this.catchUp()
return me return me
} catch (e) { } catch (e) {
if (!(e instanceof AuthKeyUnregisteredError)) throw e if (!(e instanceof AuthKeyUnregisteredError)) throw e
} }
if (!params.phone && !params.botToken)
throw new MtCuteArgumentError(
'Neither phone nor bot token were provided'
)
let phone = params.phone ? await resolveMaybeDynamic(params.phone) : null let phone = params.phone ? await resolveMaybeDynamic(params.phone) : null
if (phone) { if (phone) {
phone = normalizePhoneNumber(phone) phone = normalizePhoneNumber(phone)
@ -149,9 +160,7 @@ export async function start(
'Either bot token or phone number must be provided' 'Either bot token or phone number must be provided'
) )
if (params.catchUp !== false) await this.catchUp() return await this.signInBot(botToken)
return this.signInBot(botToken)
} }
let sentCode = await this.sendCode(phone) let sentCode = await this.sendCode(phone)

View file

@ -767,4 +767,5 @@ export function catchUp(this: TelegramClient): Promise<void> {
.then(() => _loadDifference.call(this)) .then(() => _loadDifference.call(this))
.catch((err) => this._emitError(err)) .catch((err) => this._emitError(err))
.then(() => this._updLock.release()) .then(() => this._updLock.release())
.then(() => this._saveStorage())
} }