taskbuffer/ts/taskbuffer.classes.task.ts

72 lines
1.9 KiB
TypeScript
Raw Normal View History

/// <reference path="./typings/main.d.ts" />
import * as plugins from "./taskbuffer.plugins"
import * as helpers from "./taskbuffer.classes.helpers"
export class Task {
2016-05-14 02:28:22 +00:00
name:string;
2016-05-04 02:28:50 +00:00
task:any;
running:boolean;
2016-05-06 00:05:45 +00:00
idle:boolean;
buffered:boolean;
bufferCounter:number;
bufferMax:number;
private _counterTriggerAbsolute:number;
private _state:string;
preTask:Task;
afterTask:Task;
constructor(optionsArg:{
taskFunction:any,
preTask?:Task,
afterTask?:Task,
buffered?:boolean,
bufferMax?:number,
name?:string
}){
if (!optionsArg){optionsArg = {taskFunction:function(){}}}
var options = optionsArg;
this.task = optionsArg.taskFunction;
this.preTask = options.preTask;
this.afterTask = options.afterTask;
this.running = false;
2016-05-06 00:05:45 +00:00
this.idle = true;
this.buffered = options.buffered;
2016-05-05 17:21:01 +00:00
this.bufferMax = options.bufferMax;
this.name = options.name;
}
2016-05-05 17:21:01 +00:00
trigger(){
let done = plugins.Q.defer();
2016-05-06 00:05:45 +00:00
if(this.buffered) {
this.triggerBuffered()
.then(done.resolve);
}
else {
this.triggerUnBuffered()
.then(done.resolve);
};
return done.promise;
};
2016-05-05 17:21:01 +00:00
triggerUnBuffered(){
return helpers.runTask(this);
}
triggerBuffered(){
var done = plugins.Q.defer();
2016-05-05 17:21:01 +00:00
if(!(this.bufferCounter >= this.bufferMax)){
this.bufferCounter++
}
helpers.runBufferedTask(this);
return done.promise;
}
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");
}
}
}