taskbuffer/dist/taskbuffer.classes.helpers.js

151 lines
11 KiB
JavaScript
Raw Normal View History

2016-05-04 02:28:50 +00:00
"use strict";
2017-07-10 10:42:06 +00:00
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());
});
};
2017-06-09 21:26:23 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
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;
};
2017-07-10 10:42:06 +00:00
exports.runTask = (taskArg, optionsArg) => __awaiter(this, void 0, void 0, function* () {
2017-01-19 16:26:35 +00:00
let done = plugins.q.defer();
2017-07-10 10:42:06 +00:00
// pay respect to execDelay
if (taskArg.execDelay) {
yield plugins.smartdelay.delayFor(taskArg.execDelay);
}
// set running params
2016-08-01 11:17:15 +00:00
taskArg.running = true;
2017-07-10 10:42:06 +00:00
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
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();
2017-07-10 10:42:06 +00:00
return yield 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.running = false;
this.task = taskArg;
}
2017-06-09 21:33:41 +00:00
trigger(x) {
2017-07-10 10:42:06 +00:00
if (!(this.bufferCounter >= this.task.bufferMax)) {
2017-06-09 21:33:41 +00:00
this.bufferCounter++;
}
let returnPromise = this.task.cycleCounter.getPromiseForCycle(this.bufferCounter + 1);
if (!this.running) {
this._run(x);
}
return returnPromise;
}
_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
}
}
exports.BufferRunner = BufferRunner;
2017-07-10 10:42:06 +00:00
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLmhlbHBlcnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90YXNrYnVmZmVyLmNsYXNzZXMuaGVscGVycy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsZ0RBQWdEO0FBQ2hELHVFQUErRDtBQUVwRCxRQUFBLGlCQUFpQixHQUFrQixVQUFVLENBQUM7SUFDdkQsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQTtJQUM1QixJQUFJLENBQUMsT0FBTyxFQUFFLENBQUE7SUFDZCxNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQTtBQUN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