taskbuffer/ts/taskbuffer.classes.bufferrunner.ts

43 lines
1.0 KiB
TypeScript
Raw Normal View History

import { Task } from './taskbuffer.classes.task';
2019-09-23 16:06:43 +00:00
export class BufferRunner {
public task: Task;
// initialze by default
public bufferCounter: number = 0;
public running: boolean = false;
constructor(taskArg: Task) {
this.task = taskArg;
}
public trigger(x): Promise<any> {
if (!(this.bufferCounter >= this.task.bufferMax)) {
this.bufferCounter++;
}
const returnPromise: Promise<any> = this.task.cycleCounter.getPromiseForCycle(
this.bufferCounter + 1
);
if (!this.running) {
this._run(x);
}
return returnPromise;
}
private _run(x) {
const recursiveBufferRunner = x => {
if (this.bufferCounter >= 0) {
this.running = true;
this.task.running = true;
Task.runTask(this.task, { x: x }).then(x => {
this.bufferCounter--;
this.task.cycleCounter.informOfCycle(x);
recursiveBufferRunner(x);
});
} else {
this.running = false;
this.task.running = false;
}
};
recursiveBufferRunner(x);
}
}