2016-05-04 01:44:54 +00:00
|
|
|
import * as plugins from "./taskbuffer.plugins"
|
|
|
|
import * as helpers from "./taskbuffer.classes.helpers"
|
|
|
|
|
2016-08-03 12:51:43 +00:00
|
|
|
//interfaces
|
|
|
|
import {Promise} from "q";
|
|
|
|
export {Promise} from "q";
|
2016-08-01 11:17:15 +00:00
|
|
|
|
|
|
|
export interface ITaskFunction {
|
2016-08-02 09:09:42 +00:00
|
|
|
(x?:any):PromiseLike<any>;
|
2016-08-01 11:17:15 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:44:54 +00:00
|
|
|
export class Task {
|
2016-05-14 02:28:22 +00:00
|
|
|
name:string;
|
2016-08-02 09:09:42 +00:00
|
|
|
taskFunction:ITaskFunction;
|
2016-08-01 14:10:00 +00:00
|
|
|
buffered:boolean;
|
2016-05-04 01:44:54 +00:00
|
|
|
preTask:Task;
|
|
|
|
afterTask:Task;
|
|
|
|
|
2016-08-01 14:10:00 +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-05-14 21:24:11 +00:00
|
|
|
constructor(optionsArg:{
|
2016-08-01 11:17:15 +00:00
|
|
|
taskFunction:ITaskFunction,
|
2016-05-14 21:24:11 +00:00
|
|
|
preTask?:Task,
|
|
|
|
afterTask?:Task,
|
|
|
|
buffered?:boolean,
|
|
|
|
bufferMax?:number,
|
|
|
|
name?:string
|
|
|
|
}){
|
2016-05-04 01:44:54 +00:00
|
|
|
var options = optionsArg;
|
2016-08-02 09:09:42 +00:00
|
|
|
this.taskFunction = optionsArg.taskFunction;
|
2016-05-04 01:44:54 +00:00
|
|
|
this.preTask = options.preTask;
|
|
|
|
this.afterTask = options.afterTask;
|
2016-08-01 14:10:00 +00:00
|
|
|
this.idle = !this.running;
|
2016-05-05 16:06:04 +00:00
|
|
|
this.buffered = options.buffered;
|
2016-08-01 14:10:00 +00:00
|
|
|
this.bufferRunner.setBufferMax(options.bufferMax);
|
2016-05-14 21:24:11 +00:00
|
|
|
this.name = options.name;
|
2016-05-04 01:44:54 +00:00
|
|
|
}
|
2016-05-05 17:21:01 +00:00
|
|
|
|
2016-08-01 11:17:15 +00:00
|
|
|
/**
|
|
|
|
* trigger the task. Will trigger buffered if this.buffered is true
|
|
|
|
*/
|
2016-08-03 12:51:43 +00:00
|
|
|
trigger(x?):Promise<any> {
|
2016-05-06 00:05:45 +00:00
|
|
|
if(this.buffered) {
|
2016-08-02 09:09:42 +00:00
|
|
|
return this.triggerBuffered(x)
|
2016-05-06 00:05:45 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-08-02 09:09:42 +00:00
|
|
|
return this.triggerUnBuffered(x);
|
2016-05-06 00:05:45 +00:00
|
|
|
};
|
2016-05-04 01:44:54 +00:00
|
|
|
};
|
2016-08-01 11:17:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* trigger task unbuffered.
|
|
|
|
*/
|
2016-08-03 12:51:43 +00:00
|
|
|
triggerUnBuffered(x?):Promise<any>{
|
2016-08-02 09:09:42 +00:00
|
|
|
return helpers.runTask(this,{x:x});
|
2016-05-05 17:21:01 +00:00
|
|
|
}
|
2016-08-01 11:17:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* trigger task buffered.
|
|
|
|
*/
|
2016-08-03 12:51:43 +00:00
|
|
|
triggerBuffered(x?):Promise<any>{
|
2016-08-02 09:09:42 +00:00
|
|
|
return this.bufferRunner.trigger(x);
|
2016-05-04 01:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get state():string {
|
|
|
|
return this._state;
|
|
|
|
}
|
|
|
|
set state(stateArg:string){
|
|
|
|
if (stateArg == "locked"){
|
|
|
|
this._state = "locked";
|
|
|
|
} else {
|
2016-05-06 00:05:45 +00:00
|
|
|
plugins.beautylog.error("state type " + stateArg.blue + " could not be set");
|
2016-05-04 01:44:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|