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) {
2020-07-12 00:48:51 +00:00
const recursiveBufferRunner = (x) => {
2019-09-23 16:06:43 +00:00
if (this.bufferCounter >= 0) {
this.running = true;
this.task.running = true;
2020-07-12 00:48:51 +00:00
Task.runTask(this.task, { x: x }).then((x) => {
2019-09-23 16:06:43 +00:00
this.bufferCounter--;
this.task.cycleCounter.informOfCycle(x);
recursiveBufferRunner(x);
});
} else {
this.running = false;
this.task.running = false;
}
};
recursiveBufferRunner(x);
}
}