1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/gallery/grid/thumnailLoader.service.ts
2016-06-17 11:25:15 +02:00

77 lines
2.2 KiB
TypeScript

///<reference path="../../../browser.d.ts"/>
import {Injectable} from "@angular/core";
import {GridPhoto} from "./GridPhoto";
import {Config} from "../../config/Config";
@Injectable()
export class ThumbnailLoaderService {
que:Array<ThumbnailTask> = [];
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<Function>;
onLoad:Array<Function>;
onError:Array<Function>;
}