taskbuffer/ts/taskbuffer.classes.helpers.ts

167 lines
4.2 KiB
TypeScript
Raw Normal View History

2018-08-04 15:53:22 +00:00
import plugins = require('./taskbuffer.plugins');
import { Task, ITaskFunction } from './taskbuffer.classes.task';
2019-09-18 17:15:56 +00:00
export const emptyTaskFunction: ITaskFunction = function(x) {
const done = plugins.smartpromise.defer();
2018-08-04 15:53:22 +00:00
done.resolve();
return done.promise;
};
2019-09-18 17:15:56 +00:00
export const isTask = (taskArg: Task): boolean => {
2018-08-04 15:53:22 +00:00
if (taskArg instanceof Task && typeof taskArg.taskFunction === 'function') {
return true;
2017-02-15 21:52:29 +00:00
} else {
2018-08-04 15:53:22 +00:00
return false;
2017-02-15 21:52:29 +00:00
}
2018-08-04 15:53:22 +00:00
};
2016-05-04 00:49:43 +00:00
2019-09-18 17:15:56 +00:00
export const isTaskTouched = (taskArg: Task, touchedTasksArray: Task[]): boolean => {
2018-08-04 15:53:22 +00:00
let result = false;
2019-09-18 17:15:56 +00:00
for (const keyArg in touchedTasksArray) {
2018-08-04 15:53:22 +00:00
if (taskArg === touchedTasksArray[keyArg]) {
result = true;
2016-05-06 00:05:45 +00:00
}
2017-02-15 21:52:29 +00:00
}
2018-08-04 15:53:22 +00:00
return result;
};
2019-09-18 17:15:56 +00:00
export const runTask = async (taskArg: Task, optionsArg: { x?; touchedTasksArray?: Task[] }) => {
const done = plugins.smartpromise.defer();
2017-02-15 21:52:29 +00:00
2017-07-10 10:42:06 +00:00
// pay respect to execDelay
if (taskArg.execDelay) {
2018-08-04 15:53:22 +00:00
await plugins.smartdelay.delayFor(taskArg.execDelay);
2017-07-10 10:42:06 +00:00
}
2017-02-15 21:52:29 +00:00
// set running params
2018-08-04 15:53:22 +00:00
taskArg.running = true;
2017-07-10 10:42:06 +00:00
2018-08-04 15:53:22 +00:00
done.promise.then(async () => {
taskArg.running = false;
});
2017-02-15 21:52:29 +00:00
// handle options
2019-09-18 17:15:56 +00:00
const options = {
2018-08-04 15:53:22 +00:00
...{ x: undefined, touchedTasksArray: [] },
...optionsArg
};
2019-09-18 17:15:56 +00:00
const x = options.x;
const touchedTasksArray: Task[] = options.touchedTasksArray;
2017-02-15 21:52:29 +00:00
2018-08-04 15:53:22 +00:00
touchedTasksArray.push(taskArg);
2017-02-15 21:52:29 +00:00
// run the task cascade
2019-09-18 17:15:56 +00:00
const localDeferred = plugins.smartpromise.defer();
2017-02-15 21:52:29 +00:00
localDeferred.promise
.then(() => {
2019-09-18 17:15:56 +00:00
// lets run any preTask
2017-02-15 21:52:29 +00:00
if (taskArg.preTask && !isTaskTouched(taskArg.preTask, touchedTasksArray)) {
2019-09-18 17:15:56 +00:00
return runTask(taskArg.preTask, { x, touchedTasksArray });
2017-02-15 21:52:29 +00:00
} else {
2019-09-18 17:15:56 +00:00
const done2 = plugins.smartpromise.defer();
2018-08-04 15:53:22 +00:00
done2.resolve(x);
return done2.promise;
2017-02-15 21:52:29 +00:00
}
})
2019-09-18 17:15:56 +00:00
.then(async x => {
// lets run the main task
try {
return await taskArg.taskFunction(x);
} catch (e) {
console.log(e);
}
2017-02-15 21:52:29 +00:00
})
.then(x => {
if (taskArg.afterTask && !isTaskTouched(taskArg.afterTask, touchedTasksArray)) {
2018-08-04 15:53:22 +00:00
return runTask(taskArg.afterTask, { x: x, touchedTasksArray: touchedTasksArray });
2017-02-15 21:52:29 +00:00
} else {
2019-09-18 17:15:56 +00:00
const done2 = plugins.smartpromise.defer();
2018-08-04 15:53:22 +00:00
done2.resolve(x);
return done2.promise;
2017-02-15 21:52:29 +00:00
}
})
.then(x => {
2018-08-04 15:53:22 +00:00
done.resolve(x);
2017-02-15 21:52:29 +00:00
})
2018-08-04 15:53:22 +00:00
.catch(err => {
console.log(err);
});
localDeferred.resolve();
return await done.promise;
};
2016-05-05 17:21:01 +00:00
2016-08-01 14:10:00 +00:00
export interface cycleObject {
2018-08-04 15:53:22 +00:00
cycleCounter: number;
deferred: plugins.smartpromise.Deferred<any>;
2016-08-01 14:10:00 +00:00
}
export class CycleCounter {
2018-08-04 15:53:22 +00:00
task: Task;
cycleObjectArray: cycleObject[] = [];
2017-02-15 21:52:29 +00:00
constructor(taskArg: Task) {
2018-08-04 15:53:22 +00:00
this.task = taskArg;
2017-06-09 21:33:41 +00:00
}
2018-08-04 15:53:22 +00:00
getPromiseForCycle(cycleCountArg: number) {
2019-09-18 17:15:56 +00:00
const done = plugins.smartpromise.defer();
const cycleObject: cycleObject = {
2017-02-15 21:52:29 +00:00
cycleCounter: cycleCountArg,
deferred: done
2018-08-04 15:53:22 +00:00
};
this.cycleObjectArray.push(cycleObject);
return done.promise;
2017-06-09 21:33:41 +00:00
}
2018-08-04 15:53:22 +00:00
informOfCycle(x) {
2019-09-18 17:15:56 +00:00
const newCycleObjectArray: cycleObject[] = [];
2017-02-15 21:52:29 +00:00
this.cycleObjectArray.forEach(cycleObjectArg => {
2018-08-04 15:53:22 +00:00
cycleObjectArg.cycleCounter--;
2017-02-15 21:52:29 +00:00
if (cycleObjectArg.cycleCounter <= 0) {
2018-08-04 15:53:22 +00:00
cycleObjectArg.deferred.resolve(x);
2017-02-15 21:52:29 +00:00
} else {
2018-08-04 15:53:22 +00:00
newCycleObjectArray.push(cycleObjectArg);
2017-06-09 21:33:41 +00:00
}
2018-08-04 15:53:22 +00:00
});
this.cycleObjectArray = newCycleObjectArray;
2017-02-15 21:52:29 +00:00
}
2016-08-01 14:10:00 +00:00
}
export class BufferRunner {
2018-08-04 15:53:22 +00:00
task: Task;
2017-02-15 21:52:29 +00:00
// initialze by default
2018-08-04 15:53:22 +00:00
bufferCounter: number = 0;
running: boolean = false;
2017-02-15 21:52:29 +00:00
constructor(taskArg: Task) {
2018-08-04 15:53:22 +00:00
this.task = taskArg;
2017-06-09 21:33:41 +00:00
}
2018-08-04 15:53:22 +00:00
trigger(x): Promise<any> {
2017-07-10 10:42:06 +00:00
if (!(this.bufferCounter >= this.task.bufferMax)) {
2018-08-04 15:53:22 +00:00
this.bufferCounter++;
2017-06-09 21:33:41 +00:00
}
2019-09-18 17:15:56 +00:00
const returnPromise: Promise<any> = this.task.cycleCounter.getPromiseForCycle(
2018-08-04 15:53:22 +00:00
this.bufferCounter + 1
);
2017-06-09 21:33:41 +00:00
if (!this.running) {
2018-08-04 15:53:22 +00:00
this._run(x);
2017-06-09 21:33:41 +00:00
}
2018-08-04 15:53:22 +00:00
return returnPromise;
2017-06-09 21:33:41 +00:00
}
2018-08-04 15:53:22 +00:00
private _run(x) {
2019-09-18 17:15:56 +00:00
const recursiveBufferRunner = x => {
2017-02-15 21:52:29 +00:00
if (this.bufferCounter >= 0) {
2018-08-04 15:53:22 +00:00
this.running = true;
this.task.running = true;
runTask(this.task, { x: x }).then(x => {
this.bufferCounter--;
this.task.cycleCounter.informOfCycle(x);
recursiveBufferRunner(x);
});
2017-02-15 21:52:29 +00:00
} else {
2018-08-04 15:53:22 +00:00
this.running = false;
this.task.running = false;
2017-02-15 21:52:29 +00:00
}
2018-08-04 15:53:22 +00:00
};
recursiveBufferRunner(x);
2017-06-09 21:33:41 +00:00
}
}