now has working taskchain

This commit is contained in:
Philipp Kunz 2016-05-15 03:16:50 +02:00
parent 454a999a42
commit 1714f552ac
6 changed files with 132 additions and 40 deletions

View File

@ -3,11 +3,12 @@ export declare class Taskchain extends Task {
taskArray: Task[];
private _oraObject;
constructor(optionsArg: {
taskArray: Task[];
name?: string;
log?: boolean;
taskArray: Task[];
});
addTask(taskArg: Task): void;
removeTask(taskArg: Task): void;
shiftTask(): void;
trigger(): void;
trigger(): any;
}

File diff suppressed because one or more lines are too long

View File

@ -23,7 +23,7 @@
},
"homepage": "https://github.com/pushrocks/taskbuffer#readme",
"dependencies": {
"beautylog": "^4.2.0",
"beautylog": "^5.0.0",
"lodash": "^4.12.0",
"projectinfo": "1.0.1",
"q": "^1.4.1",

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,7 @@ let plugins = {
q: require("q")
}
//setup some testData to work with
// setup some testData to work with
let testTask:taskbuffer.Task;
let testTaskFunction = function(){
let done = plugins.q.defer();
@ -45,5 +45,40 @@ describe("taskbuffer",function(){
testTask.trigger()
.then(done);
});
it("should run a task without pre and afterTask",function(done){
let localTestTask = new taskbuffer.Task({taskFunction:testTaskFunction});
localTestTask.trigger().then(done);
});
});
describe("Taskchain",function(){
let testTaskchain;
let testTaskArray = [
new taskbuffer.Task({
name:"task1",
taskFunction:function(){
let done = plugins.q.defer();
console.log("Task1 run");
done.resolve();
return done.promise;
}
}),
new taskbuffer.Task({
name:"task2",
taskFunction: function(){
let done = plugins.q.defer();
console.log("Task2 run");
done.resolve();
return done.promise;
}
}),
];
it("should run tasks in sequence",function(done){
testTaskchain = new taskbuffer.Taskchain({
name:"Taskchain1",
taskArray:testTaskArray
});
testTaskchain.trigger().then(done);
});
});
});

View File

@ -7,26 +7,40 @@ export class Taskchain extends Task {
taskArray:Task[];
private _oraObject;
constructor(optionsArg:{
name?:string,
log?:boolean,
taskArray:Task[]
name?:string
}){
let options = plugins.lodash.assign(optionsArg,{
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();
let options = plugins.lodash.assign(
{
name:"unnamed Task",
log:false
},
optionsArg,
{
taskFunction: () => { // this is the function that gets executed when TaskChain is triggered
console.log("running taskchain function");
let done = plugins.Q.defer(); // this is the starting Deferred object
let taskCounter = 0;
let iterateTasks = () => {
if(typeof this.taskArray[taskCounter] != "undefined"){
this.taskArray[taskCounter].trigger()
.then(()=>{
taskCounter++;
iterateTasks();
});
} else {
done.resolve();
}
};
iterateTasks();
return done.promise;
}
}
});
);
super(options);
this.taskArray = optionsArg.taskArray;
this._oraObject = plugins.beautylog.ora("Taskchain idle","blue");
this._oraObject = new plugins.beautylog.Ora("Taskchain idle","blue");
}
addTask(taskArg:Task){
this.taskArray.push(taskArg);
@ -38,7 +52,8 @@ export class Taskchain extends Task {
};
trigger(){
this._oraObject.start(this.name + "running");
this._oraObject.start(this.name + " running...");
return helpers.runTask(this);
}
};