taskbuffer/ts/taskbuffer.classes.taskchain.ts

59 lines
1.7 KiB
TypeScript
Raw Normal View History

2017-06-17 14:56:33 +00:00
// TaskChain chains tasks
// and extends Task
2018-08-04 15:53:22 +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 {
2018-08-04 15:53:22 +00:00
taskArray: Task[];
2017-02-15 21:52:29 +00:00
constructor(optionsArg: {
2018-08-04 15:53:22 +00:00
taskArray: Task[];
name?: string;
log?: boolean;
buffered?: boolean;
bufferMax?: number;
2017-02-15 21:52:29 +00:00
}) {
2018-08-04 15:53:22 +00:00
let options = {
...{
2017-02-15 21:52:29 +00:00
name: 'unnamed Taskchain',
log: false
},
2018-08-04 15:53:22 +00:00
...optionsArg,
...{
taskFunction: (x: any) => {
// this is the function that gets executed when TaskChain is triggered
let done = plugins.smartpromise.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') {
console.log(this.name + ' running: Task' + this.taskArray[taskCounter].name);
this.taskArray[taskCounter].trigger(x).then(x => {
2019-01-14 23:17:44 +00:00
plugins.smartlog.defaultLogger.log('info', this.taskArray[taskCounter].name);
2018-08-04 15:53:22 +00:00
taskCounter++;
iterateTasks(x);
});
2017-02-15 21:52:29 +00:00
} else {
2018-08-04 15:53:22 +00:00
console.log('Taskchain "' + this.name + '" completed successfully');
done.resolve(x);
}
2018-08-04 15:53:22 +00:00
};
iterateTasks(x);
return done.promise;
2017-02-15 21:52:29 +00:00
}
}
2018-08-04 15:53:22 +00:00
};
super(options);
this.taskArray = optionsArg.taskArray;
2017-02-15 21:52:29 +00:00
}
2018-08-04 15:53:22 +00:00
addTask(taskArg: Task) {
this.taskArray.push(taskArg);
2017-06-09 21:33:41 +00:00
}
2018-08-04 15:53:22 +00:00
removeTask(taskArg: Task) {
2017-06-09 21:33:41 +00:00
// TODO:
}
2018-08-04 15:53:22 +00:00
shiftTask() {
2017-06-09 21:33:41 +00:00
// TODO:
}
2017-02-15 21:52:29 +00:00
}