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
2022-03-25 11:14:49 +00:00
import * as plugins from './taskbuffer.plugins.js';
import { Task } from './taskbuffer.classes.task.js';
import { logger } from './taskbuffer.logging.js';
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
}) {
2020-07-12 00:48:51 +00:00
const options = {
2018-08-04 15:53:22 +00:00
...{
2017-02-15 21:52:29 +00:00
name: 'unnamed Taskchain',
2020-07-12 00:48:51 +00:00
log: false,
2017-02-15 21:52:29 +00:00
},
2018-08-04 15:53:22 +00:00
...optionsArg,
...{
taskFunction: (x: any) => {
// this is the function that gets executed when TaskChain is triggered
2020-07-12 00:48:51 +00:00
const done = plugins.smartpromise.defer(); // this is the starting Deferred object
2018-08-04 15:53:22 +00:00
let taskCounter = 0; // counter for iterating async over the taskArray
2021-09-26 12:45:02 +00:00
const iterateTasks = (x: any) => {
2018-08-04 15:53:22 +00:00
if (typeof this.taskArray[taskCounter] !== 'undefined') {
console.log(this.name + ' running: Task' + this.taskArray[taskCounter].name);
2020-07-12 00:48:51 +00:00
this.taskArray[taskCounter].trigger(x).then((x) => {
logger.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;
2020-07-12 00:48:51 +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
}