now running correctly in buffered mode
This commit is contained in:
parent
7a60f30f0b
commit
d30fb551f9
22
dist/taskbuffer.classes.helpers.d.ts
vendored
22
dist/taskbuffer.classes.helpers.d.ts
vendored
@ -7,4 +7,24 @@ export declare let isTaskTouched: (taskArg: Task, touchedTasksArray: Task[]) =>
|
||||
export declare let runTask: (taskArg: Task, optionsArg?: {
|
||||
touchedTasksArray: Task[];
|
||||
}) => plugins.Q.Promise<{}>;
|
||||
export declare let runBufferedTask: (taskArg: Task) => void;
|
||||
export interface cycleObject {
|
||||
cycleCounter: number;
|
||||
deferred: plugins.Q.Deferred<any>;
|
||||
}
|
||||
export declare class CycleCounter {
|
||||
task: Task;
|
||||
cycleObjectArray: cycleObject[];
|
||||
constructor(taskArg: Task);
|
||||
getPromiseForCycle(cycleCountArg: number): plugins.Q.Promise<{}>;
|
||||
informOfCycle(): void;
|
||||
}
|
||||
export declare class BufferRunner {
|
||||
task: Task;
|
||||
bufferCounter: number;
|
||||
bufferMax: number;
|
||||
running: boolean;
|
||||
constructor(taskArg: Task);
|
||||
private _run();
|
||||
setBufferMax(bufferMaxArg: number): void;
|
||||
trigger(): PromiseLike<any>;
|
||||
}
|
||||
|
93
dist/taskbuffer.classes.helpers.js
vendored
93
dist/taskbuffer.classes.helpers.js
vendored
File diff suppressed because one or more lines are too long
17
dist/taskbuffer.classes.task.d.ts
vendored
17
dist/taskbuffer.classes.task.d.ts
vendored
@ -1,21 +1,20 @@
|
||||
/// <reference types="q" />
|
||||
import * as plugins from "./taskbuffer.plugins";
|
||||
import * as helpers from "./taskbuffer.classes.helpers";
|
||||
export interface ITaskFunction {
|
||||
(): PromiseLike<any>;
|
||||
}
|
||||
export declare class Task {
|
||||
name: string;
|
||||
task: any;
|
||||
running: boolean;
|
||||
runningBuffered: boolean;
|
||||
idle: boolean;
|
||||
buffered: boolean;
|
||||
bufferCounter: number;
|
||||
bufferMax: number;
|
||||
private _counterTriggerAbsolute;
|
||||
private _state;
|
||||
preTask: Task;
|
||||
afterTask: Task;
|
||||
running: boolean;
|
||||
bufferRunner: helpers.BufferRunner;
|
||||
cycleCounter: helpers.CycleCounter;
|
||||
idle: boolean;
|
||||
private _state;
|
||||
constructor(optionsArg: {
|
||||
taskFunction: ITaskFunction;
|
||||
preTask?: Task;
|
||||
@ -31,10 +30,10 @@ export declare class Task {
|
||||
/**
|
||||
* trigger task unbuffered.
|
||||
*/
|
||||
triggerUnBuffered(): plugins.Q.Promise<{}>;
|
||||
triggerUnBuffered(): PromiseLike<any>;
|
||||
/**
|
||||
* trigger task buffered.
|
||||
*/
|
||||
triggerBuffered(): plugins.Q.Promise<{}>;
|
||||
triggerBuffered(): PromiseLike<any>;
|
||||
state: string;
|
||||
}
|
||||
|
26
dist/taskbuffer.classes.task.js
vendored
26
dist/taskbuffer.classes.task.js
vendored
@ -3,20 +3,19 @@ const plugins = require("./taskbuffer.plugins");
|
||||
const helpers = require("./taskbuffer.classes.helpers");
|
||||
class Task {
|
||||
constructor(optionsArg) {
|
||||
// initialize by default
|
||||
this.running = false;
|
||||
this.runningBuffered = false;
|
||||
this.bufferRunner = new helpers.BufferRunner(this);
|
||||
this.cycleCounter = new helpers.CycleCounter(this);
|
||||
this.idle = true;
|
||||
this.buffered = false;
|
||||
this.bufferMax = 1;
|
||||
this._counterTriggerAbsolute = 0;
|
||||
this._state = "ready";
|
||||
var options = optionsArg;
|
||||
this.task = optionsArg.taskFunction;
|
||||
this.preTask = options.preTask;
|
||||
this.afterTask = options.afterTask;
|
||||
this.running = false;
|
||||
this.idle = !this.running && !this.runningBuffered;
|
||||
this.idle = !this.running;
|
||||
this.buffered = options.buffered;
|
||||
this.bufferMax = options.bufferMax;
|
||||
this.bufferRunner.setBufferMax(options.bufferMax);
|
||||
this.name = options.name;
|
||||
}
|
||||
/**
|
||||
@ -46,16 +45,7 @@ class Task {
|
||||
* trigger task buffered.
|
||||
*/
|
||||
triggerBuffered() {
|
||||
var done = plugins.Q.defer();
|
||||
if (!(this.bufferCounter >= this.bufferMax)) {
|
||||
this.bufferCounter++;
|
||||
}
|
||||
;
|
||||
if (!this.runningBuffered) {
|
||||
helpers.runBufferedTask(this);
|
||||
}
|
||||
;
|
||||
return done.promise;
|
||||
return this.bufferRunner.trigger();
|
||||
}
|
||||
get state() {
|
||||
return this._state;
|
||||
@ -70,4 +60,4 @@ class Task {
|
||||
}
|
||||
}
|
||||
exports.Task = Task;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLnRhc2suanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90YXNrYnVmZmVyLmNsYXNzZXMudGFzay50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsTUFBWSxPQUFPLFdBQU0sc0JBQ3pCLENBQUMsQ0FEOEM7QUFDL0MsTUFBWSxPQUFPLFdBQU0sOEJBR3pCLENBQUMsQ0FIc0Q7QUFPdkQ7SUFjSSxZQUFZLFVBT1g7UUFsQkQsWUFBTyxHQUFXLEtBQUssQ0FBQztRQUN4QixvQkFBZSxHQUFXLEtBQUssQ0FBQztRQUNoQyxTQUFJLEdBQVcsSUFBSSxDQUFDO1FBQ3BCLGFBQVEsR0FBVyxLQUFLLENBQUM7UUFFekIsY0FBUyxHQUFVLENBQUMsQ0FBQztRQUNiLDRCQUF1QixHQUFVLENBQUMsQ0FBQztRQWF2QyxJQUFJLE9BQU8sR0FBRyxVQUFVLENBQUM7UUFDekIsSUFBSSxDQUFDLElBQUksR0FBRyxVQUFVLENBQUMsWUFBWSxDQUFDO1FBQ3BDLElBQUksQ0FBQyxPQUFPLEdBQUcsT0FBTyxDQUFDLE9BQU8sQ0FBQztRQUMvQixJQUFJLENBQUMsU0FBUyxHQUFHLE9BQU8sQ0FBQyxTQUFTLENBQUM7UUFDbkMsSUFBSSxDQUFDLE9BQU8sR0FBRyxLQUFLLENBQUM7UUFDckIsSUFBSSxDQUFDLElBQUksR0FB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FBSSxDQUFDLE9BQU8sQ0FBQyxDQUFDO1FBQzVCLENBQUM7UUFDRCxJQUFJLENBQUMsQ0FBQztZQUNGLElBQUksQ0FBQyxpQkFBaUIsRUFBRTtpQkFDbkIsSUFBSSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsQ0FBQztRQUM1QixDQUFDO1FBQUEsQ0FBQztRQUNGLE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDO0lBQ3hCLENBQUM7O0lBRUQ7O09BRUc7SUFDSCxpQkFBaUI7UUFDYixNQUFNLENBQUMsT0FBTyxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FBQztJQUNqQyxDQUFDO0lBRUQ7O09BRUc7SUFDSCxlQUFlO1FBQ1gsTUFBTSxDQUFDLElBQUksQ0FBQyxZQUFZLENBQUMsT0FBTyxFQUFFLENBQUM7SUFDdkMsQ0FBQztJQUVELElBQUksS0FBSztRQUNMLE1BQU0sQ0FBQyxJQUFJLENBQUMsTUFBTSxDQUFDO0lBQ3ZCLENBQUM7SUFDRCxJQUFJLEtBQUssQ0FBQyxRQUFlO1FBQ3JCLEVBQUUsQ0FBQyxDQUFDLFFBQVEsSUFBSSxRQUFRLENBQUMsQ0FBQSxDQUFDO1lBQ3RCLElBQUksQ0FBQyxNQUFNLEdBQUcsUUFBUSxDQUFDO1FBQzNCLENBQUM7UUFBQyxJQUFJLENBQUMsQ0FBQztZQUNKLE9BQU8sQ0FBQyxTQUFTLENBQUMsS0FBSyxDQUFDLGFBQWEsR0FBRyxRQUFRLENBQUMsSUFBSSxHQUFHLG1CQUFtQixDQUFDLENBQUM7UUFDakYsQ0FBQztJQUNMLENBQUM7QUFDTCxDQUFDO0FBeEVZLFlBQUksT0F3RWhCLENBQUEifQ==
|
99
test/test.js
99
test/test.js
File diff suppressed because one or more lines are too long
164
test/test.ts
164
test/test.ts
@ -4,123 +4,153 @@ let should = require("should");
|
||||
import q = require("q")
|
||||
|
||||
// setup some testData to work with
|
||||
let testTask:taskbuffer.Task;
|
||||
let testTaskFunction = function(){
|
||||
let testTask: taskbuffer.Task;
|
||||
let testTaskFunction = function () {
|
||||
let done = q.defer();
|
||||
console.log("main function executed!")
|
||||
done.resolve();
|
||||
return done.promise;
|
||||
}
|
||||
};
|
||||
|
||||
let testTaskFunctionTimeout = function () {
|
||||
let done = q.defer();
|
||||
console.log("main function started!");
|
||||
setTimeout(() => {
|
||||
console.log("main function ended!");
|
||||
done.resolve();
|
||||
},2000);
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
let testPreTask = new taskbuffer.Task({
|
||||
taskFunction:function(){
|
||||
taskFunction: function () {
|
||||
let done = q.defer();
|
||||
console.log("preTask executed");
|
||||
done.resolve();
|
||||
return done.promise;
|
||||
},
|
||||
preTask:testTask
|
||||
preTask: testTask
|
||||
});
|
||||
|
||||
describe("taskbuffer",function(){
|
||||
describe(".Task",function(){
|
||||
it("new Task() should return a new task",function(){
|
||||
testTask = new taskbuffer.Task({taskFunction:testTaskFunction,preTask:testPreTask});
|
||||
|
||||
// some more tasks to wirj with
|
||||
let task1 = new taskbuffer.Task({
|
||||
name: "Task 1",
|
||||
taskFunction: () => {
|
||||
let done = q.defer();
|
||||
console.log("Task1 started");
|
||||
setTimeout(() => {
|
||||
console.log("Task1 executed");
|
||||
done.resolve();
|
||||
}, 5000)
|
||||
return done.promise;
|
||||
}
|
||||
});
|
||||
let task2 = new taskbuffer.Task({
|
||||
name: "Task 1",
|
||||
taskFunction: () => {
|
||||
let done = q.defer();
|
||||
console.log("Task2 started");
|
||||
setTimeout(() => {
|
||||
console.log("Task2 executed");
|
||||
done.resolve();
|
||||
}, 5000)
|
||||
return done.promise;
|
||||
}
|
||||
});
|
||||
let task3 = new taskbuffer.Task({
|
||||
name: "Task 3",
|
||||
taskFunction: () => {
|
||||
let done = q.defer();
|
||||
console.log("Task3 started");
|
||||
setTimeout(() => {
|
||||
console.log("Task3 executed");
|
||||
done.resolve();
|
||||
}, 5000)
|
||||
return done.promise;
|
||||
}
|
||||
});
|
||||
|
||||
// the actual tests
|
||||
describe("taskbuffer", function () {
|
||||
describe(".Task", function () {
|
||||
it("new Task() should return a new task", function () {
|
||||
testTask = new taskbuffer.Task({ taskFunction: testTaskFunction, preTask: testPreTask });
|
||||
});
|
||||
it("testTask should be and instance of Task",function(){
|
||||
it("testTask should be and instance of Task", function () {
|
||||
testTask.should.be.instanceof(taskbuffer.Task);
|
||||
});
|
||||
it("testTask.idle is true",function(){
|
||||
if (!testTask.idle){
|
||||
it("testTask.idle is true", function () {
|
||||
if (!testTask.idle) {
|
||||
throw new Error("testTask.idle is not true");
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
it("testTask.running is type boolean and initially false",function(){
|
||||
it("testTask.running is type boolean and initially false", function () {
|
||||
testTask.running.should.be.type("boolean");
|
||||
testTask.running.should.be.false();
|
||||
});
|
||||
it("testTask.trigger() should return Promise",function(){
|
||||
it("testTask.trigger() should return Promise", function () {
|
||||
testTask.trigger().should.be.Promise();
|
||||
});
|
||||
it("testTask.trigger() returned Promise should be fullfilled",function(done){
|
||||
it("testTask.trigger() returned Promise should be fullfilled", function (done) {
|
||||
testTask.trigger()
|
||||
.then(done);
|
||||
});
|
||||
it("should run a task without pre and afterTask",function(done){
|
||||
let localTestTask = new taskbuffer.Task({taskFunction:testTaskFunction});
|
||||
it("should run a task without pre and afterTask", function (done) {
|
||||
let localTestTask = new taskbuffer.Task({ taskFunction: testTaskFunction });
|
||||
localTestTask.trigger().then(done);
|
||||
});
|
||||
|
||||
it("should run buffered", function (done) {
|
||||
this.timeout(10000);
|
||||
let localTestTask = new taskbuffer.Task({
|
||||
taskFunction: testTaskFunctionTimeout,
|
||||
buffered:true,
|
||||
bufferMax:2
|
||||
});
|
||||
localTestTask.trigger();
|
||||
localTestTask.trigger();
|
||||
localTestTask.trigger();
|
||||
localTestTask.trigger().then(() => {
|
||||
done();
|
||||
})
|
||||
});
|
||||
});
|
||||
describe("Taskchain",function(){
|
||||
describe("Taskchain", function () {
|
||||
let testTaskchain;
|
||||
let testTaskArray = [
|
||||
new taskbuffer.Task({
|
||||
name:"task1",
|
||||
taskFunction:function(){
|
||||
name: "task1",
|
||||
taskFunction: function () {
|
||||
let done = q.defer();
|
||||
setTimeout(done.resolve,2000);
|
||||
setTimeout(done.resolve, 2000);
|
||||
return done.promise;
|
||||
}
|
||||
}),
|
||||
new taskbuffer.Task({
|
||||
name:"task2",
|
||||
taskFunction: function(){
|
||||
name: "task2",
|
||||
taskFunction: function () {
|
||||
let done = q.defer();
|
||||
setTimeout(done.resolve,2000);
|
||||
setTimeout(done.resolve, 2000);
|
||||
return done.promise;
|
||||
}
|
||||
}),
|
||||
];
|
||||
it("should run tasks in sequence",function(done){
|
||||
it("should run tasks in sequence", function (done) {
|
||||
this.timeout(5000);
|
||||
testTaskchain = new taskbuffer.Taskchain({
|
||||
name:"Taskchain1",
|
||||
taskArray:testTaskArray
|
||||
name: "Taskchain1",
|
||||
taskArray: testTaskArray
|
||||
});
|
||||
testTaskchain.trigger().then(done);
|
||||
});
|
||||
});
|
||||
describe("taskparallel",function(){
|
||||
let task1 = new taskbuffer.Task({
|
||||
name:"Task 1",
|
||||
taskFunction: () => {
|
||||
let done = q.defer();
|
||||
console.log("Task1 started");
|
||||
setTimeout(() => {
|
||||
console.log("Task1 executed");
|
||||
done.resolve();
|
||||
},5000)
|
||||
return done.promise;
|
||||
}
|
||||
});
|
||||
let task2 = new taskbuffer.Task({
|
||||
name:"Task 1",
|
||||
taskFunction: () => {
|
||||
let done = q.defer();
|
||||
console.log("Task2 started");
|
||||
setTimeout(() => {
|
||||
console.log("Task2 executed");
|
||||
done.resolve();
|
||||
},5000)
|
||||
return done.promise;
|
||||
}
|
||||
});
|
||||
let task3 = new taskbuffer.Task({
|
||||
name:"Task 3",
|
||||
taskFunction: () => {
|
||||
let done = q.defer();
|
||||
console.log("Task3 started");
|
||||
setTimeout(() => {
|
||||
console.log("Task3 executed");
|
||||
done.resolve();
|
||||
},5000)
|
||||
return done.promise;
|
||||
}
|
||||
});
|
||||
it("should run in Parallel",function(done){
|
||||
describe("taskparallel", function () {
|
||||
it("should run in Parallel", function (done) {
|
||||
this.timeout("7000");
|
||||
let testTaskparallel = new taskbuffer.Taskparallel({
|
||||
taskArray:[task1,task2,task3]
|
||||
taskArray: [task1, task2, task3]
|
||||
});
|
||||
testTaskparallel.trigger().then(() => {
|
||||
done();
|
||||
|
@ -1,17 +1,17 @@
|
||||
import plugins = require("./taskbuffer.plugins");
|
||||
import {Task,ITaskFunction} from "./taskbuffer.classes.task";
|
||||
import { Task, ITaskFunction } from "./taskbuffer.classes.task";
|
||||
|
||||
export let emptyTaskFunction:ITaskFunction = function(){
|
||||
export let emptyTaskFunction: ITaskFunction = function () {
|
||||
let done = plugins.Q.defer();
|
||||
done.resolve();
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
export let isTask = function(taskArg):boolean{
|
||||
if(
|
||||
export let isTask = function (taskArg): boolean {
|
||||
if (
|
||||
taskArg instanceof Task
|
||||
&& typeof taskArg.task === "function"
|
||||
){
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
@ -19,32 +19,32 @@ export let isTask = function(taskArg):boolean{
|
||||
};
|
||||
|
||||
|
||||
export let isTaskTouched = (taskArg:Task, touchedTasksArray:Task[]):boolean => {
|
||||
export let isTaskTouched = (taskArg: Task, touchedTasksArray: Task[]): boolean => {
|
||||
let result = false;
|
||||
for (let keyArg in touchedTasksArray){
|
||||
if(taskArg === touchedTasksArray[keyArg]){
|
||||
for (let keyArg in touchedTasksArray) {
|
||||
if (taskArg === touchedTasksArray[keyArg]) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export let runTask = function(taskArg:Task,optionsArg:{touchedTasksArray:Task[]} = {touchedTasksArray:[]}){
|
||||
export let runTask = function (taskArg: Task, optionsArg: { touchedTasksArray: Task[] } = { touchedTasksArray: [] }) {
|
||||
let done = plugins.Q.defer();
|
||||
taskArg.running = true;
|
||||
done.promise.then(function(){taskArg.running = false});
|
||||
done.promise.then(function () { taskArg.running = false });
|
||||
let localDeferred = plugins.Q.defer();
|
||||
let touchedTasksArray:Task[];
|
||||
if(optionsArg.touchedTasksArray){
|
||||
let touchedTasksArray: Task[];
|
||||
if (optionsArg.touchedTasksArray) {
|
||||
touchedTasksArray = optionsArg.touchedTasksArray;
|
||||
} else {
|
||||
touchedTasksArray = [];
|
||||
}
|
||||
touchedTasksArray.push(taskArg);
|
||||
localDeferred.promise
|
||||
.then(() =>{
|
||||
if(taskArg.preTask && !isTaskTouched(taskArg.preTask,touchedTasksArray)){
|
||||
return runTask(taskArg.preTask,{touchedTasksArray:touchedTasksArray})
|
||||
.then(() => {
|
||||
if (taskArg.preTask && !isTaskTouched(taskArg.preTask, touchedTasksArray)) {
|
||||
return runTask(taskArg.preTask, { touchedTasksArray: touchedTasksArray })
|
||||
} else {
|
||||
let done2 = plugins.Q.defer();
|
||||
done2.resolve();
|
||||
@ -55,8 +55,8 @@ export let runTask = function(taskArg:Task,optionsArg:{touchedTasksArray:Task[]}
|
||||
return taskArg.task();
|
||||
})
|
||||
.then(() => {
|
||||
if(taskArg.afterTask && !isTaskTouched(taskArg.afterTask,touchedTasksArray)){
|
||||
return runTask(taskArg.afterTask,{touchedTasksArray:touchedTasksArray})
|
||||
if (taskArg.afterTask && !isTaskTouched(taskArg.afterTask, touchedTasksArray)) {
|
||||
return runTask(taskArg.afterTask, { touchedTasksArray: touchedTasksArray })
|
||||
} else {
|
||||
let done2 = plugins.Q.defer();
|
||||
done2.resolve();
|
||||
@ -70,15 +70,79 @@ export let runTask = function(taskArg:Task,optionsArg:{touchedTasksArray:Task[]}
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
export let runBufferedTask = (taskArg:Task) => {
|
||||
let recursiveBufferRunner = () => {
|
||||
if(taskArg.bufferCounter > 0){
|
||||
taskArg.runningBuffered = true;
|
||||
taskArg.bufferCounter--;
|
||||
runTask(taskArg)
|
||||
.then(recursiveBufferRunner);
|
||||
} else {
|
||||
taskArg.runningBuffered = false;
|
||||
}
|
||||
|
||||
export interface cycleObject {
|
||||
cycleCounter:number,
|
||||
deferred:plugins.Q.Deferred<any>
|
||||
}
|
||||
|
||||
export class CycleCounter {
|
||||
task:Task;
|
||||
cycleObjectArray:cycleObject[] = [];
|
||||
constructor(taskArg:Task){
|
||||
this.task = taskArg;
|
||||
};
|
||||
getPromiseForCycle(cycleCountArg:number){
|
||||
let done = plugins.Q.defer();
|
||||
let cycleObject:cycleObject = {
|
||||
cycleCounter:cycleCountArg,
|
||||
deferred:done
|
||||
};
|
||||
this.cycleObjectArray.push(cycleObject);
|
||||
return done.promise;
|
||||
};
|
||||
informOfCycle(){
|
||||
let newCycleObjectArray:cycleObject[] = [];
|
||||
this.cycleObjectArray.forEach(cycleObjectArg => {
|
||||
cycleObjectArg.cycleCounter--;
|
||||
if(cycleObjectArg.cycleCounter <= 0){
|
||||
cycleObjectArg.deferred.resolve();
|
||||
} else {
|
||||
newCycleObjectArray.push(cycleObjectArg);
|
||||
};
|
||||
});
|
||||
this.cycleObjectArray = newCycleObjectArray;
|
||||
}
|
||||
}
|
||||
|
||||
export class BufferRunner {
|
||||
task:Task;
|
||||
// initialze by default
|
||||
bufferCounter:number = 0;
|
||||
bufferMax:number = 1;
|
||||
running:boolean = false;
|
||||
constructor(taskArg: Task) {
|
||||
this.task = taskArg;
|
||||
};
|
||||
private _run() {
|
||||
let recursiveBufferRunner = () => {
|
||||
if (this.bufferCounter >= 0) {
|
||||
this.running = true;
|
||||
this.task.running = true;
|
||||
runTask(this.task)
|
||||
.then(() => {
|
||||
this.bufferCounter--;
|
||||
this.task.cycleCounter.informOfCycle();
|
||||
recursiveBufferRunner();
|
||||
});
|
||||
} else {
|
||||
this.running = false;
|
||||
this.task.running = false;
|
||||
}
|
||||
};
|
||||
recursiveBufferRunner();
|
||||
};
|
||||
setBufferMax(bufferMaxArg:number){
|
||||
this.bufferMax = bufferMaxArg;
|
||||
};
|
||||
trigger(): PromiseLike<any> {
|
||||
if(!(this.bufferCounter >= this.bufferMax)){
|
||||
this.bufferCounter++
|
||||
};
|
||||
let returnPromise:PromiseLike<any> = this.task.cycleCounter.getPromiseForCycle(this.bufferCounter + 1);
|
||||
if(!this.running){
|
||||
this._run();
|
||||
}
|
||||
return returnPromise;
|
||||
};
|
||||
};
|
@ -9,17 +9,17 @@ export interface ITaskFunction {
|
||||
export class Task {
|
||||
name:string;
|
||||
task:any;
|
||||
running:boolean = false;
|
||||
runningBuffered:boolean = false;
|
||||
idle:boolean = true;
|
||||
buffered:boolean = false;
|
||||
bufferCounter:number;
|
||||
bufferMax:number = 1;
|
||||
private _counterTriggerAbsolute:number = 0;
|
||||
private _state:string;
|
||||
buffered:boolean;
|
||||
preTask:Task;
|
||||
afterTask:Task;
|
||||
|
||||
// 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:{
|
||||
taskFunction:ITaskFunction,
|
||||
preTask?:Task,
|
||||
@ -32,10 +32,9 @@ export class Task {
|
||||
this.task = optionsArg.taskFunction;
|
||||
this.preTask = options.preTask;
|
||||
this.afterTask = options.afterTask;
|
||||
this.running = false;
|
||||
this.idle = !this.running && !this.runningBuffered;
|
||||
this.idle = !this.running;
|
||||
this.buffered = options.buffered;
|
||||
this.bufferMax = options.bufferMax;
|
||||
this.bufferRunner.setBufferMax(options.bufferMax);
|
||||
this.name = options.name;
|
||||
}
|
||||
|
||||
@ -58,22 +57,15 @@ export class Task {
|
||||
/**
|
||||
* trigger task unbuffered.
|
||||
*/
|
||||
triggerUnBuffered(){
|
||||
triggerUnBuffered():PromiseLike<any>{
|
||||
return helpers.runTask(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* trigger task buffered.
|
||||
*/
|
||||
triggerBuffered(){
|
||||
var done = plugins.Q.defer();
|
||||
if(!(this.bufferCounter >= this.bufferMax)){
|
||||
this.bufferCounter++
|
||||
};
|
||||
if(!this.runningBuffered){
|
||||
helpers.runBufferedTask(this);
|
||||
};
|
||||
return done.promise;
|
||||
triggerBuffered():PromiseLike<any>{
|
||||
return this.bufferRunner.trigger();
|
||||
}
|
||||
|
||||
get state():string {
|
||||
|
Loading…
Reference in New Issue
Block a user