From 74906cdb9f9a06815024e60cd91a92a119c086f7 Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Sun, 12 Aug 2018 19:01:47 +0200 Subject: [PATCH] Fixed icon double loading --- js/FileManager.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/js/FileManager.ts b/js/FileManager.ts index 477c94ed..874a213e 100644 --- a/js/FileManager.ts +++ b/js/FileManager.ts @@ -242,6 +242,7 @@ class Icon { class IconManager { handle: FileManager; + private loading_icons: {promise: Promise, id: number}[] = []; constructor(handle: FileManager) { this.handle = handle; @@ -266,18 +267,27 @@ class IconManager { return undefined; } + private load_finished(id: number) { + for(let entry of this.loading_icons) + if(entry.id == id) + this.loading_icons.remove(entry); + } loadIcon(id: number) : Promise { - const _this = this; - return new Promise((resolve, reject) => { + for(let entry of this.loading_icons) + if(entry.id == id) return entry.promise; + + let promise = new Promise((resolve, reject) => { let icon = this.resolveCached(id); if(icon){ + this.load_finished(id); resolve(icon); return; } - _this.downloadIcon(id).then(ft => { + this.downloadIcon(id).then(ft => { let array = new Uint8Array(0); ft.on_fail = reason => { + this.load_finished(id); console.error("Could not download icon " + id + " -> " + reason); chat.serverChat().appendError("Fail to download icon {0}. ({1})", id, JSON.stringify(reason)); reject(reason); @@ -287,15 +297,14 @@ class IconManager { array = concatenate(Uint8Array, array, data); }; ft.on_complete = () => { - console.log("Length: " + array.length); let base64 = btoa(String.fromCharCode.apply(null, array)); - console.log("Length: " + array.length); let icon = new Icon(); icon.base64 = base64; icon.id = id; icon.name = "icon_" + id; localStorage.setItem("icon_" + id, JSON.stringify(icon)); + this.load_finished(id); resolve(icon); }; @@ -306,6 +315,9 @@ class IconManager { reject(reason); }); }); + + this.loading_icons.push({promise: promise, id: id}); + return promise; } //$("\"tick\"") @@ -408,6 +420,7 @@ class AvatarManager { let promise = new Promise((resolve, reject) => { let avatar = this.resolveCached(client); if(avatar){ + this.load_finished(name); resolve(avatar); return; }