2016-05-04 02:28:50 +00:00
|
|
|
"use strict";
|
2017-06-09 21:26:23 +00:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2016-07-31 22:53:27 +00:00
|
|
|
const plugins = require("./taskbuffer.plugins");
|
|
|
|
const taskbuffer_classes_task_1 = require("./taskbuffer.classes.task");
|
2016-08-02 09:09:42 +00:00
|
|
|
exports.emptyTaskFunction = function (x) {
|
2017-01-19 16:26:35 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-05-04 00:49:43 +00:00
|
|
|
done.resolve();
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-05-04 02:28:50 +00:00
|
|
|
exports.isTask = function (taskArg) {
|
2016-05-15 13:28:38 +00:00
|
|
|
if (taskArg instanceof taskbuffer_classes_task_1.Task
|
2017-02-15 21:52:29 +00:00
|
|
|
&& typeof taskArg.taskFunction === 'function') {
|
2016-05-04 02:28:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2016-07-31 22:53:27 +00:00
|
|
|
exports.isTaskTouched = (taskArg, touchedTasksArray) => {
|
|
|
|
let result = false;
|
|
|
|
for (let keyArg in touchedTasksArray) {
|
2016-05-06 00:05:45 +00:00
|
|
|
if (taskArg === touchedTasksArray[keyArg]) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2016-05-05 16:36:31 +00:00
|
|
|
};
|
2016-08-02 09:09:42 +00:00
|
|
|
exports.runTask = function (taskArg, optionsArg) {
|
2017-01-19 16:26:35 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-08-02 09:09:42 +00:00
|
|
|
// set running params
|
2016-08-01 11:17:15 +00:00
|
|
|
taskArg.running = true;
|
|
|
|
done.promise.then(function () { taskArg.running = false; });
|
2016-08-02 09:09:42 +00:00
|
|
|
// handle options
|
|
|
|
let options = plugins.lodash.merge({ x: undefined, touchedTasksArray: [] }, optionsArg);
|
|
|
|
let x = options.x;
|
|
|
|
let touchedTasksArray = options.touchedTasksArray;
|
2016-05-05 16:36:31 +00:00
|
|
|
touchedTasksArray.push(taskArg);
|
2016-08-02 09:09:42 +00:00
|
|
|
// run the task cascade
|
2017-01-19 16:26:35 +00:00
|
|
|
let localDeferred = plugins.q.defer();
|
2016-05-05 16:36:31 +00:00
|
|
|
localDeferred.promise
|
2016-07-31 22:53:27 +00:00
|
|
|
.then(() => {
|
2016-05-05 16:36:31 +00:00
|
|
|
if (taskArg.preTask && !exports.isTaskTouched(taskArg.preTask, touchedTasksArray)) {
|
2016-08-02 09:09:42 +00:00
|
|
|
return exports.runTask(taskArg.preTask, { x: x, touchedTasksArray: touchedTasksArray });
|
2016-05-05 16:36:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-01-19 16:26:35 +00:00
|
|
|
let done2 = plugins.q.defer();
|
2016-08-02 09:09:42 +00:00
|
|
|
done2.resolve(x);
|
2016-05-05 16:36:31 +00:00
|
|
|
return done2.promise;
|
|
|
|
}
|
|
|
|
})
|
2016-08-02 09:09:42 +00:00
|
|
|
.then(x => {
|
|
|
|
return taskArg.taskFunction(x);
|
2016-05-05 16:36:31 +00:00
|
|
|
})
|
2016-08-02 09:09:42 +00:00
|
|
|
.then(x => {
|
2016-05-05 16:36:31 +00:00
|
|
|
if (taskArg.afterTask && !exports.isTaskTouched(taskArg.afterTask, touchedTasksArray)) {
|
2016-08-02 09:09:42 +00:00
|
|
|
return exports.runTask(taskArg.afterTask, { x: x, touchedTasksArray: touchedTasksArray });
|
2016-05-05 16:36:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-01-19 16:26:35 +00:00
|
|
|
let done2 = plugins.q.defer();
|
2016-08-02 09:09:42 +00:00
|
|
|
done2.resolve(x);
|
2016-05-05 16:36:31 +00:00
|
|
|
return done2.promise;
|
|
|
|
}
|
|
|
|
})
|
2016-08-02 09:09:42 +00:00
|
|
|
.then(x => {
|
|
|
|
done.resolve(x);
|
2016-08-03 09:28:13 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
console.log(err);
|
2016-05-05 16:36:31 +00:00
|
|
|
});
|
2016-05-06 00:05:45 +00:00
|
|
|
localDeferred.resolve();
|
2016-05-04 00:49:43 +00:00
|
|
|
return done.promise;
|
|
|
|
};
|
2016-08-01 14:10:00 +00:00
|
|
|
class CycleCounter {
|
|
|
|
constructor(taskArg) {
|
|
|
|
this.cycleObjectArray = [];
|
|
|
|
this.task = taskArg;
|
|
|
|
}
|
|
|
|
getPromiseForCycle(cycleCountArg) {
|
2017-01-19 16:26:35 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-08-01 14:10:00 +00:00
|
|
|
let cycleObject = {
|
|
|
|
cycleCounter: cycleCountArg,
|
|
|
|
deferred: done
|
|
|
|
};
|
|
|
|
this.cycleObjectArray.push(cycleObject);
|
|
|
|
return done.promise;
|
|
|
|
}
|
2016-08-03 12:51:43 +00:00
|
|
|
informOfCycle(x) {
|
2016-08-01 14:10:00 +00:00
|
|
|
let newCycleObjectArray = [];
|
|
|
|
this.cycleObjectArray.forEach(cycleObjectArg => {
|
|
|
|
cycleObjectArg.cycleCounter--;
|
|
|
|
if (cycleObjectArg.cycleCounter <= 0) {
|
2016-08-03 12:51:43 +00:00
|
|
|
cycleObjectArg.deferred.resolve(x);
|
2016-08-01 14:10:00 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2017-06-09 21:33:41 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-08-02 09:09:42 +00:00
|
|
|
_run(x) {
|
|
|
|
let recursiveBufferRunner = (x) => {
|
2016-08-01 14:10:00 +00:00
|
|
|
if (this.bufferCounter >= 0) {
|
|
|
|
this.running = true;
|
|
|
|
this.task.running = true;
|
2016-08-02 09:09:42 +00:00
|
|
|
exports.runTask(this.task, { x: x })
|
|
|
|
.then((x) => {
|
2016-08-01 14:10:00 +00:00
|
|
|
this.bufferCounter--;
|
2016-08-03 12:51:43 +00:00
|
|
|
this.task.cycleCounter.informOfCycle(x);
|
2016-08-02 09:09:42 +00:00
|
|
|
recursiveBufferRunner(x);
|
2016-08-01 14:10:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.running = false;
|
|
|
|
this.task.running = false;
|
|
|
|
}
|
|
|
|
};
|
2016-08-02 09:09:42 +00:00
|
|
|
recursiveBufferRunner(x);
|
2016-08-01 14:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.BufferRunner = BufferRunner;
|
2017-06-09 21:33:41 +00:00
|
|
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLmhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90YXNrYnVmZmVyLmNsYXNzZXMuaGVscGVycy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLGdEQUFnRDtBQUNoRCx1RUFBK0Q7QUFFcEQsUUFBQSxpQkFBaUIsR0F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