now running Tasks and handling task loops allright.

This commit is contained in:
LosslessBot 2016-05-05 18:06:04 +02:00
parent fb7f5c5e68
commit 44db37574c
3 changed files with 80 additions and 29 deletions

View File

@ -1,9 +1,9 @@
/// <reference path="./typings/main.d.ts" /> /// <reference path="./typings/main.d.ts" />
import plugins = require("./taskbuffer.plugins"); import plugins = require("./taskbuffer.plugins");
import classes = require("./taskbuffer.classes"); import * as classes from "./taskbuffer.classes";
export var emptyTaskFunction = function(){ export var emptyTaskFunction = function(){
var done = plugins.Q.defer(); let done = plugins.Q.defer();
done.resolve(); done.resolve();
return done.promise; return done.promise;
}; };
@ -19,11 +19,46 @@ export var isTask = function(taskArg):boolean{
} }
}; };
export var runTask = function(taskArg){
var done = plugins.Q.defer(); let isTaskTouched = (task:classes.Task, touchedTasksArray:classes.Task[]):boolean => {
var taskReturn; return false;
if(isTask(taskArg)){ }
taskReturn = taskArg.task();
export let runTask = function(taskArg:classes.Task,optionsArg?:{touchedTasksArray:classes.Task[]}){
let done = plugins.Q.defer();
let localDeferred = plugins.Q.defer();
let touchedTasksArray:classes.Task[];
if(optionsArg.touchedTasksArray){
touchedTasksArray = optionsArg.touchedTasksArray;
} else {
touchedTasksArray = [];
} }
touchedTasksArray.push(taskArg);
localDeferred.promise
.then(() =>{
if(taskArg.preTask && !isTaskTouched(taskArg.preTask,touchedTasksArray)){
return runTask(taskArg.preTask,{touchedTasksArray:touchedTasksArray})
} else {
let done2 = plugins.Q.resolve();
done2.resolve();
return done2.promise;
}
})
.then(() => {
return taskArg.task();
})
.then(() => {
if(taskArg.afterTask && !isTaskTouched(taskArg.afterTask,touchedTasksArray)){
return runTask(taskArg.afterTask,{touchedTasksArray:touchedTasksArray})
} else {
let done2 = plugins.Q.resolve();
done2.resolve();
return done2.promise;
}
})
.then(() => {
done.resolve();
})
return done.promise; return done.promise;
}; };

View File

@ -14,32 +14,19 @@ export class Task {
preTask:Task; preTask:Task;
afterTask:Task; afterTask:Task;
constructor(taskArg,optionsArg:{preTask?:Task,afterTask?:Task, buffered?:boolean} = {}){ constructor(optionsArg:{taskFunction:any,preTask?:Task,afterTask?:Task, buffered?:boolean}){
if (!optionsArg){optionsArg = {taskFunction:function(){}}}
var options = optionsArg; var options = optionsArg;
this.task = taskArg; this.task = optionsArg.taskFunction;
this.preTask = options.preTask; this.preTask = options.preTask;
this.afterTask = options.afterTask; this.afterTask = options.afterTask;
this.idle = true; this.idle = true;
this.running = false; this.running = false;
if (typeof options.buffered === "boolean"){ this.buffered = options.buffered;
this.buffered = options.buffered;
} else {
this.buffered = false;
}
} }
trigger(){ trigger(){
let done = plugins.Q.defer(); let done = plugins.Q.defer();
helpers.runTask(this.preTask) return helpers.runTask(this)
.then(function(){
})
.then(function(){
})
.then(function(){
done.resolve();
});
return done.promise;
}; };
triggerBuffered(){ triggerBuffered(){
var done = plugins.Q.defer(); var done = plugins.Q.defer();

View File

@ -1,11 +1,40 @@
/// <reference path="./typings/main.d.ts" /> /// <reference path="./typings/main.d.ts" />
import helpers = require("./taskbuffer.classes.helpers"); import * as plugins from "./taskbuffer.plugins";
import * as classes from "./taskbuffer.classes" import * as classes from "./taskbuffer.classes"
import helpers = require("./taskbuffer.classes.helpers");
export class Taskchain extends classes.Task { export class Taskchain extends classes.Task {
constructor(taskArrayArg:classes.Task[]){ taskArray:classes.Task[];
constructor(taskArrayArg:classes.Task[]|classes.Task){
super({ super({
task: function(){} taskFunction: () => { // this is the function that gets executed when TaskChain is triggered
if(this.taskArray.length = 0) return; //make sure there is actually a Task available to execute
let startDeferred = plugins.Q.defer(); // this is the starting Deferred object
let promisePointer = startDeferred.promise;
for(let keyArg in this.taskArray){
promisePointer.then(function(){
promisePointer = this.taskArray[keyArg].trigger();
return promisePointer;
})
};
startDeferred.resolve();
}
}); });
} }
} addTask(taskArg:classes.Task){
this.taskArray.push(taskArg);
};
removeTask(taskArg:classes.Task){
//TODO
};
shiftTask(){
};
};
let myTask = new Taskchain(
new classes.Task({
taskFunction:function(){}
})
);