taskbuffer/ts/taskbuffer.classes.task.ts

80 lines
1.9 KiB
TypeScript
Raw Normal View History

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 {
(x?:any):PromiseLike<any>;
2016-08-01 11:17:15 +00:00
}
export class Task {
2016-05-14 02:28:22 +00:00
name:string;
taskFunction:ITaskFunction;
2016-08-01 14:10:00 +00:00
buffered:boolean;
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";
constructor(optionsArg:{
2016-08-01 11:17:15 +00:00
taskFunction:ITaskFunction,
preTask?:Task,
afterTask?:Task,
buffered?:boolean,
bufferMax?:number,
name?:string
}){
var options = optionsArg;
this.taskFunction = optionsArg.taskFunction;
this.preTask = options.preTask;
this.afterTask = options.afterTask;
2016-08-01 14:10:00 +00:00
this.idle = !this.running;
this.buffered = options.buffered;
2016-08-01 14:10:00 +00:00
this.bufferRunner.setBufferMax(options.bufferMax);
this.name = options.name;
}
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) {
return this.triggerBuffered(x)
2016-05-06 00:05:45 +00:00
}
else {
return this.triggerUnBuffered(x);
2016-05-06 00:05:45 +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>{
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>{
return this.bufferRunner.trigger(x);
}
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");
}
}
}