/// import {Injectable} from "@angular/core"; import {GridPhoto} from "./GridPhoto"; import {Config} from "../../config/Config"; @Injectable() export class ThumbnailLoaderService { que:Array = []; runningRequests:number = 0; constructor() { } loadImage(gridPhoto:GridPhoto, onStartedLoading, onLoad, onError):void { console.log("[LOAD IMG]" + gridPhoto.photo.name); let tmp:ThumbnailTask = null; for (let i = 0; i < this.que.length; i++) { if (this.que[i].gridPhoto.getThumbnailPath() == gridPhoto.getThumbnailPath()) { tmp = this.que[i]; break; } } if (tmp != null) { tmp.onStartedLoading.push(onStartedLoading); tmp.onLoad.push(onLoad); tmp.onError.push(onError); } else { this.que.push({ gridPhoto: gridPhoto, onStartedLoading: [onStartedLoading], onLoad: [onLoad], onError: [onError] }); } this.run(); } run() { if (this.que.length === 0 || this.runningRequests >= Config.Client.concurrentThumbnailGenerations) { return; } this.runningRequests++; let task = this.que.shift(); task.onStartedLoading.forEach(cb=>cb()); console.log("loadingstarted: " + task.gridPhoto.getThumbnailPath()); let curImg = new Image(); curImg.src = task.gridPhoto.getThumbnailPath(); curImg.onload = () => { console.log(task.gridPhoto.getThumbnailPath() + "done"); task.gridPhoto.thumbnailLoaded(); task.onLoad.forEach(cb=>cb()); this.runningRequests--; this.run(); }; curImg.onerror = (error) => { console.error(task.gridPhoto.getThumbnailPath() + "error"); task.onLoad.forEach(cb=>cb(error)); this.runningRequests--; this.run(); }; } } interface ThumbnailTask { gridPhoto:GridPhoto; onStartedLoading:Array; onLoad:Array; onError:Array; }