fix(client): improved channels difference loading

This commit is contained in:
teidesu 2021-05-31 22:50:39 +03:00
parent 1e9c4de86d
commit d70ccaa1ad

View file

@ -425,14 +425,11 @@ async function _loadDifference(
for (const upd of diff.otherUpdates) { for (const upd of diff.otherUpdates) {
if (upd._ === 'updateChannelTooLong') { if (upd._ === 'updateChannelTooLong') {
if (upd.pts) {
this._cpts[upd.channelId] = upd.pts
this._cptsMod[upd.channelId] = upd.pts
}
await _loadChannelDifference.call( await _loadChannelDifference.call(
this, this,
upd.channelId, upd.channelId,
noDispatch noDispatch,
upd.pts
) )
continue continue
} }
@ -446,22 +443,20 @@ async function _loadDifference(
// we only need to check this for channels since for // we only need to check this for channels since for
// common pts it is guaranteed by the server // common pts it is guaranteed by the server
// (however i would not really trust telegram server lol) // (however i would not really trust telegram server lol)
let nextLocalPts let nextLocalPts: number | null = null
if (cid in this._cpts) nextLocalPts = this._cpts[cid] + ptsCount if (cid in this._cpts) nextLocalPts = this._cpts[cid] + ptsCount
else { else if (this._catchUpChannels) {
const saved = await this.storage.getChannelPts(cid) const saved = await this.storage.getChannelPts(cid)
if (saved) { if (saved) {
this._cpts[cid] = saved this._cpts[cid] = saved
nextLocalPts = saved + ptsCount nextLocalPts = saved + ptsCount
} else {
nextLocalPts = null
} }
} }
if (nextLocalPts) { if (nextLocalPts) {
if (nextLocalPts > pts) continue if (nextLocalPts > pts) continue
if (nextLocalPts < pts) { if (nextLocalPts < pts) {
await _loadChannelDifference.call(this, cid, noDispatch) await _loadChannelDifference.call(this, cid, noDispatch, pts)
continue continue
} }
} }
@ -487,7 +482,8 @@ async function _loadDifference(
async function _loadChannelDifference( async function _loadChannelDifference(
this: TelegramClient, this: TelegramClient,
channelId: number, channelId: number,
noDispatch?: NoDispatchIndex noDispatch?: NoDispatchIndex,
fallbackPts?: number
): Promise<void> { ): Promise<void> {
let channel let channel
try { try {
@ -498,12 +494,16 @@ async function _loadChannelDifference(
return return
} }
let pts = this._cpts[channelId] let _pts: number | null | undefined = this._cpts[channelId]
if (!pts) { if (!_pts && this._catchUpChannels) {
pts = (await this.storage.getChannelPts(channelId)) ?? 0 _pts = await this.storage.getChannelPts(channelId)
} }
if (!_pts) _pts = fallbackPts
if (!pts) return if (!_pts) return
// to make TS happy
let pts = _pts
for (;;) { for (;;) {
const diff = await this.call({ const diff = await this.call({
@ -641,14 +641,11 @@ export function _handleUpdate(
for (const upd of update.updates) { for (const upd of update.updates) {
if (upd._ === 'updateChannelTooLong') { if (upd._ === 'updateChannelTooLong') {
if (upd.pts) {
this._cpts[upd.channelId] = upd.pts
this._cptsMod[upd.channelId] = upd.pts
}
await _loadChannelDifference.call( await _loadChannelDifference.call(
this, this,
upd.channelId, upd.channelId,
noDispatchIndex noDispatchIndex,
upd.pts
) )
continue continue
} }
@ -690,7 +687,8 @@ export function _handleUpdate(
await _loadChannelDifference.call( await _loadChannelDifference.call(
this, this,
channelId, channelId,
noDispatchIndex noDispatchIndex,
pts
) )
continue continue
} else { } else {