taskbuffer/dist/taskbuffer.classes.helpers.js

153 lines
11 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");
2016-08-03 12:51:43 +00:00
var q_1 = require("q");
exports.Promise = q_1.Promise;
exports.emptyTaskFunction = function (x) {
2016-07-31 22:53:27 +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
2016-05-04 02:28:50 +00:00
&& typeof taskArg.task === "function") {
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) {
2016-07-31 22:53:27 +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
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 {
2016-07-31 22:53:27 +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 {
2016-07-31 22:53:27 +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) {
let done = plugins.Q.defer();
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;
;
2016-08-03 12:51:43 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLmhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90YXNrYnVmZmVyLmNsYXNzZXMuaGVscGVycy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsTUFBTyxPQUFPLFdBQVcsc0JBQXNCLENBQUMsQ0FBQztBQUNqRCwwQ0FBb0MsMkJBQTJCLENBQUMsQ0FBQTtBQUdoRSxrQkFBc0IsR0FBRyxDQUFDO0FBQWxCLDhCQUFrQjtBQUVmLHlCQUFpQixHQUFrQixVQUFVLENBQUM7SUFDckQsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQztJQUM3QixJQUFJLENBQUMsT0FBTyxFQUFFLENBQUM7SUFDZixNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQztBQUN4QixDQUFDLENBQUM7QUFFUyxjQUFNLEdBQUcsVUFBVSxPQUFPO0lBQ2pDLEVBQUUsQ0FBQyxDQUNDLE9BQU8sWUFBWSw4QkFBSTtXQUNwQixPQUFPLE9BQU8sQ0FBQyxJQUFJLEtBQUssVUFDL0IsQ0FBQyxDQUFDLENBQUM7UUFDQyxNQUFNLENBQUMsSUFBSSxDQUFDO0lBQ2hCLENBQUM7SUFBQyxJQUFJLENBQUMsQ0FBQztRQUNKLE1BQU0sQ0FBQyxLQUFLLENBQUM7SUF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