taskbuffer/ts/taskbuffer.classes.taskchain.ts

64 lines
1.8 KiB
TypeScript
Raw Normal View History

2017-06-17 14:56:33 +00:00
// TaskChain chains tasks
// and extends Task
2017-06-09 21:33:41 +00:00
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 {
2017-02-15 21:52:29 +00:00
taskArray: Task[]
private _oraObject
constructor(optionsArg: {
taskArray: Task[],
name?: string,
log?: boolean,
buffered?: boolean,
bufferMax?: number
}) {
let options = plugins.lodash.merge(
{
name: 'unnamed Taskchain',
log: false
},
optionsArg,
{
taskFunction: (x: any) => { // 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 // counter for iterating async over the taskArray
let iterateTasks = (x) => {
if (typeof this.taskArray[ taskCounter ] !== 'undefined') {
this._oraObject.text(this.name + ' running: Task' + this.taskArray[ taskCounter ].name)
this.taskArray[ taskCounter ].trigger(x)
.then((x) => {
plugins.beautylog.ok(this.taskArray[ taskCounter ].name)
taskCounter++
iterateTasks(x)
})
} else {
this._oraObject.endOk('Taskchain "' + this.name + '" completed successfully')
done.resolve(x)
}
2017-02-15 21:52:29 +00:00
}
iterateTasks(x)
return done.promise
}
}
)
super(options)
this.taskArray = optionsArg.taskArray
this._oraObject = plugins.beautylog.ora
if (optionsArg.log === true) {
this._oraObject.start()
2017-06-09 21:33:41 +00:00
}
2017-02-15 21:52:29 +00:00
}
2017-06-09 21:33:41 +00:00
addTask (taskArg: Task) {
2017-02-15 21:52:29 +00:00
this.taskArray.push(taskArg)
2017-06-09 21:33:41 +00:00
}
removeTask (taskArg: Task) {
// TODO:
}
shiftTask () {
// TODO:
}
2017-02-15 21:52:29 +00:00
}