Browse Source

Possible FILE_REFERENCE_EXPIRED fix

master
Eduard Kuzmenko 3 years ago
parent
commit
7d794758a1
  1. 1
      src/lib/appManagers/appStickersManager.ts
  2. 52
      src/lib/mtproto/apiFileManager.ts
  3. 3
      src/lib/mtproto/mtproto.worker.ts

1
src/lib/appManagers/appStickersManager.ts

@ -55,6 +55,7 @@ export class AppStickersManager { @@ -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());
});

52
src/lib/mtproto/apiFileManager.ts

@ -86,7 +86,12 @@ export class ApiFileManager { @@ -86,7 +86,12 @@ export class ApiFileManager {
private downloadActives: {[dcId: string]: number} = {};
public webpConvertPromises: {[fileName: string]: CancellablePromise<Uint8Array>} = {};
public refreshReferencePromises: {[referenceHex: string]: CancellablePromise<ReferenceBytes>} = {};
public refreshReferencePromises: {
[referenceHex: string]: {
deferred: CancellablePromise<ReferenceBytes>,
ready: Promise<void>
}
} = {};
private log: ReturnType<typeof logger> = logger('AFM', LogTypes.Error | LogTypes.Log);
private tempId = 0;
@ -96,7 +101,7 @@ export class ApiFileManager { @@ -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 { @@ -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<ReferenceBytes>();
this.refreshReferencePromises[hex] = promise;
}
let r = this.refreshReferencePromises[hex];
if(!r) {
const deferred = deferredPromise<ReferenceBytes>();
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<Blob> {

3
src/lib/mtproto/mtproto.worker.ts

@ -88,7 +88,8 @@ const taskListeners = { @@ -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);

Loading…
Cancel
Save