taskbuffer/ts/taskbuffer.classes.task.ts

107 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-08-04 15:53:22 +00:00
import * as plugins from './taskbuffer.plugins';
import * as helpers from './taskbuffer.classes.helpers';
2016-08-01 11:17:15 +00:00
export interface ITaskFunction {
2018-08-04 15:53:22 +00:00
(x?: any): PromiseLike<any>;
2016-08-01 11:17:15 +00:00
}
export class Task {
2017-07-10 10:42:06 +00:00
// man datory properties
2018-08-04 15:53:22 +00:00
name: string;
taskFunction: ITaskFunction;
buffered: boolean;
2017-07-10 10:42:06 +00:00
2018-08-04 15:53:22 +00:00
bufferMax: number;
execDelay: number;
2017-07-10 10:42:06 +00:00
// tasks to run before and after
2018-08-04 15:53:22 +00:00
preTask: Task;
afterTask: Task;
2017-02-15 21:52:29 +00:00
// initialize by default
2018-08-04 15:53:22 +00:00
running: boolean = false;
bufferRunner = new helpers.BufferRunner(this);
cycleCounter = new helpers.CycleCounter(this);
2017-07-10 10:42:06 +00:00
2018-08-04 15:53:22 +00:00
idle: boolean = true;
private _state: string = 'ready';
2016-08-01 14:10:00 +00:00
2017-07-10 10:42:06 +00:00
constructor(optionsArg: {
/**
* the task function to run, must return promise
*/
2018-08-04 15:53:22 +00:00
taskFunction: ITaskFunction;
2017-07-10 10:42:06 +00:00
/**
* any other task to run before
*/
2018-08-04 15:53:22 +00:00
preTask?: Task;
2017-07-10 10:42:06 +00:00
/**
* any other task to run after
*/
2018-08-04 15:53:22 +00:00
afterTask?: Task;
2017-07-10 10:42:06 +00:00
/**
* wether this task should run buffered
*/
2018-08-04 15:53:22 +00:00
buffered?: boolean;
2017-07-10 10:42:06 +00:00
/**
* the maximum buffer
*/
2018-08-04 15:53:22 +00:00
bufferMax?: number;
2017-07-10 10:42:06 +00:00
/**
* the execution delay, before the task is executed
* only makes sense when running in buffered mode
*/
2018-08-04 15:53:22 +00:00
execDelay?: number;
2017-07-10 10:42:06 +00:00
/**
* the name of the task
*/
2018-08-04 15:53:22 +00:00
name?: string;
2017-02-15 21:52:29 +00:00
}) {
2018-08-04 15:53:22 +00:00
this.taskFunction = optionsArg.taskFunction;
this.preTask = optionsArg.preTask;
this.afterTask = optionsArg.afterTask;
this.idle = !this.running;
this.buffered = optionsArg.buffered;
this.bufferMax = optionsArg.bufferMax;
this.execDelay = optionsArg.execDelay;
this.name = optionsArg.name;
2017-02-15 21:52:29 +00:00
}
/**
* trigger the task. Will trigger buffered if this.buffered is true
*/
2017-07-10 10:42:06 +00:00
trigger(x?): Promise<any> {
2017-02-15 21:52:29 +00:00
if (this.buffered) {
2018-08-04 15:53:22 +00:00
return this.triggerBuffered(x);
2017-06-09 21:33:41 +00:00
} else {
2018-08-04 15:53:22 +00:00
return this.triggerUnBuffered(x);
2017-06-09 21:33:41 +00:00
}
}
2016-08-01 11:17:15 +00:00
2017-02-15 21:52:29 +00:00
/**
* trigger task unbuffered.
*/
2017-07-10 10:42:06 +00:00
triggerUnBuffered(x?): Promise<any> {
2018-08-04 15:53:22 +00:00
return helpers.runTask(this, { x: x });
2017-02-15 21:52:29 +00:00
}
2017-02-15 21:52:29 +00:00
/**
* trigger task buffered.
*/
2017-07-10 10:42:06 +00:00
triggerBuffered(x?): Promise<any> {
2018-08-04 15:53:22 +00:00
return this.bufferRunner.trigger(x);
2017-02-15 21:52:29 +00:00
}
2017-07-10 10:42:06 +00:00
get state(): string {
2018-08-04 15:53:22 +00:00
return this._state;
2017-02-15 21:52:29 +00:00
}
2017-07-10 10:42:06 +00:00
set state(stateArg: string) {
2017-02-15 21:52:29 +00:00
if (stateArg === 'locked') {
2018-08-04 15:53:22 +00:00
this._state = 'locked';
2017-02-15 21:52:29 +00:00
} else {
2019-01-14 23:17:44 +00:00
plugins.smartlog.defaultLogger.log('error', 'state type ' + stateArg + ' could not be set');
}
2017-02-15 21:52:29 +00:00
}
2017-06-09 21:33:41 +00:00
}