143 lines
10 KiB
JavaScript
143 lines
10 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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;
|
|
}
|
|
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;
|
|
}
|
|
_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);
|
|
}
|
|
}
|
|
exports.BufferRunner = BufferRunner;
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLmhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90YXNrYnVmZmVyLmNsYXNzZXMuaGVscGVycy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLGdEQUFnRDtBQUNoRCx1RUFBK0Q7QUFFcEQsUUFBQSxpQkFBaUIsR0FBa0IsVUFBVSxDQUFDO0lBQ3ZELElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUE7SUFDNUIsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO0lBQ2QsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7QUFDckIsQ0FBQyxDQUFBO0FBRVUsUUFBQSxNQUFNLEdBQUcsVUFBVSxPQUFhO0lBQ3pDLEVBQUUsQ0FBQyxDQUNELE9BQU8sWUFBWSw4QkFBSTtXQUNwQixPQUFPLE9BQU8sQ0FBQyxZQUFZLEtBQUssVUFDckMsQ0FBQyxDQUFDLENBQUM7UUFDRCxNQUFNLENBQUMsSUFBSSxDQUFBO0lBQ2IsQ0FBQztJQUFDLElBQUksQ0FBQyxDQUFDO1FBQ04sTUFBTSxDQUFDLEtBQUssQ0FBQTtJQUNkLENBQUM7QUFDSCxDQUFDLENBQUE7QUFFVSxRQUFBLGFBQWEsR0FBRyxDQUFDLE9BQWEsRUFBRSxpQkFBeUI7SUFDbEUsSUFBSSxNQUFNLEdBQUcsS0FBSyxDQUFBO0lBQ2xCLEdBQUcsQ0FBQy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