2016-05-04 01:44:54 +00:00
|
|
|
/// <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;
|
2016-05-04 01:44:54 +00:00
|
|
|
running:boolean;
|
2016-05-06 00:05:45 +00:00
|
|
|
idle:boolean;
|
2016-05-04 01:44:54 +00:00
|
|
|
buffered:boolean;
|
2016-05-04 03:11:08 +00:00
|
|
|
bufferCounter:number;
|
|
|
|
bufferMax:number;
|
|
|
|
private _counterTriggerAbsolute:number;
|
2016-05-04 01:44:54 +00:00
|
|
|
private _state:string;
|
|
|
|
preTask:Task;
|
|
|
|
afterTask:Task;
|
|
|
|
|
2016-05-14 21:24:11 +00:00
|
|
|
constructor(optionsArg:{
|
|
|
|
taskFunction:any,
|
|
|
|
preTask?:Task,
|
|
|
|
afterTask?:Task,
|
|
|
|
buffered?:boolean,
|
|
|
|
bufferMax?:number,
|
|
|
|
name?:string
|
|
|
|
}){
|
2016-05-05 16:06:04 +00:00
|
|
|
if (!optionsArg){optionsArg = {taskFunction:function(){}}}
|
2016-05-04 01:44:54 +00:00
|
|
|
var options = optionsArg;
|
2016-05-05 16:06:04 +00:00
|
|
|
this.task = optionsArg.taskFunction;
|
2016-05-04 01:44:54 +00:00
|
|
|
this.preTask = options.preTask;
|
|
|
|
this.afterTask = options.afterTask;
|
|
|
|
this.running = false;
|
2016-05-06 00:05:45 +00:00
|
|
|
this.idle = true;
|
2016-05-05 16:06:04 +00:00
|
|
|
this.buffered = options.buffered;
|
2016-05-05 17:21:01 +00:00
|
|
|
this.bufferMax = 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-05-04 01:44:54 +00:00
|
|
|
trigger(){
|
2016-05-04 02:17:59 +00:00
|
|
|
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-04 01:44:54 +00:00
|
|
|
};
|
2016-05-05 17:21:01 +00:00
|
|
|
triggerUnBuffered(){
|
|
|
|
return helpers.runTask(this);
|
|
|
|
}
|
2016-05-04 01:44:54 +00:00
|
|
|
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;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|