"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; } ; _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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLmhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90YXNrYnVmZmVyLmNsYXNzZXMuaGVscGVycy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLGdEQUFpRDtBQUNqRCx1RUFBK0Q7QUFFcEQsUUFBQSxpQkFBaUIsR0FBa0IsVUFBVSxDQUFDO0lBQ3ZELElBQUksSUFBSSxHQUFHLE9BQU8sQ0FBQyxDQUFDLENBQUMsS0FBSyxFQUFFLENBQUE7SUFDNUIsSUFBSSxDQUFDLE9BQU8sRUFBRSxDQUFBO0lBQ2QsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7QUFDckIsQ0FBQyxDQUFBO0FBRVUsUUFBQSxNQUFNLEdBQUcsVUFBVSxPQUFhO0lBQ3pDLEVBQUUsQ0FBQyxDQUNELE9BQU8sWUFBWSw4QkFBST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