taskbuffer/ts/taskbuffer.classes.task.ts

76 lines
1.7 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-06-09 21:33:41 +00:00
name: string
2017-02-15 21:52:29 +00:00
taskFunction: ITaskFunction
buffered: boolean
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)
idle: boolean = true
private _state: string = 'ready'
2016-08-01 14:10:00 +00:00
2017-02-15 21:52:29 +00:00
constructor(optionsArg: {
taskFunction: ITaskFunction,
preTask?: Task,
afterTask?: Task,
buffered?: boolean,
bufferMax?: number,
name?: string
}) {
let options = optionsArg
this.taskFunction = optionsArg.taskFunction
this.preTask = options.preTask
this.afterTask = options.afterTask
this.idle = !this.running
this.buffered = options.buffered
this.bufferRunner.setBufferMax(options.bufferMax)
this.name = options.name
}
/**
* trigger the task. Will trigger buffered if this.buffered is true
*/
2017-06-09 21:33:41 +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-06-09 21:33:41 +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-06-09 21:33:41 +00:00
triggerBuffered (x?): Promise<any> {
2017-02-15 21:52:29 +00:00
return this.bufferRunner.trigger(x)
}
2017-06-09 21:33:41 +00:00
get state (): string {
2017-02-15 21:52:29 +00:00
return this._state
}
2017-06-09 21:33:41 +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
}