taskbuffer/dist/taskbuffer.classes.helpers.js

153 lines
11 KiB
JavaScript

"use strict";
const plugins = require("./taskbuffer.plugins");
const taskbuffer_classes_task_1 = require("./taskbuffer.classes.task");
var q_1 = require("q");
exports.Promise = q_1.Promise;
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.task === "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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLmhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90YXNrYnVmZmVyLmNsYXNzZXMuaGVscGVycy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsTUFBTyxPQUFPLFdBQVcsc0JBQXNCLENBQUMsQ0FBQztBQUNqRCwwQ0FBb0MsMkJBQTJCLENBQUMsQ0FBQTtBQUdoRSxrQkFBc0IsR0FBRyxDQUFDO0FBQWxCLDhCQUFrQjtBQUVmLHlCQUFpQixHQUFrQixVQUFVLENBQUM7SUFDckQsSUFBSSxJQU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