diff --git a/src/lib/appManagers/appStickersManager.ts b/src/lib/appManagers/appStickersManager.ts index bf67aa62..4d9dcf08 100644 --- a/src/lib/appManagers/appStickersManager.ts +++ b/src/lib/appManagers/appStickersManager.ts @@ -55,6 +55,7 @@ export class AppStickersManager { if(!this.getGreetingStickersPromise) { this.getGreetingStickersPromise = this.getStickersByEmoticon('👋⭐️', false).then(docs => { + if(!docs.length) throw 'NO_STICKERS'; this.greetingStickers = docs.slice() as Document.document[]; this.greetingStickers.sort((a, b) => Math.random() - Math.random()); }); diff --git a/src/lib/mtproto/apiFileManager.ts b/src/lib/mtproto/apiFileManager.ts index de5afa1d..cf521091 100644 --- a/src/lib/mtproto/apiFileManager.ts +++ b/src/lib/mtproto/apiFileManager.ts @@ -86,7 +86,12 @@ export class ApiFileManager { private downloadActives: {[dcId: string]: number} = {}; public webpConvertPromises: {[fileName: string]: CancellablePromise} = {}; - public refreshReferencePromises: {[referenceHex: string]: CancellablePromise} = {}; + public refreshReferencePromises: { + [referenceHex: string]: { + deferred: CancellablePromise, + ready: Promise + } + } = {}; private log: ReturnType = logger('AFM', LogTypes.Error | LogTypes.Log); private tempId = 0; @@ -96,7 +101,7 @@ export class ApiFileManager { constructor() { setInterval(() => { // clear old promises for(const hex in this.refreshReferencePromises) { - const deferred = this.refreshReferencePromises[hex]; + const {deferred} = this.refreshReferencePromises[hex]; if(deferred.isFulfilled || deferred.isRejected) { delete this.refreshReferencePromises[hex]; } @@ -256,29 +261,36 @@ export class ApiFileManager { private refreshReference(inputFileLocation: InputFileLocation) { const reference = (inputFileLocation as InputFileLocation.inputDocumentFileLocation).file_reference; const hex = bytesToHex(reference); - let promise = this.refreshReferencePromises[hex]; - const havePromise = !!promise; - if(!havePromise) { - promise = deferredPromise(); - this.refreshReferencePromises[hex] = promise; - } + let r = this.refreshReferencePromises[hex]; + if(!r) { + const deferred = deferredPromise(); - promise = promise.then(reference => { - if(hex === bytesToHex(reference)) { - throw 'REFERENCE_IS_NOT_REFRESHED'; - } - - return (inputFileLocation as InputFileLocation.inputDocumentFileLocation).file_reference = reference; - }); + r = this.refreshReferencePromises[hex] = { + deferred, + ready: deferred.then(reference => { + if(hex === bytesToHex(reference)) { + throw 'REFERENCE_IS_NOT_REFRESHED'; + } - if(havePromise) { - return promise; + (inputFileLocation as InputFileLocation.inputDocumentFileLocation).file_reference = reference; + }) + }; + + const timeout = setTimeout(() => { + this.log.error('Didn\'t refresh the reference:', inputFileLocation); + deferred.reject('REFERENCE_IS_NOT_REFRESHED'); + }, 60000); + + deferred.finally(() => { + clearTimeout(timeout); + }); + + const task = {type: 'refreshReference', payload: reference}; + notifySomeone(task); } - const task = {type: 'refreshReference', payload: reference}; - notifySomeone(task); - return this.refreshReferencePromises[hex] = promise; + return r.ready; } public downloadFile(options: DownloadOptions): CancellablePromise { diff --git a/src/lib/mtproto/mtproto.worker.ts b/src/lib/mtproto/mtproto.worker.ts index e11c6256..edf94f7f 100644 --- a/src/lib/mtproto/mtproto.worker.ts +++ b/src/lib/mtproto/mtproto.worker.ts @@ -88,7 +88,8 @@ const taskListeners = { refreshReference: (task: RefreshReferenceTaskResponse) => { const hex = bytesToHex(task.originalPayload); - const deferred = apiFileManager.refreshReferencePromises[hex]; + const r = apiFileManager.refreshReferencePromises[hex]; + const deferred = r?.deferred; if(deferred) { if(task.error) { deferred.reject(task.error);