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-01-19 16:26:35 +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-01-19 16:26:35 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLmhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90YXNrYnVmZmVyLmNsYXNzZXMuaGVscGVycy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsZ0RBQWlEO0FBQ2pELHVFQUFnRTtBQUVyRCxRQUFBLGlCQUFpQixHQUFrQixVQUFVLENBQUM7SUFDckQsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQztJQUM3QixJQUFJLENBQUMsT0FBTyxFQUFFLENBQUM7SUFDZixNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQztBQUN4QixDQUFDLENBQUM7QUFFUyxRQUFBLE1BQU0sR0FBRyxVQUFVLE9BQWE7SUFDdkMsRUFBRSxDQUFDLENBQ0MsT0FBTyxZQUFZLDhCQUFJO1dBQ3BCLE9BQU8sT0FBTyxDQUFDLFlBQVksS0FBSyxVQUN2QyxDQUFDLENBQUMsQ0FBQztRQUNDLE1BQU0sQ0FBQyxJQUFJLENBQUM7SUFDaEIsQ0FBQztJQUFDLElBQUksQ0FBQyxDQUFDO1FBQ0osTUFBTSxDQUFDLEtBQUssQ0FBQztJQUNqQixDQUFDO0FBQ0wsQ0FBQyxDQUFDO0FBR1MsUUFBQSxhQUFhLEdBQUcsQ0FBQ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