2018-03-31 03:30:30 +08:00
|
|
|
import {RendererInput, ThumbnailWoker} from './ThumbnailWoker';
|
|
|
|
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[] = [];
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|