taskbuffer/ts/taskbuffer.classes.task.ts

107 lines
2.2 KiB
TypeScript
Raw Normal View History

2017-02-15 21:52:29 +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 {
2017-06-09 21:33:41 +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
2017-06-09 21:33:41 +00:00
name: string
2017-02-15 21:52:29 +00:00
taskFunction: ITaskFunction
buffered: boolean
2017-07-10 10:42:06 +00:00
bufferMax: number
execDelay: number
// tasks to run before and after
2017-02-15 21:52:29 +00:00
preTask: Task
afterTask: Task
2017-02-15 21:52:29 +00:00
// initialize by default
running: boolean = false
bufferRunner = new helpers.BufferRunner(this)
cycleCounter = new helpers.CycleCounter(this)
2017-07-10 10:42:06 +00:00
2017-02-15 21:52:29 +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
*/
taskFunction: ITaskFunction
/**
* any other task to run before
*/
preTask?: Task
/**
* any other task to run after
*/
afterTask?: Task
/**
* wether this task should run buffered
*/
buffered?: boolean
/**
* the maximum buffer
*/
bufferMax?: number
/**
* the execution delay, before the task is executed
* only makes sense when running in buffered mode
*/
execDelay?: number
/**
* the name of the task
*/
2017-02-15 21:52:29 +00:00
name?: string
}) {
this.taskFunction = optionsArg.taskFunction
2017-07-10 10:42:06 +00:00
this.preTask = optionsArg.preTask
this.afterTask = optionsArg.afterTask
2017-02-15 21:52:29 +00:00
this.idle = !this.running
2017-07-10 10:42:06 +00:00
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) {
return this.triggerBuffered(x)
2017-06-09 21:33:41 +00:00
} else {
2017-02-15 21:52:29 +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> {
2017-02-15 21:52:29 +00:00
return helpers.runTask(this, { x: x })
}
2017-02-15 21:52:29 +00:00
/**
* trigger task buffered.
*/
2017-07-10 10:42:06 +00:00
triggerBuffered(x?): Promise<any> {
2017-02-15 21:52:29 +00:00
return this.bufferRunner.trigger(x)
}
2017-07-10 10:42:06 +00:00
get state(): string {
2017-02-15 21:52:29 +00:00
return this._state
}
2017-07-10 10:42:06 +00:00
set state(stateArg: string) {
2017-02-15 21:52:29 +00:00
if (stateArg === 'locked') {
this._state = 'locked'
} else {
plugins.beautylog.error('state type ' + stateArg + ' could not be set')
}
2017-02-15 21:52:29 +00:00
}
2017-06-09 21:33:41 +00:00
}