97 lines
11 KiB
JavaScript
97 lines
11 KiB
JavaScript
"use strict";
|
|
/// <reference path="./typings/main.d.ts" />
|
|
var plugins = require("./taskbuffer.plugins");
|
|
var taskbuffer_classes_task_1 = require("./taskbuffer.classes.task");
|
|
exports.emptyTaskFunction = function () {
|
|
var done = plugins.Q.defer();
|
|
done.resolve();
|
|
return done.promise;
|
|
};
|
|
exports.isTask = function (taskArg) {
|
|
if (taskArg instanceof taskbuffer_classes_task_1.Task
|
|
&& typeof taskArg.task === "function") {
|
|
return true;
|
|
}
|
|
else {
|
|
return false;
|
|
}
|
|
};
|
|
exports.isTaskTouched = function (taskArg, touchedTasksArray) {
|
|
var result = false;
|
|
for (var keyArg in touchedTasksArray) {
|
|
if (taskArg === touchedTasksArray[keyArg]) {
|
|
result = true;
|
|
}
|
|
}
|
|
return result;
|
|
};
|
|
exports.runTask = function (taskArg, optionsArg) {
|
|
if (optionsArg === void 0) { optionsArg = { touchedTasksArray: [] }; }
|
|
var done = plugins.Q.defer();
|
|
exports.updateTaskStatus(taskArg, "running");
|
|
done.promise.then(function () { exports.updateTaskStatus(taskArg, "idle"); });
|
|
var localDeferred = plugins.Q.defer();
|
|
var touchedTasksArray;
|
|
if (optionsArg.touchedTasksArray) {
|
|
touchedTasksArray = optionsArg.touchedTasksArray;
|
|
}
|
|
else {
|
|
touchedTasksArray = [];
|
|
}
|
|
touchedTasksArray.push(taskArg);
|
|
localDeferred.promise
|
|
.then(function () {
|
|
if (taskArg.preTask && !exports.isTaskTouched(taskArg.preTask, touchedTasksArray)) {
|
|
return exports.runTask(taskArg.preTask, { touchedTasksArray: touchedTasksArray });
|
|
}
|
|
else {
|
|
var done2 = plugins.Q.defer();
|
|
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 {
|
|
var done2 = plugins.Q.defer();
|
|
done2.resolve();
|
|
return done2.promise;
|
|
}
|
|
})
|
|
.then(function () {
|
|
done.resolve();
|
|
});
|
|
localDeferred.resolve();
|
|
return done.promise;
|
|
};
|
|
exports.runBufferedTask = function (taskArg) {
|
|
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");
|
|
}
|
|
};
|
|
|
|
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRhc2tidWZmZXIuY2xhc3Nlcy5oZWxwZXJzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSw0Q0FBNEM7QUFDNUMsSUFBTyxPQUFPLFdBQVcsc0JBQXNCLENBQUMsQ0FBQztBQUNqRCx3Q0FBbUIsMkJBQTJCLENBQUMsQ0FBQTtBQUVwQyx5QkFBaUIsR0FBRztJQUMzQixJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsQ0FBQyxDQUFDLEtBQUssRUFBRSxDQUFDO0lBQzdCLElBQUksQ0FBQyxPQUFPLEVBQUUsQ0FBQztJQUNmLE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDO0FBQ3hCLENBQUMsQ0FBQztBQUVTLGNBQU0sR0FBRyxVQUFTLE9BQU87SUFDaEMsRUFBRSxDQUFBLENBQ0UsT0FBTyxZQUFZLDhCQUFJO1dBQ3BCLE9BQU8sT0FBTyxDQUFDLElBQUksS0F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JheSA9IFtdO1xyXG4gICAgfVxyXG4gICAgdG91Y2hlZFRhc2tzQXJyYXkucHVzaCh0YXNrQXJnKTtcclxuICAgIGxvY2FsRGVmZXJyZWQucHJvbWlzZVxyXG4gICAgICAgIC50aGVuKCgpID0+e1xyXG4gICAgICAgICAgICBpZih0YXNrQXJnLnByZVRhc2sgJiYgIWlzVGFza1RvdWNoZWQodGFza0FyZy5wcmVUYXNrLHRvdWNoZWRUYXNrc0FycmF5KSl7XHJcbiAgICAgICAgICAgICAgICByZXR1cm4gcnVuVGFzayh0YXNrQXJnLnByZVRhc2sse3RvdWNoZWRUYXNrc0FycmF5OnRvdWNoZWRUYXNrc0FycmF5fSlcclxuICAgICAgICAgICAgfSBlbHNlIHtcclxuICAgICAgICAgICAgICAgIGxldCBkb25lMiA9IHBsdWdpbnMuUS5kZWZlcigpO1xyXG4gICAgICAgICAgICAgICAgZG9uZTIucmVzb2x2ZSgpO1xyXG4gICAgICAgICAgICAgICAgcmV0dXJuIGRvbmUyLnByb21pc2U7XHJcbiAgICAgICAgICAgIH1cclxuICAgICAgICB9KVxyXG4gICAgICAgIC50aGVuKCgpID0+IHtcclxuICAgICAgICAgICAgcmV0dXJuIHRhc2tBcmcudGFzaygpO1xyXG4gICAgICAgIH0pXHJcbiAgICAgICAgLnRoZW4oKCkgPT4ge1xyXG4gICAgICAgICAgICBpZih0YXNrQXJnLmFmdGVyVGFzayAmJiAhaXNUYXNrVG91Y2hlZCh0YXNrQXJnLmFmdGVyVGFzayx0b3VjaGVkVGFza3NBcnJheSkpe1xyXG4gICAgICAgICAgICAgICAgcmV0dXJuIHJ1blRhc2sodGFza0FyZy5hZnRlclRhc2sse3RvdWNoZWRUYXNrc0FycmF5OnRvdWNoZWRUYXNrc0FycmF5fSlcclxuICAgICAgICAgICAgfSBlbHNlIHtcclxuICAgICAgICAgICAgICAgIGxldCBkb25lMiA9IHBsdWdpbnMuUS5kZWZlcigpO1xyXG4gICAgICAgICAgICAgICAgZG9uZTIucmVzb2x2ZSgpO1xyXG4gICAgICAgICAgICAgICAgcmV0dXJuIGRvbmUyLnByb21pc2U7XHJcbiAgICAgICAgICAgIH1cclxuICAgICAgICB9KVxyXG4gICAgICAgIC50aGVuKCgpID0+IHtcclxuICAgICAgICAgICAgZG9uZS5yZXNvbHZlKCk7XHJcbiAgICAgICAgfSk7XHJcbiAgICBsb2NhbERlZmVycmVkLnJlc29sdmUoKTtcclxuICAgIHJldHVybiBkb25lLnByb21pc2U7XHJcbn07XHJcblxyXG5leHBvcnQgbGV0IHJ1bkJ1ZmZlcmVkVGFzayA9ICh0YXNrQXJnOlRhc2spID0+IHtcclxuICAgIGxldCByZWN1cnNpdmVCdWZmZXJSdW5uZXIgPSAoKSA9PiB7XHJcbiAgICAgICAgaWYodGFza0FyZy5idWZmZXJDb3VudGVyID4gMCl7XHJcbiAgICAgICAgICAgIHRhc2tBcmcuYnVmZmVyQ291bnRlci0tO1xyXG4gICAgICAgICAgICBydW5UYXNrKHRhc2tBcmcpXHJcbiAgICAgICAgICAgICAgICAudGhlbihyZWN1cnNpdmVCdWZmZXJSdW5uZXIpO1xyXG4gICAgICAgIH1cclxuICAgIH1cclxufVxyXG5cclxuZXhwb3J0IGxldCB1cGRhdGVUYXNrU3RhdHVzID0gKHRhc2tBcmcsc3RhdHVzQXJnOnN0cmluZykgPT4ge1xyXG4gICAgc3dpdGNoIChzdGF0dXNBcmcpIHtcclxuICAgICAgICBjYXNlIFwicnVubmluZ1wiOlxyXG4gICAgICAgICAgICB0YXNrQXJnLnJ1bm5pbmcgPSB0cnVlO1xyXG4gICAgICAgICAgICB0YXNrQXJnLmlkbGUgPSBmYWxzZTtcclxuICAgICAgICAgICAgYnJlYWs7XHJcbiAgICAgICAgY2FzZSBcImlkbGVcIjpcclxuICAgICAgICAgICAgdGFza0FyZy5ydW5uaW5nID0gZmFsc2U7XHJcbiAgICAgICAgICAgIHRhc2tBcmcuaWRsZSA9IHRydWU7XHJcbiAgICAgICAgICAgIGJyZWFrO1xyXG4gICAgICAgIGRlZmF1bHQ6XHJcbiAgICAgICAgICAgIHRocm93IG5ldyBFcnJvcihcInN0YXR1cyBub3QgcmVjb2duaXNlZFwiKTtcclxuICAgIH1cclxufSJdfQ==
|