151 lines
10 KiB
JavaScript
151 lines
10 KiB
JavaScript
"use strict";
|
|
const plugins = require("./taskbuffer.plugins");
|
|
const taskbuffer_classes_task_1 = require("./taskbuffer.classes.task");
|
|
exports.emptyTaskFunction = function (x) {
|
|
let done = plugins.q.defer();
|
|
done.resolve();
|
|
return done.promise;
|
|
};
|
|
exports.isTask = function (taskArg) {
|
|
if (taskArg instanceof taskbuffer_classes_task_1.Task
|
|
&& typeof taskArg.taskFunction === "function") {
|
|
return true;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
};
|
|
exports.isTaskTouched = (taskArg, touchedTasksArray) => {
|
|
let result = false;
|
|
for (let keyArg in touchedTasksArray) {
|
|
if (taskArg === touchedTasksArray[keyArg]) {
|
|
result = true;
|
|
}
|
|
}
|
|
return result;
|
|
};
|
|
exports.runTask = function (taskArg, optionsArg) {
|
|
let done = plugins.q.defer();
|
|
// set running params
|
|
taskArg.running = true;
|
|
done.promise.then(function () { taskArg.running = false; });
|
|
// handle options
|
|
let options = plugins.lodash.merge({ x: undefined, touchedTasksArray: [] }, optionsArg);
|
|
let x = options.x;
|
|
let touchedTasksArray = options.touchedTasksArray;
|
|
touchedTasksArray.push(taskArg);
|
|
// run the task cascade
|
|
let localDeferred = plugins.q.defer();
|
|
localDeferred.promise
|
|
.then(() => {
|
|
if (taskArg.preTask && !exports.isTaskTouched(taskArg.preTask, touchedTasksArray)) {
|
|
return exports.runTask(taskArg.preTask, { x: x, touchedTasksArray: touchedTasksArray });
|
|
}
|
|
else {
|
|
let done2 = plugins.q.defer();
|
|
done2.resolve(x);
|
|
return done2.promise;
|
|
}
|
|
})
|
|
.then(x => {
|
|
return taskArg.taskFunction(x);
|
|
})
|
|
.then(x => {
|
|
if (taskArg.afterTask && !exports.isTaskTouched(taskArg.afterTask, touchedTasksArray)) {
|
|
return exports.runTask(taskArg.afterTask, { x: x, touchedTasksArray: touchedTasksArray });
|
|
}
|
|
else {
|
|
let done2 = plugins.q.defer();
|
|
done2.resolve(x);
|
|
return done2.promise;
|
|
}
|
|
})
|
|
.then(x => {
|
|
done.resolve(x);
|
|
}).catch((err) => {
|
|
console.log(err);
|
|
});
|
|
localDeferred.resolve();
|
|
return done.promise;
|
|
};
|
|
class CycleCounter {
|
|
constructor(taskArg) {
|
|
this.cycleObjectArray = [];
|
|
this.task = taskArg;
|
|
}
|
|
;
|
|
getPromiseForCycle(cycleCountArg) {
|
|
let done = plugins.q.defer();
|
|
let cycleObject = {
|
|
cycleCounter: cycleCountArg,
|
|
deferred: done
|
|
};
|
|
this.cycleObjectArray.push(cycleObject);
|
|
return done.promise;
|
|
}
|
|
;
|
|
informOfCycle(x) {
|
|
let newCycleObjectArray = [];
|
|
this.cycleObjectArray.forEach(cycleObjectArg => {
|
|
cycleObjectArg.cycleCounter--;
|
|
if (cycleObjectArg.cycleCounter <= 0) {
|
|
cycleObjectArg.deferred.resolve(x);
|
|
}
|
|
else {
|
|
newCycleObjectArray.push(cycleObjectArg);
|
|
}
|
|
;
|
|
});
|
|
this.cycleObjectArray = newCycleObjectArray;
|
|
}
|
|
}
|
|
exports.CycleCounter = CycleCounter;
|
|
class BufferRunner {
|
|
constructor(taskArg) {
|
|
// initialze by default
|
|
this.bufferCounter = 0;
|
|
this.bufferMax = 1;
|
|
this.running = false;
|
|
this.task = taskArg;
|
|
}
|
|
;
|
|
_run(x) {
|
|
let recursiveBufferRunner = (x) => {
|
|
if (this.bufferCounter >= 0) {
|
|
this.running = true;
|
|
this.task.running = true;
|
|
exports.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);
|
|
}
|
|
;
|
|
setBufferMax(bufferMaxArg) {
|
|
this.bufferMax = bufferMaxArg;
|
|
}
|
|
;
|
|
trigger(x) {
|
|
if (!(this.bufferCounter >= this.bufferMax)) {
|
|
this.bufferCounter++;
|
|
}
|
|
;
|
|
let returnPromise = this.task.cycleCounter.getPromiseForCycle(this.bufferCounter + 1);
|
|
if (!this.running) {
|
|
this._run(x);
|
|
}
|
|
return returnPromise;
|
|
}
|
|
;
|
|
}
|
|
exports.BufferRunner = BufferRunner;
|
|
;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLmhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90YXNrYnVmZmVyLmNsYXNzZXMuaGVscGVycy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsZ0RBQWlEO0FBQ2pELHVFQUFnRTtBQUVyRCxRQUFBLGlCQUFpQixHQUFrQixVQUFVLENBQUM7SUFDckQsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQztJQUM3QixJQUFJLENBQUMsT0FBTyxFQUFFLENBQUM7SUFDZixNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQztBQUN4QixDQUFDLENBQUM7QUFFUyxRQUFBLE1BQU0sR0FBRyxVQUFVLE9BQWE7SUFDdkMsRUFBRSxDQUFDLENBQ0MsT0FBTyxZQUFZLDhCQUFJO1dBQ3BCLE9BQU8sT0FBTyxDQUFDLFlBQVksS0FBSyxVQUN2QyxDQUFDLENBQUMsQ0FBQztRQUNDLE1BQU0sQ0FBQyxJQUFJLENBQUM7SUFDaEIsQ0FBQztJQUFDLElBQUksQ0FBQyxDQUFDO1FBQ0osTUFBTSxDQUFDLEtBQUssQ0FBQztJQUNqQixDQUFDO0FBQ0wsQ0FBQyxDQUFDO0FBR1MsUUFBQSxhQUFhLEdBQUcsQ0FBQyxPQUFhLEVBQUUsaUJBQXlCO0lBQ2hFLElBQUksTUFBTSxHQUFHLEtBQUssQ0FBQztJQ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