add some more buffer logic

This commit is contained in:
LosslessBot 2016-05-05 19:21:01 +02:00
parent 569cb311c9
commit 5fd11ec9c0
3 changed files with 25 additions and 10 deletions

View File

@ -62,3 +62,7 @@ export let runTask = function(taskArg:Task,optionsArg?:{touchedTasksArray:Task[]
return done.promise; return done.promise;
}; };
export let runBufferedTask = (taskArg:Task) => {
}

View File

@ -15,7 +15,7 @@ export class Task {
preTask:Task; preTask:Task;
afterTask:Task; afterTask:Task;
constructor(optionsArg:{taskFunction:any,preTask?:Task,afterTask?:Task, buffered?:boolean}){ constructor(optionsArg:{taskFunction:any,preTask?:Task,afterTask?:Task, buffered?:boolean, bufferMax?:number}){
if (!optionsArg){optionsArg = {taskFunction:function(){}}} if (!optionsArg){optionsArg = {taskFunction:function(){}}}
var options = optionsArg; var options = optionsArg;
this.task = optionsArg.taskFunction; this.task = optionsArg.taskFunction;
@ -24,13 +24,24 @@ export class Task {
this.idle = true; this.idle = true;
this.running = false; this.running = false;
this.buffered = options.buffered; this.buffered = options.buffered;
this.bufferMax = options.bufferMax;
} }
trigger(){ trigger(){
let done = plugins.Q.defer(); let done = plugins.Q.defer();
return helpers.runTask(this); if(this.buffered) {this.triggerBuffered()}
else{this.triggerUnBuffered()};
}; };
triggerUnBuffered(){
return helpers.runTask(this);
}
triggerBuffered(){ triggerBuffered(){
var done = plugins.Q.defer(); var done = plugins.Q.defer();
if(!(this.bufferCounter >= this.bufferMax)){
this.bufferCounter++
}
helpers.runBufferedTask(this);
return done.promise;
} }
get state():string { get state():string {

View File

@ -1,12 +1,12 @@
/// <reference path="./typings/main.d.ts" /> /// <reference path="./typings/main.d.ts" />
import * as plugins from "./taskbuffer.plugins"; import * as plugins from "./taskbuffer.plugins";
import * as classes from "./taskbuffer.classes" import {Task} from "./taskbuffer.classes";
import helpers = require("./taskbuffer.classes.helpers"); import helpers = require("./taskbuffer.classes.helpers");
export class Taskchain extends classes.Task { export class Taskchain extends Task {
taskArray:classes.Task[]; taskArray:Task[];
constructor(taskArrayArg:classes.Task[]|classes.Task){ constructor(taskArrayArg:Task[]|Task){
super({ super({
taskFunction: () => { // this is the function that gets executed when TaskChain is triggered taskFunction: () => { // this is the function that gets executed when TaskChain is triggered
if(this.taskArray.length = 0) return; //make sure there is actually a Task available to execute if(this.taskArray.length = 0) return; //make sure there is actually a Task available to execute
@ -22,10 +22,10 @@ export class Taskchain extends classes.Task {
} }
}); });
} }
addTask(taskArg:classes.Task){ addTask(taskArg:Task){
this.taskArray.push(taskArg); this.taskArray.push(taskArg);
}; };
removeTask(taskArg:classes.Task){ removeTask(taskArg:Task){
//TODO //TODO
}; };
shiftTask(){ shiftTask(){
@ -34,7 +34,7 @@ export class Taskchain extends classes.Task {
}; };
let myTask = new Taskchain( let myTask = new Taskchain(
new classes.Task({ new Task({
taskFunction:function(){} taskFunction:function(){}
}) })
); );