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

215 lines
5.3 KiB
TypeScript
Raw Normal View History

2016-06-17 06:05:31 +08:00
import {Injectable} from "@angular/core";
import {GalleryCacheService} from "./cache.gallery.service";
import {Photo} from "./Photo";
import {IconPhoto} from "./IconPhoto";
import {PhotoDTO} from "../../../common/entities/PhotoDTO";
2017-06-04 21:25:08 +08:00
import {Config} from "../../../common/config/public/Config";
2016-06-17 06:05:31 +08:00
export enum ThumbnailLoadingPriority{
high, medium, low
}
2016-06-17 06:05:31 +08:00
@Injectable()
export class ThumbnailLoaderService {
que: Array<ThumbnailTask> = [];
runningRequests: number = 0;
2016-06-17 06:05:31 +08:00
2017-06-28 05:00:28 +08:00
constructor(private galleryCacheService: GalleryCacheService) {
}
2016-06-17 06:05:31 +08:00
2016-06-24 04:25:16 +08:00
removeTask(taskEntry: ThumbnailTaskEntity) {
2017-06-28 05:00:28 +08:00
let index = taskEntry.parentTask.taskEntities.indexOf(taskEntry);
if (index == -1) {
throw "ThumbnailTaskEntity not exist on Task";
}
taskEntry.parentTask.taskEntities.splice(index, 1);
2017-06-28 05:00:28 +08:00
if (taskEntry.parentTask.taskEntities.length == 0
&& taskEntry.parentTask.inProgress == false) {
let i = this.que.indexOf(taskEntry.parentTask);
if (i == -1) {
throw "ThumbnailTask not exist";
}
2017-06-28 05:00:28 +08:00
this.que.splice(i, 1);
}
}
loadIcon(photo: IconPhoto, priority: ThumbnailLoadingPriority, listener: ThumbnailLoadingListener): ThumbnailTaskEntity {
2017-06-28 05:00:28 +08:00
let thTask: ThumbnailTask = null;
//is image already qued?
for (let i = 0; i < this.que.length; i++) {
if (this.que[i].path == photo.getIconPath()) {
2017-06-28 05:00:28 +08:00
thTask = this.que[i];
break;
}
}
2017-06-28 05:00:28 +08:00
if (thTask == null) {
thTask = {
photo: photo.photo,
inProgress: false,
2017-06-28 05:00:28 +08:00
taskEntities: [],
onLoaded: () => {
photo.iconLoaded();
},
path: photo.getIconPath()
2017-06-28 05:00:28 +08:00
};
this.que.push(thTask);
}
let thumbnailTaskEntity = {priority: priority, listener: listener, parentTask: thTask};
thTask.taskEntities.push(thumbnailTaskEntity);
if (thTask.inProgress == true) {
listener.onStartedLoading();
}
2017-06-28 05:00:28 +08:00
2017-06-11 04:56:23 +08:00
setTimeout(this.run, 0);
return thumbnailTaskEntity;
}
loadImage(photo: Photo, priority: ThumbnailLoadingPriority, listener: ThumbnailLoadingListener): ThumbnailTaskEntity {
2017-06-28 05:00:28 +08:00
let thTask: ThumbnailTask = null;
//is image already qued?
for (let i = 0; i < this.que.length; i++) {
if (this.que[i].path == photo.getThumbnailPath()) {
2017-06-28 05:00:28 +08:00
thTask = this.que[i];
break;
}
}
2017-06-28 05:00:28 +08:00
if (thTask == null) {
thTask = {
photo: photo.photo,
inProgress: false,
2017-06-28 05:00:28 +08:00
taskEntities: [],
onLoaded: () => {
photo.thumbnailLoaded();
},
path: photo.getThumbnailPath()
2017-06-28 05:00:28 +08:00
};
this.que.push(thTask);
}
2017-06-28 05:00:28 +08:00
let thumbnailTaskEntity = {priority: priority, listener: listener, parentTask: thTask};
//add to task
thTask.taskEntities.push(thumbnailTaskEntity);
if (thTask.inProgress == true) {
listener.onStartedLoading();
}
2017-06-11 04:56:23 +08:00
setTimeout(this.run, 0);
return thumbnailTaskEntity;
}
private getNextTask(): ThumbnailTask {
if (this.que.length === 0) {
return null;
}
for (let i = 0; i < this.que.length; i++) {
for (let j = 0; j < this.que[i].taskEntities.length; j++) {
if (this.que[i].inProgress == false && this.que[i].taskEntities[j].priority === ThumbnailLoadingPriority.high) {
return this.que[i];
}
}
}
2016-06-17 06:05:31 +08:00
for (let i = 0; i < this.que.length; i++) {
for (let j = 0; j < this.que[i].taskEntities.length; j++) {
if (this.que[i].inProgress == false && this.que[i].taskEntities[j].priority === ThumbnailLoadingPriority.medium) {
return this.que[i];
}
}
}
for (let i = 0; i < this.que.length; i++) {
if (this.que[i].inProgress == false) {
return this.que[i];
}
2016-06-17 06:05:31 +08:00
}
return null;
}
private taskReady(task: ThumbnailTask) {
let i = this.que.indexOf(task);
if (i == -1) {
if (task.taskEntities.length !== 0) {
console.error("ThumbnailLoader: can't find task to remove");
}
return;
}
this.que.splice(i, 1);
}
2016-06-17 06:05:31 +08:00
run = () => {
if (this.que.length === 0 || this.runningRequests >= Config.Client.concurrentThumbnailGenerations) {
return;
}
let task = this.getNextTask();
if (task === null) {
return;
}
this.runningRequests++;
task.taskEntities.forEach(te => te.listener.onStartedLoading());
task.inProgress = true;
2017-06-28 05:00:28 +08:00
console.log("loading", task.path);
2016-06-24 04:25:16 +08:00
let curImg = new Image();
curImg.onload = () => {
task.onLoaded();
2017-06-28 05:00:28 +08:00
this.galleryCacheService.photoUpdated(task.photo);
task.taskEntities.forEach((te: ThumbnailTaskEntity) => te.listener.onLoad());
2016-06-17 06:05:31 +08:00
this.taskReady(task);
this.runningRequests--;
this.run();
};
2016-06-24 04:25:16 +08:00
curImg.onerror = (error) => {
task.taskEntities.forEach((te: ThumbnailTaskEntity) => te.listener.onError(error));
this.taskReady(task);
this.runningRequests--;
this.run();
};
curImg.src = task.path;
};
}
2016-06-17 06:05:31 +08:00
export interface ThumbnailLoadingListener {
onStartedLoading: () => void;
onLoad: () => void;
onError: (error: any) => void;
}
export interface ThumbnailTaskEntity {
priority: ThumbnailLoadingPriority;
listener: ThumbnailLoadingListener;
2017-06-28 05:00:28 +08:00
parentTask: ThumbnailTask;
2016-06-17 06:05:31 +08:00
}
interface ThumbnailTask {
photo: PhotoDTO;
inProgress: boolean;
taskEntities: Array<ThumbnailTaskEntity>;
path: string;
onLoaded: Function;
2016-06-17 06:05:31 +08:00
}