taskbuffer/dist/taskbuffer.classes.helpers.js

149 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) {
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-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;
}
;
informOfCycle() {
let newCycleObjectArray = [];
this.cycleObjectArray.forEach(cycleObjectArg => {
cycleObjectArg.cycleCounter--;
if (cycleObjectArg.cycleCounter <= 0) {
cycleObjectArg.deferred.resolve();
}
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--;
this.task.cycleCounter.informOfCycle();
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;
}
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