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-05 16:36:31 +00:00
|
|
|
var taskbuffer_classes_1 = require("./taskbuffer.classes");
|
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-05 16:36:31 +00:00
|
|
|
if (taskArg instanceof taskbuffer_classes_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-06 00:05:45 +00:00
|
|
|
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRhc2tidWZmZXIuY2xhc3Nlcy5oZWxwZXJzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSw0Q0FBNEM7QUFDNUMsSUFBTyxPQUFPLFdBQVcsc0JBQXNCLENBQUMsQ0FBQztBQUNqRCxtQ0FBbUIsc0JBRW5CLENBQUMsQ0FGd0M7QUFFOUIseUJBQWlCLEdBQUc7SUFDM0IsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQztJQUM3QixJQUFJLENBQUMsT0FBTyxFQUFFLENBQUM7SUFDZixNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQztBQUN4QixDQUFDLENBQUM7QUFFUyxjQUFNLEdBQUcsVUFBUyxPQUFPO0lBQ2hDLEVBQUUsQ0FBQSxDQUNFLE9BQU8sWUFBWSx5QkFBSTtXQUNwQixPQUFPLE9BQU8sQ0FBQyxJQUFJLEtBQUssVUFDL0IsQ0FBQyxDQUFBLENBQUM7UUFDRSxNQUFNLENBQUMsSUFBSSxDQUFDO0lBQ2hCLENBQUM7SUFBQyxJQUFJLENBQUMsQ0FBQztRQUNKLE1BQU0sQ0FBQyxLQUFLLENBQUM7SUFDakIsQ0FBQztBQUNMLENBQUMsQ0FBQztBQUdTLHFCQUFhLEdBQUcsVUFBQyxPQUFZLEVB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