taskbuffer/ts/taskbuffer.classes.bufferrunner.ts
2023-08-02 02:30:13 +02:00

35 lines
849 B
TypeScript

import { Task } from './taskbuffer.classes.task.js';
export class BufferRunner {
public task: Task;
// initialize by default
public bufferCounter: number = 0;
constructor(taskArg: Task<any>) {
this.task = taskArg;
}
public trigger(x: any): Promise<any> {
if (!(this.bufferCounter >= this.task.bufferMax)) {
this.bufferCounter++;
}
const returnPromise: Promise<any> = this.task.cycleCounter.getPromiseForCycle(
this.bufferCounter
);
if (!this.task.running) {
this._run(x);
}
return returnPromise;
}
private async _run(x: any) {
this.task.running = true;
while (this.bufferCounter > 0) {
const result = await Task.runTask(this.task, { x: x });
this.bufferCounter--;
this.task.cycleCounter.informOfCycle(result);
}
this.task.running = false;
}
}