2016-05-04 02:28:50 +00:00
|
|
|
"use strict";
|
2016-05-04 00:49:43 +00:00
|
|
|
/// <reference path="./typings/main.d.ts" />
|
|
|
|
var plugins = require("./taskbuffer.plugins");
|
2016-05-15 13:28:38 +00:00
|
|
|
var taskbuffer_classes_task_1 = require("./taskbuffer.classes.task");
|
2016-05-04 00:49:43 +00:00
|
|
|
exports.emptyTaskFunction = function () {
|
|
|
|
var done = plugins.Q.defer();
|
|
|
|
done.resolve();
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-05-04 02:28:50 +00:00
|
|
|
exports.isTask = function (taskArg) {
|
2016-05-15 13:28:38 +00:00
|
|
|
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-05-06 00:05:45 +00:00
|
|
|
exports.isTaskTouched = function (taskArg, touchedTasksArray) {
|
|
|
|
var result = false;
|
|
|
|
for (var keyArg in touchedTasksArray) {
|
|
|
|
if (taskArg === touchedTasksArray[keyArg]) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2016-05-05 16:36:31 +00:00
|
|
|
};
|
|
|
|
exports.runTask = function (taskArg, optionsArg) {
|
2016-05-06 00:05:45 +00:00
|
|
|
if (optionsArg === void 0) { optionsArg = { touchedTasksArray: [] }; }
|
2016-05-04 00:49:43 +00:00
|
|
|
var done = plugins.Q.defer();
|
2016-05-06 00:05:45 +00:00
|
|
|
exports.updateTaskStatus(taskArg, "running");
|
|
|
|
done.promise.then(function () { exports.updateTaskStatus(taskArg, "idle"); });
|
2016-05-05 16:36:31 +00:00
|
|
|
var localDeferred = plugins.Q.defer();
|
|
|
|
var touchedTasksArray;
|
|
|
|
if (optionsArg.touchedTasksArray) {
|
|
|
|
touchedTasksArray = optionsArg.touchedTasksArray;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
touchedTasksArray = [];
|
2016-05-04 00:49:43 +00:00
|
|
|
}
|
2016-05-05 16:36:31 +00:00
|
|
|
touchedTasksArray.push(taskArg);
|
|
|
|
localDeferred.promise
|
|
|
|
.then(function () {
|
|
|
|
if (taskArg.preTask && !exports.isTaskTouched(taskArg.preTask, touchedTasksArray)) {
|
|
|
|
return exports.runTask(taskArg.preTask, { touchedTasksArray: touchedTasksArray });
|
|
|
|
}
|
|
|
|
else {
|
2016-05-06 00:05:45 +00:00
|
|
|
var done2 = plugins.Q.defer();
|
2016-05-05 16:36:31 +00:00
|
|
|
done2.resolve();
|
|
|
|
return done2.promise;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return taskArg.task();
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
if (taskArg.afterTask && !exports.isTaskTouched(taskArg.afterTask, touchedTasksArray)) {
|
|
|
|
return exports.runTask(taskArg.afterTask, { touchedTasksArray: touchedTasksArray });
|
|
|
|
}
|
|
|
|
else {
|
2016-05-06 00:05:45 +00:00
|
|
|
var done2 = plugins.Q.defer();
|
2016-05-05 16:36:31 +00:00
|
|
|
done2.resolve();
|
|
|
|
return done2.promise;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
done.resolve();
|
|
|
|
});
|
2016-05-06 00:05:45 +00:00
|
|
|
localDeferred.resolve();
|
2016-05-04 00:49:43 +00:00
|
|
|
return done.promise;
|
|
|
|
};
|
2016-05-05 17:21:50 +00:00
|
|
|
exports.runBufferedTask = function (taskArg) {
|
2016-05-06 00:05:45 +00:00
|
|
|
var recursiveBufferRunner = function () {
|
|
|
|
if (taskArg.bufferCounter > 0) {
|
|
|
|
taskArg.bufferCounter--;
|
|
|
|
exports.runTask(taskArg)
|
|
|
|
.then(recursiveBufferRunner);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
exports.updateTaskStatus = function (taskArg, statusArg) {
|
|
|
|
switch (statusArg) {
|
|
|
|
case "running":
|
|
|
|
taskArg.running = true;
|
|
|
|
taskArg.idle = false;
|
|
|
|
break;
|
|
|
|
case "idle":
|
|
|
|
taskArg.running = false;
|
|
|
|
taskArg.idle = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("status not recognised");
|
|
|
|
}
|
2016-05-05 17:21:50 +00:00
|
|
|
};
|
2016-05-04 00:49:43 +00:00
|
|
|
|
2016-05-15 13:28:38 +00:00
|
|
|
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRhc2tidWZmZXIuY2xhc3Nlcy5oZWxwZXJzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSw0Q0FBNEM7QUFDNUMsSUFBTyxPQUFPLFdBQVcsc0JBQXNCLENBQUMsQ0FBQztBQUNqRCx3Q0FBbUIsMkJBQTJCLENBQUMsQ0FBQTtBQUVwQyx5QkFBaUIsR0FBRztJQUMzQixJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFDO0lBQzdCLElBQUksQ0FBQyxPQUFPLEVBQUUsQ0FBQztJQUNmLE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDO0FBQ3hCLENBQUMsQ0FBQztBQUVTLGNBQU0sR0FBRyxVQUFTLE9BQU87SUFDaEMsRUFBRSxDQUFBLENBQ0UsT0FBTyxZQUFZLDhCQUFJO1dBQ3BCLE9BQU8sT0FBTyxDQUFDLElBQUksS0FBSyxVQUMvQixDQUFDLENBQUEsQ0FBQztRQUNFLE1BQU0sQ0FBQyxJQUFJLENBQUM7SUFDaEIsQ0FBQztJQUFDLElBQUksQ0FBQyxDQUFDO1FBQ0osTUFBTSxDQUFDLEtBQUssQ0FBQztJQUNqQixDQUFDO0FBQ0wsQ0FBQyxDQUFDO0FBR1MscUJBQWEsR0FBRyxVQUFDLE9BQVks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