1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/model/threading/TaskExecuter.ts

38 lines
894 B
TypeScript
Raw Normal View History

2018-12-09 01:17:33 +08:00
import {TaskQue} from './TaskQue';
export interface ITaskExecuter<I, O> {
execute(input: I): Promise<O>;
}
export class TaskExecuter<I, O> implements ITaskExecuter<I, O> {
private taskQue = new TaskQue<I, O>();
private taskInProgress = 0;
private run = async () => {
if (this.taskQue.isEmpty() || this.taskInProgress >= this.size) {
return;
}
this.taskInProgress++;
const task = this.taskQue.get();
try {
task.promise.resolve(await this.worker(task.data));
} catch (err) {
task.promise.reject(err);
}
this.taskQue.ready(task);
this.taskInProgress--;
process.nextTick(this.run);
};
constructor(private size: number, private worker: (input: I) => Promise<O>) {
}
execute(input: I): Promise<O> {
const promise = this.taskQue.add(input).promise.obj;
this.run().catch(console.error);
return promise;
}
}