taskbuffer/ts/taskbuffer.classes.taskchain.ts

60 lines
2.2 KiB
TypeScript
Raw Normal View History

2016-05-04 01:45:52 +00:00
/// <reference path="./typings/main.d.ts" />
import * as plugins from "./taskbuffer.plugins";
import {Task} from "./taskbuffer.classes.task";
import helpers = require("./taskbuffer.classes.helpers");
2016-05-05 17:21:01 +00:00
export class Taskchain extends Task {
taskArray:Task[];
2016-05-15 13:52:59 +00:00
private _oraObject:plugins.beautylog.Ora;
constructor(optionsArg:{
2016-05-15 01:16:50 +00:00
name?:string,
log?:boolean,
taskArray:Task[]
}){
2016-05-15 01:16:50 +00:00
let options = plugins.lodash.assign(
{
2016-05-15 13:52:59 +00:00
name:"unnamed Taskchain",
2016-05-15 01:16:50 +00:00
log:false
},
optionsArg,
{
taskFunction: () => { // this is the function that gets executed when TaskChain is triggered
let done = plugins.Q.defer(); // this is the starting Deferred object
let taskCounter = 0;
let iterateTasks = () => {
if(typeof this.taskArray[taskCounter] != "undefined"){
2016-05-15 13:52:59 +00:00
this._oraObject.text(this.name + " running: Task" + this.taskArray[taskCounter].name);
2016-05-15 01:16:50 +00:00
this.taskArray[taskCounter].trigger()
.then(()=>{
2016-05-15 13:52:59 +00:00
this._oraObject.log(this.taskArray[taskCounter].name,"ok");
2016-05-15 01:16:50 +00:00
taskCounter++;
iterateTasks();
});
} else {
2016-05-15 13:52:59 +00:00
this._oraObject.endOk("Taskchain \"" + this.name + "\" completed successfully");
2016-05-15 01:16:50 +00:00
done.resolve();
}
};
iterateTasks();
return done.promise;
}
}
2016-05-15 01:16:50 +00:00
);
super(options);
this.taskArray = optionsArg.taskArray;
2016-05-15 01:16:50 +00:00
this._oraObject = new plugins.beautylog.Ora("Taskchain idle","blue");
}
2016-05-05 17:21:01 +00:00
addTask(taskArg:Task){
this.taskArray.push(taskArg);
};
2016-05-05 17:21:01 +00:00
removeTask(taskArg:Task){
//TODO
};
shiftTask(){
};
trigger(){
2016-05-15 01:16:50 +00:00
this._oraObject.start(this.name + " running...");
return helpers.runTask(this);
}
};