2016-06-17 06:05:31 +08:00
|
|
|
///<reference path="../../../browser.d.ts"/>
|
|
|
|
|
|
|
|
import {Injectable} from "@angular/core";
|
|
|
|
import {GridPhoto} from "./GridPhoto";
|
2016-06-17 17:25:15 +08:00
|
|
|
import {Config} from "../../config/Config";
|
2016-06-17 06:05:31 +08:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ThumbnailLoaderService {
|
|
|
|
|
|
|
|
que:Array<ThumbnailTask> = [];
|
2016-06-17 17:25:15 +08:00
|
|
|
runningRequests:number = 0;
|
2016-06-17 06:05:31 +08:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
2016-06-22 22:34:44 +08:00
|
|
|
loadImage(gridPhoto:GridPhoto, onStartedLoading, onLoad, onError):void {
|
2016-06-17 17:25:15 +08:00
|
|
|
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;
|
2016-06-17 06:05:31 +08:00
|
|
|
}
|
2016-06-17 17:25:15 +08:00
|
|
|
}
|
|
|
|
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();
|
2016-06-17 06:05:31 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
run() {
|
2016-06-17 17:25:15 +08:00
|
|
|
if (this.que.length === 0 || this.runningRequests >= Config.Client.concurrentThumbnailGenerations) {
|
2016-06-17 06:05:31 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-06-17 17:25:15 +08:00
|
|
|
this.runningRequests++;
|
2016-06-17 06:05:31 +08:00
|
|
|
let task = this.que.shift();
|
2016-06-22 22:34:44 +08:00
|
|
|
task.onStartedLoading.forEach(cb=>cb());
|
2016-06-17 06:05:31 +08:00
|
|
|
|
|
|
|
let curImg = new Image();
|
2016-06-17 17:25:15 +08:00
|
|
|
curImg.src = task.gridPhoto.getThumbnailPath();
|
2016-06-22 22:34:44 +08:00
|
|
|
curImg.onload = () => {
|
2016-06-17 17:25:15 +08:00
|
|
|
task.gridPhoto.thumbnailLoaded();
|
|
|
|
task.onLoad.forEach(cb=>cb());
|
|
|
|
this.runningRequests--;
|
2016-06-17 06:05:31 +08:00
|
|
|
this.run();
|
|
|
|
};
|
|
|
|
|
2016-06-22 22:34:44 +08:00
|
|
|
curImg.onerror = (error) => {
|
2016-06-17 17:25:15 +08:00
|
|
|
task.onLoad.forEach(cb=>cb(error));
|
|
|
|
this.runningRequests--;
|
2016-06-17 06:05:31 +08:00
|
|
|
this.run();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ThumbnailTask {
|
2016-06-17 17:25:15 +08:00
|
|
|
gridPhoto:GridPhoto;
|
|
|
|
onStartedLoading:Array<Function>;
|
|
|
|
onLoad:Array<Function>;
|
|
|
|
onError:Array<Function>;
|
2016-06-17 06:05:31 +08:00
|
|
|
}
|