taskbuffer/dist/taskbuffer.classes.helpers.js
2017-07-10 12:42:06 +02:00

151 lines
11 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const plugins = require("./taskbuffer.plugins");
const taskbuffer_classes_task_1 = require("./taskbuffer.classes.task");
exports.emptyTaskFunction = function (x) {
let done = plugins.q.defer();
done.resolve();
return done.promise;
};
exports.isTask = function (taskArg) {
if (taskArg instanceof taskbuffer_classes_task_1.Task
&& typeof taskArg.taskFunction === 'function') {
return true;
}
else {
return false;
}
};
exports.isTaskTouched = (taskArg, touchedTasksArray) => {
let result = false;
for (let keyArg in touchedTasksArray) {
if (taskArg === touchedTasksArray[keyArg]) {
result = true;
}
}
return result;
};
exports.runTask = (taskArg, optionsArg) => __awaiter(this, void 0, void 0, function* () {
let done = plugins.q.defer();
// pay respect to execDelay
if (taskArg.execDelay) {
yield plugins.smartdelay.delayFor(taskArg.execDelay);
}
// set running params
taskArg.running = true;
done.promise.then(() => __awaiter(this, void 0, void 0, 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
.then(() => {
if (taskArg.preTask && !exports.isTaskTouched(taskArg.preTask, touchedTasksArray)) {
return exports.runTask(taskArg.preTask, { x: x, touchedTasksArray: touchedTasksArray });
}
else {
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 {
let done2 = plugins.q.defer();
done2.resolve(x);
return done2.promise;
}
})
.then(x => {
done.resolve(x);
}).catch((err) => {
console.log(err);
});
localDeferred.resolve();
return yield done.promise;
});
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(x) {
let newCycleObjectArray = [];
this.cycleObjectArray.forEach(cycleObjectArg => {
cycleObjectArg.cycleCounter--;
if (cycleObjectArg.cycleCounter <= 0) {
cycleObjectArg.deferred.resolve(x);
}
else {
newCycleObjectArray.push(cycleObjectArg);
}
});
this.cycleObjectArray = newCycleObjectArray;
}
}
exports.CycleCounter = CycleCounter;
class BufferRunner {
constructor(taskArg) {
// initialze by default
this.bufferCounter = 0;
this.running = false;
this.task = taskArg;
}
trigger(x) {
if (!(this.bufferCounter >= this.task.bufferMax)) {
this.bufferCounter++;
}
let returnPromise = this.task.cycleCounter.getPromiseForCycle(this.bufferCounter + 1);
if (!this.running) {
this._run(x);
}
return returnPromise;
}
_run(x) {
let recursiveBufferRunner = (x) => {
if (this.bufferCounter >= 0) {
this.running = true;
this.task.running = true;
exports.runTask(this.task, { x: x })
.then((x) => {
this.bufferCounter--;
this.task.cycleCounter.informOfCycle(x);
recursiveBufferRunner(x);
});
}
else {
this.running = false;
this.task.running = false;
}
};
recursiveBufferRunner(x);
}
}
exports.BufferRunner = BufferRunner;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLmhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyI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