taskbuffer/dist/taskbuffer.classes.helpers.js

151 lines
10 KiB
JavaScript
Raw Normal View History

2016-05-04 02:28:50 +00:00
"use strict";
2016-07-31 22:53:27 +00:00
const plugins = require("./taskbuffer.plugins");
const taskbuffer_classes_task_1 = require("./taskbuffer.classes.task");
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) {
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;
};
exports.runTask = function (taskArg, optionsArg) {
2017-01-19 16:26:35 +00:00
let done = plugins.q.defer();
// set running params
2016-08-01 11:17:15 +00:00
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
2017-01-19 16:26:35 +00:00
let localDeferred = plugins.q.defer();
localDeferred.promise
2016-07-31 22:53:27 +00:00
.then(() => {
if (taskArg.preTask && !exports.isTaskTouched(taskArg.preTask, touchedTasksArray)) {
return exports.runTask(taskArg.preTask, { x: x, touchedTasksArray: touchedTasksArray });
}
else {
2017-01-19 16:26:35 +00:00
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 {
2017-01-19 16:26:35 +00:00
let done2 = plugins.q.defer();
done2.resolve(x);
return done2.promise;
}
})
.then(x => {
done.resolve(x);
2016-08-03 09:28:13 +00:00
}).catch((err) => {
console.log(err);
});
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;
}
;
_run(x) {
let recursiveBufferRunner = (x) => {
2016-08-01 14:10:00 +00:00
if (this.bufferCounter >= 0) {
this.running = true;
this.task.running = true;
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);
recursiveBufferRunner(x);
2016-08-01 14:10:00 +00:00
});
}
else {
this.running = false;
this.task.running = false;
}
};
recursiveBufferRunner(x);
2016-08-01 14:10:00 +00:00
}
;
setBufferMax(bufferMaxArg) {
this.bufferMax = bufferMaxArg;
}
;
trigger(x) {
2016-08-01 14:10:00 +00:00
if (!(this.bufferCounter >= this.bufferMax)) {
this.bufferCounter++;
2016-05-06 00:05:45 +00:00
}
2016-08-01 14:10:00 +00:00
;
let returnPromise = this.task.cycleCounter.getPromiseForCycle(this.bufferCounter + 1);
if (!this.running) {
this._run(x);
2016-08-01 11:17:15 +00:00
}
2016-08-01 14:10:00 +00:00
return returnPromise;
}
;
}
exports.BufferRunner = BufferRunner;
;
2017-02-15 21:52:29 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLmhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90YXNrYnVmZmVyLmNsYXNzZXMuaGVscGVycy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsZ0RBQWlEO0FBQ2pELHVFQUErRDtBQUVwRCxRQUFBLGlCQUFpQixHQUFrQixVQUFVLENBQUM7SUFDdkQsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtJQUM1QixJQUFJLENBQUMsT0FBTyxFQUFFLENBQUE7SUFDZCxNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQTtBQUNyQixDQUFDLENBQUE7QUFFVSxRQUFBLE1BQU0sR0FBRyxVQUFVLE9BQWE7SUFDekMsRUFBRSxDQUFDLENBQ0QsT0FBTyxZQUFZLDhCQUFJO1dBQ3BCLE9BQU8sT0FBTyxDQUFDLFlBQVksS0FBSyxVQUNyQyxDQUFDLENBQUMsQ0FBQztRQUNELE1BQU0sQ0FBQyxJQUFJLENBQUE7SUFDYixDQUFDO0lBQUMsSUFBSSxDQUFDLENBQUM7UUFDTixNQUFNLENBQUMsS0FBSyxDQUFBO0lBQ2QsQ0FBQztBQUNILENBQUMsQ0FBQTtBQUdVLFFBQUEsYUFBYSxHQUFHLENBQUMsT0FB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