mirror of
https://github.com/xuthus83/pigallery2.git
synced 2024-11-03 21:04:03 +08:00
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import {RendererInput, ThumbnailWoker} from './ThumbnailWoker';
|
|
import {Config} from '../../../common/config/private/Config';
|
|
|
|
|
|
interface QueTask {
|
|
data: RendererInput;
|
|
promise: { resolve: Function, reject: Function };
|
|
}
|
|
|
|
|
|
export interface ITaskQue {
|
|
execute(input: any): Promise<any>;
|
|
}
|
|
|
|
export class TaskQue implements ITaskQue {
|
|
|
|
private tasks: QueTask[] = [];
|
|
private taskInProgress: number = 0;
|
|
private run = async () => {
|
|
if (this.tasks.length == 0 || this.taskInProgress >= this.size) {
|
|
return;
|
|
}
|
|
this.taskInProgress++;
|
|
const task = this.tasks.shift();
|
|
try {
|
|
task.promise.resolve(await ThumbnailWoker.render(task.data, Config.Server.thumbnail.processingLibrary));
|
|
} catch (err) {
|
|
task.promise.reject(err);
|
|
}
|
|
this.taskInProgress--;
|
|
process.nextTick(this.run);
|
|
};
|
|
|
|
constructor(private size: number) {
|
|
}
|
|
|
|
execute(input: RendererInput): Promise<void> {
|
|
return new Promise((resolve: Function, reject: Function) => {
|
|
this.tasks.push({
|
|
data: input,
|
|
promise: {resolve: resolve, reject: reject}
|
|
});
|
|
this.run();
|
|
});
|
|
}
|
|
}
|