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?: {
|
export declare let runTask: (taskArg: Task, optionsArg?: {
|
||||||
touchedTasksArray: Task[];
|
touchedTasksArray: Task[];
|
||||||
}) => plugins.Q.Promise<{}>;
|
}) => 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>;
|
||||||
|
}
|
||||||
|
89
dist/taskbuffer.classes.helpers.js
vendored
89
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" />
|
/// <reference types="q" />
|
||||||
import * as plugins from "./taskbuffer.plugins";
|
import * as plugins from "./taskbuffer.plugins";
|
||||||
|
import * as helpers from "./taskbuffer.classes.helpers";
|
||||||
export interface ITaskFunction {
|
export interface ITaskFunction {
|
||||||
(): PromiseLike<any>;
|
(): PromiseLike<any>;
|
||||||
}
|
}
|
||||||
export declare class Task {
|
export declare class Task {
|
||||||
name: string;
|
name: string;
|
||||||
task: any;
|
task: any;
|
||||||
running: boolean;
|
|
||||||
runningBuffered: boolean;
|
|
||||||
idle: boolean;
|
|
||||||
buffered: boolean;
|
buffered: boolean;
|
||||||
bufferCounter: number;
|
|
||||||
bufferMax: number;
|
|
||||||
private _counterTriggerAbsolute;
|
|
||||||
private _state;
|
|
||||||
preTask: Task;
|
preTask: Task;
|
||||||
afterTask: Task;
|
afterTask: Task;
|
||||||
|
running: boolean;
|
||||||
|
bufferRunner: helpers.BufferRunner;
|
||||||
|
cycleCounter: helpers.CycleCounter;
|
||||||
|
idle: boolean;
|
||||||
|
private _state;
|
||||||
constructor(optionsArg: {
|
constructor(optionsArg: {
|
||||||
taskFunction: ITaskFunction;
|
taskFunction: ITaskFunction;
|
||||||
preTask?: Task;
|
preTask?: Task;
|
||||||
@ -31,10 +30,10 @@ export declare class Task {
|
|||||||
/**
|
/**
|
||||||
* trigger task unbuffered.
|
* trigger task unbuffered.
|
||||||
*/
|
*/
|
||||||
triggerUnBuffered(): plugins.Q.Promise<{}>;
|
triggerUnBuffered(): PromiseLike<any>;
|
||||||
/**
|
/**
|
||||||
* trigger task buffered.
|
* trigger task buffered.
|
||||||
*/
|
*/
|
||||||
triggerBuffered(): plugins.Q.Promise<{}>;
|
triggerBuffered(): PromiseLike<any>;
|
||||||
state: string;
|
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");
|
const helpers = require("./taskbuffer.classes.helpers");
|
||||||
class Task {
|
class Task {
|
||||||
constructor(optionsArg) {
|
constructor(optionsArg) {
|
||||||
|
// initialize by default
|
||||||
this.running = false;
|
this.running = false;
|
||||||
this.runningBuffered = false;
|
this.bufferRunner = new helpers.BufferRunner(this);
|
||||||
|
this.cycleCounter = new helpers.CycleCounter(this);
|
||||||
this.idle = true;
|
this.idle = true;
|
||||||
this.buffered = false;
|
this._state = "ready";
|
||||||
this.bufferMax = 1;
|
|
||||||
this._counterTriggerAbsolute = 0;
|
|
||||||
var options = optionsArg;
|
var options = optionsArg;
|
||||||
this.task = optionsArg.taskFunction;
|
this.task = optionsArg.taskFunction;
|
||||||
this.preTask = options.preTask;
|
this.preTask = options.preTask;
|
||||||
this.afterTask = options.afterTask;
|
this.afterTask = options.afterTask;
|
||||||
this.running = false;
|
this.idle = !this.running;
|
||||||
this.idle = !this.running && !this.runningBuffered;
|
|
||||||
this.buffered = options.buffered;
|
this.buffered = options.buffered;
|
||||||
this.bufferMax = options.bufferMax;
|
this.bufferRunner.setBufferMax(options.bufferMax);
|
||||||
this.name = options.name;
|
this.name = options.name;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -46,16 +45,7 @@ class Task {
|
|||||||
* trigger task buffered.
|
* trigger task buffered.
|
||||||
*/
|
*/
|
||||||
triggerBuffered() {
|
triggerBuffered() {
|
||||||
var done = plugins.Q.defer();
|
return this.bufferRunner.trigger();
|
||||||
if (!(this.bufferCounter >= this.bufferMax)) {
|
|
||||||
this.bufferCounter++;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
if (!this.runningBuffered) {
|
|
||||||
helpers.runBufferedTask(this);
|
|
||||||
}
|
|
||||||
;
|
|
||||||
return done.promise;
|
|
||||||
}
|
}
|
||||||
get state() {
|
get state() {
|
||||||
return this._state;
|
return this._state;
|
||||||
@ -70,4 +60,4 @@ class Task {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.Task = Task;
|
exports.Task = Task;
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLnRhc2suanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyI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BQyxJQUFJLENBQUMsQ0FBQztJQUNqQyxDQUFDO0lBRUQ7O09BRUc7SUFDSCxlQUFlO1FBQ1gsTUFBTSxDQUFDLElBQUksQ0FBQyxZQUFZLENBQUMsT0FBTyxFQUFFLENBQUM7SUFDdkMsQ0FBQztJQUVELElBQUksS0FBSztRQUNMLE1BQU0sQ0FBQyxJQUFJLENBQUMsTUFBTSxDQUFDO0lBQ3ZCLENBQUM7SUFDRCxJQUFJLEtBQUssQ0FBQyxRQUFlO1FBQ3JCLEVBQUUsQ0FBQyxDQUFDLFFBQVEsSUFBSSxRQUFRLENBQUMsQ0FBQSxDQUFDO1lBQ3RCLElBQUksQ0FBQyxNQUFNLEdBQUcsUUFBUSxDQUFDO1FBQzNCLENBQUM7UUFBQyxJQUFJLENBQUMsQ0FBQztZQUNKLE9BQU8sQ0FBQyxTQUFTLENBQUMsS0FBSyxDQUFDLGFBQWEsR0FBRyxRQUFRLENBQUMsSUFBSSxHQUFHLG1CQUFtQixDQUFDLENBQUM7UUFDakYsQ0FBQztJQUNMLENBQUM7QUFDTCxDQUFDO0FBeEVZLFlBQUksT0F3RWhCLENBQUEifQ==
|
145
test/test.js
145
test/test.js
File diff suppressed because one or more lines are too long
150
test/test.ts
150
test/test.ts
@ -10,7 +10,18 @@ let testTaskFunction = function(){
|
|||||||
console.log("main function executed!")
|
console.log("main function executed!")
|
||||||
done.resolve();
|
done.resolve();
|
||||||
return done.promise;
|
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({
|
let testPreTask = new taskbuffer.Task({
|
||||||
taskFunction: function () {
|
taskFunction: function () {
|
||||||
let done = q.defer();
|
let done = q.defer();
|
||||||
@ -21,66 +32,8 @@ let testPreTask = new taskbuffer.Task({
|
|||||||
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});
|
|
||||||
});
|
|
||||||
it("testTask should be and instance of Task",function(){
|
|
||||||
testTask.should.be.instanceof(taskbuffer.Task);
|
|
||||||
});
|
|
||||||
it("testTask.idle is true",function(){
|
|
||||||
if (!testTask.idle){
|
|
||||||
throw new Error("testTask.idle is not true");
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
// some more tasks to wirj with
|
||||||
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(){
|
|
||||||
testTask.trigger().should.be.Promise();
|
|
||||||
});
|
|
||||||
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});
|
|
||||||
localTestTask.trigger().then(done);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe("Taskchain",function(){
|
|
||||||
let testTaskchain;
|
|
||||||
let testTaskArray = [
|
|
||||||
new taskbuffer.Task({
|
|
||||||
name:"task1",
|
|
||||||
taskFunction:function(){
|
|
||||||
let done = q.defer();
|
|
||||||
setTimeout(done.resolve,2000);
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
new taskbuffer.Task({
|
|
||||||
name:"task2",
|
|
||||||
taskFunction: function(){
|
|
||||||
let done = q.defer();
|
|
||||||
setTimeout(done.resolve,2000);
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
];
|
|
||||||
it("should run tasks in sequence",function(done){
|
|
||||||
this.timeout(5000);
|
|
||||||
testTaskchain = new taskbuffer.Taskchain({
|
|
||||||
name:"Taskchain1",
|
|
||||||
taskArray:testTaskArray
|
|
||||||
});
|
|
||||||
testTaskchain.trigger().then(done);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe("taskparallel",function(){
|
|
||||||
let task1 = new taskbuffer.Task({
|
let task1 = new taskbuffer.Task({
|
||||||
name: "Task 1",
|
name: "Task 1",
|
||||||
taskFunction: () => {
|
taskFunction: () => {
|
||||||
@ -117,6 +70,83 @@ describe("taskbuffer",function(){
|
|||||||
return done.promise;
|
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 () {
|
||||||
|
testTask.should.be.instanceof(taskbuffer.Task);
|
||||||
|
});
|
||||||
|
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 () {
|
||||||
|
testTask.running.should.be.type("boolean");
|
||||||
|
testTask.running.should.be.false();
|
||||||
|
});
|
||||||
|
it("testTask.trigger() should return Promise", function () {
|
||||||
|
testTask.trigger().should.be.Promise();
|
||||||
|
});
|
||||||
|
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 });
|
||||||
|
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 () {
|
||||||
|
let testTaskchain;
|
||||||
|
let testTaskArray = [
|
||||||
|
new taskbuffer.Task({
|
||||||
|
name: "task1",
|
||||||
|
taskFunction: function () {
|
||||||
|
let done = q.defer();
|
||||||
|
setTimeout(done.resolve, 2000);
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
new taskbuffer.Task({
|
||||||
|
name: "task2",
|
||||||
|
taskFunction: function () {
|
||||||
|
let done = q.defer();
|
||||||
|
setTimeout(done.resolve, 2000);
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
it("should run tasks in sequence", function (done) {
|
||||||
|
this.timeout(5000);
|
||||||
|
testTaskchain = new taskbuffer.Taskchain({
|
||||||
|
name: "Taskchain1",
|
||||||
|
taskArray: testTaskArray
|
||||||
|
});
|
||||||
|
testTaskchain.trigger().then(done);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe("taskparallel", function () {
|
||||||
it("should run in Parallel", function (done) {
|
it("should run in Parallel", function (done) {
|
||||||
this.timeout("7000");
|
this.timeout("7000");
|
||||||
let testTaskparallel = new taskbuffer.Taskparallel({
|
let testTaskparallel = new taskbuffer.Taskparallel({
|
||||||
|
@ -70,15 +70,79 @@ export let runTask = function(taskArg:Task,optionsArg:{touchedTasksArray:Task[]}
|
|||||||
return done.promise;
|
return done.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
export let runBufferedTask = (taskArg:Task) => {
|
|
||||||
let recursiveBufferRunner = () => {
|
export interface cycleObject {
|
||||||
if(taskArg.bufferCounter > 0){
|
cycleCounter:number,
|
||||||
taskArg.runningBuffered = true;
|
deferred:plugins.Q.Deferred<any>
|
||||||
taskArg.bufferCounter--;
|
|
||||||
runTask(taskArg)
|
|
||||||
.then(recursiveBufferRunner);
|
|
||||||
} else {
|
|
||||||
taskArg.runningBuffered = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
export class Task {
|
||||||
name:string;
|
name:string;
|
||||||
task:any;
|
task:any;
|
||||||
running:boolean = false;
|
buffered:boolean;
|
||||||
runningBuffered:boolean = false;
|
|
||||||
idle:boolean = true;
|
|
||||||
buffered:boolean = false;
|
|
||||||
bufferCounter:number;
|
|
||||||
bufferMax:number = 1;
|
|
||||||
private _counterTriggerAbsolute:number = 0;
|
|
||||||
private _state:string;
|
|
||||||
preTask:Task;
|
preTask:Task;
|
||||||
afterTask: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:{
|
constructor(optionsArg:{
|
||||||
taskFunction:ITaskFunction,
|
taskFunction:ITaskFunction,
|
||||||
preTask?:Task,
|
preTask?:Task,
|
||||||
@ -32,10 +32,9 @@ export class Task {
|
|||||||
this.task = optionsArg.taskFunction;
|
this.task = optionsArg.taskFunction;
|
||||||
this.preTask = options.preTask;
|
this.preTask = options.preTask;
|
||||||
this.afterTask = options.afterTask;
|
this.afterTask = options.afterTask;
|
||||||
this.running = false;
|
this.idle = !this.running;
|
||||||
this.idle = !this.running && !this.runningBuffered;
|
|
||||||
this.buffered = options.buffered;
|
this.buffered = options.buffered;
|
||||||
this.bufferMax = options.bufferMax;
|
this.bufferRunner.setBufferMax(options.bufferMax);
|
||||||
this.name = options.name;
|
this.name = options.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,22 +57,15 @@ export class Task {
|
|||||||
/**
|
/**
|
||||||
* trigger task unbuffered.
|
* trigger task unbuffered.
|
||||||
*/
|
*/
|
||||||
triggerUnBuffered(){
|
triggerUnBuffered():PromiseLike<any>{
|
||||||
return helpers.runTask(this);
|
return helpers.runTask(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* trigger task buffered.
|
* trigger task buffered.
|
||||||
*/
|
*/
|
||||||
triggerBuffered(){
|
triggerBuffered():PromiseLike<any>{
|
||||||
var done = plugins.Q.defer();
|
return this.bufferRunner.trigger();
|
||||||
if(!(this.bufferCounter >= this.bufferMax)){
|
|
||||||
this.bufferCounter++
|
|
||||||
};
|
|
||||||
if(!this.runningBuffered){
|
|
||||||
helpers.runBufferedTask(this);
|
|
||||||
};
|
|
||||||
return done.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get state():string {
|
get state():string {
|
||||||
|
Loading…
Reference in New Issue
Block a user