2018-07-29 03:27:05 +08:00
|
|
|
import {RendererInput, ThumbnailWorker} from './ThumbnailWorker';
|
2018-03-31 03:30:30 +08:00
|
|
|
import {Config} from '../../../common/config/private/Config';
|
2017-07-24 04:37:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
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[] = [];
|
2018-05-13 00:19:51 +08:00
|
|
|
private taskInProgress = 0;
|
2017-07-24 04:37:29 +08:00
|
|
|
private run = async () => {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (this.tasks.length === 0 || this.taskInProgress >= this.size) {
|
2017-07-24 04:37:29 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.taskInProgress++;
|
|
|
|
const task = this.tasks.shift();
|
|
|
|
try {
|
2018-07-29 01:58:17 +08:00
|
|
|
task.promise.resolve(await ThumbnailWorker.render(task.data, Config.Server.thumbnail.processingLibrary));
|
2017-07-24 04:37:29 +08:00
|
|
|
} 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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|