Fixed icon double loading

This commit is contained in:
WolverinDEV 2018-08-12 19:01:47 +02:00
parent e8fc074b53
commit 74906cdb9f

View file

@ -242,6 +242,7 @@ class Icon {
class IconManager { class IconManager {
handle: FileManager; handle: FileManager;
private loading_icons: {promise: Promise<Icon>, id: number}[] = [];
constructor(handle: FileManager) { constructor(handle: FileManager) {
this.handle = handle; this.handle = handle;
@ -266,18 +267,27 @@ class IconManager {
return undefined; 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<Icon> { loadIcon(id: number) : Promise<Icon> {
const _this = this; for(let entry of this.loading_icons)
return new Promise<Icon>((resolve, reject) => { if(entry.id == id) return entry.promise;
let promise = new Promise<Icon>((resolve, reject) => {
let icon = this.resolveCached(id); let icon = this.resolveCached(id);
if(icon){ if(icon){
this.load_finished(id);
resolve(icon); resolve(icon);
return; return;
} }
_this.downloadIcon(id).then(ft => { this.downloadIcon(id).then(ft => {
let array = new Uint8Array(0); let array = new Uint8Array(0);
ft.on_fail = reason => { ft.on_fail = reason => {
this.load_finished(id);
console.error("Could not download icon " + id + " -> " + reason); console.error("Could not download icon " + id + " -> " + reason);
chat.serverChat().appendError("Fail to download icon {0}. ({1})", id, JSON.stringify(reason)); chat.serverChat().appendError("Fail to download icon {0}. ({1})", id, JSON.stringify(reason));
reject(reason); reject(reason);
@ -287,15 +297,14 @@ class IconManager {
array = concatenate(Uint8Array, array, data); array = concatenate(Uint8Array, array, data);
}; };
ft.on_complete = () => { ft.on_complete = () => {
console.log("Length: " + array.length);
let base64 = btoa(String.fromCharCode.apply(null, array)); let base64 = btoa(String.fromCharCode.apply(null, array));
console.log("Length: " + array.length);
let icon = new Icon(); let icon = new Icon();
icon.base64 = base64; icon.base64 = base64;
icon.id = id; icon.id = id;
icon.name = "icon_" + id; icon.name = "icon_" + id;
localStorage.setItem("icon_" + id, JSON.stringify(icon)); localStorage.setItem("icon_" + id, JSON.stringify(icon));
this.load_finished(id);
resolve(icon); resolve(icon);
}; };
@ -306,6 +315,9 @@ class IconManager {
reject(reason); reject(reason);
}); });
}); });
this.loading_icons.push({promise: promise, id: id});
return promise;
} }
//$("<img width=\"16\" height=\"16\" alt=\"tick\" src=\"data:image/png;base64," + value.base64 + "\">") //$("<img width=\"16\" height=\"16\" alt=\"tick\" src=\"data:image/png;base64," + value.base64 + "\">")
@ -408,6 +420,7 @@ class AvatarManager {
let promise = new Promise<Avatar>((resolve, reject) => { let promise = new Promise<Avatar>((resolve, reject) => {
let avatar = this.resolveCached(client); let avatar = this.resolveCached(client);
if(avatar){ if(avatar){
this.load_finished(name);
resolve(avatar); resolve(avatar);
return; return;
} }