export class ImgManager { imgCache: { [index: string]: HTMLImageElement } = {} constructor(private getImageBlob: (imageId: string) => Promise) {} getImg = (imageId: string): Promise => { if (imageId in this.imgCache) { return Promise.resolve(this.imgCache[imageId]) } return new Promise(r => { this.getImageBlob(imageId).then(blob => { const img = document.createElement("img") img.addEventListener("load", () => { this.imgCache[imageId] = img r(img) }) img.src = URL.createObjectURL(blob) }) }) } }