2019-11-28 11:33:26 +00:00
|
|
|
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;
|
|
|
|
constructor(taskArg: Task) {
|
|
|
|
this.task = taskArg;
|
|
|
|
}
|
|
|
|
|
2021-09-26 12:45:02 +00:00
|
|
|
public trigger(x: any): Promise<any> {
|
2019-09-23 16:06:43 +00:00
|
|
|
if (!(this.bufferCounter >= this.task.bufferMax)) {
|
|
|
|
this.bufferCounter++;
|
|
|
|
}
|
|
|
|
const returnPromise: Promise<any> = this.task.cycleCounter.getPromiseForCycle(
|
|
|
|
this.bufferCounter + 1
|
|
|
|
);
|
2020-09-07 17:35:50 +00:00
|
|
|
if (!this.task.running) {
|
2019-09-23 16:06:43 +00:00
|
|
|
this._run(x);
|
|
|
|
}
|
|
|
|
return returnPromise;
|
|
|
|
}
|
|
|
|
|
2021-09-26 12:45:02 +00:00
|
|
|
private _run(x: any) {
|
|
|
|
const recursiveBufferRunner = (x: any) => {
|
2019-09-23 16:06:43 +00:00
|
|
|
if (this.bufferCounter >= 0) {
|
|
|
|
this.task.running = true;
|
2020-07-12 00:48:51 +00:00
|
|
|
Task.runTask(this.task, { x: x }).then((x) => {
|
2020-09-07 17:35:50 +00:00
|
|
|
this.bufferCounter--; // this.bufferCounter drops below 0, the recursion stops.
|
2019-09-23 16:06:43 +00:00
|
|
|
this.task.cycleCounter.informOfCycle(x);
|
|
|
|
recursiveBufferRunner(x);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.task.running = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
recursiveBufferRunner(x);
|
|
|
|
}
|
2019-11-28 11:33:26 +00:00
|
|
|
}
|