2016-05-04 00:49:43 +00:00
|
|
|
import plugins = require("./taskbuffer.plugins");
|
2016-08-01 14:10:00 +00:00
|
|
|
import { Task, ITaskFunction } from "./taskbuffer.classes.task";
|
2016-05-04 00:49:43 +00:00
|
|
|
|
2016-08-01 14:10:00 +00:00
|
|
|
export let emptyTaskFunction: ITaskFunction = function () {
|
2016-05-05 16:06:04 +00:00
|
|
|
let done = plugins.Q.defer();
|
2016-05-04 00:49:43 +00:00
|
|
|
done.resolve();
|
|
|
|
return done.promise;
|
|
|
|
};
|
|
|
|
|
2016-08-01 14:10:00 +00:00
|
|
|
export let isTask = function (taskArg): boolean {
|
|
|
|
if (
|
2016-05-05 16:36:31 +00:00
|
|
|
taskArg instanceof Task
|
2016-05-04 00:49:43 +00:00
|
|
|
&& typeof taskArg.task === "function"
|
2016-08-01 14:10:00 +00:00
|
|
|
) {
|
2016-05-04 00:49:43 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-05 16:06:04 +00:00
|
|
|
|
2016-08-01 14:10:00 +00:00
|
|
|
export let isTaskTouched = (taskArg: Task, touchedTasksArray: Task[]): boolean => {
|
2016-05-06 00:05:45 +00:00
|
|
|
let result = false;
|
2016-08-01 14:10:00 +00:00
|
|
|
for (let keyArg in touchedTasksArray) {
|
|
|
|
if (taskArg === touchedTasksArray[keyArg]) {
|
2016-05-06 00:05:45 +00:00
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2016-05-05 16:06:04 +00:00
|
|
|
}
|
|
|
|
|
2016-08-01 14:10:00 +00:00
|
|
|
export let runTask = function (taskArg: Task, optionsArg: { touchedTasksArray: Task[] } = { touchedTasksArray: [] }) {
|
2016-05-05 16:06:04 +00:00
|
|
|
let done = plugins.Q.defer();
|
2016-08-01 11:17:15 +00:00
|
|
|
taskArg.running = true;
|
2016-08-01 14:10:00 +00:00
|
|
|
done.promise.then(function () { taskArg.running = false });
|
2016-05-05 16:06:04 +00:00
|
|
|
let localDeferred = plugins.Q.defer();
|
2016-08-01 14:10:00 +00:00
|
|
|
let touchedTasksArray: Task[];
|
|
|
|
if (optionsArg.touchedTasksArray) {
|
2016-05-05 16:06:04 +00:00
|
|
|
touchedTasksArray = optionsArg.touchedTasksArray;
|
|
|
|
} else {
|
|
|
|
touchedTasksArray = [];
|
2016-05-04 00:49:43 +00:00
|
|
|
}
|
2016-05-05 16:06:04 +00:00
|
|
|
touchedTasksArray.push(taskArg);
|
|
|
|
localDeferred.promise
|
2016-08-01 14:10:00 +00:00
|
|
|
.then(() => {
|
|
|
|
if (taskArg.preTask && !isTaskTouched(taskArg.preTask, touchedTasksArray)) {
|
|
|
|
return runTask(taskArg.preTask, { touchedTasksArray: touchedTasksArray })
|
2016-05-05 16:06:04 +00:00
|
|
|
} else {
|
2016-05-06 00:05:45 +00:00
|
|
|
let done2 = plugins.Q.defer();
|
2016-05-05 16:06:04 +00:00
|
|
|
done2.resolve();
|
|
|
|
return done2.promise;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return taskArg.task();
|
|
|
|
})
|
|
|
|
.then(() => {
|
2016-08-01 14:10:00 +00:00
|
|
|
if (taskArg.afterTask && !isTaskTouched(taskArg.afterTask, touchedTasksArray)) {
|
|
|
|
return runTask(taskArg.afterTask, { touchedTasksArray: touchedTasksArray })
|
2016-05-05 16:06:04 +00:00
|
|
|
} else {
|
2016-05-06 00:05:45 +00:00
|
|
|
let done2 = plugins.Q.defer();
|
2016-05-05 16:06:04 +00:00
|
|
|
done2.resolve();
|
|
|
|
return done2.promise;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
done.resolve();
|
2016-05-06 00:05:45 +00:00
|
|
|
});
|
|
|
|
localDeferred.resolve();
|
2016-05-04 00:49:43 +00:00
|
|
|
return done.promise;
|
2016-05-05 17:21:01 +00:00
|
|
|
};
|
|
|
|
|
2016-08-01 14:10:00 +00:00
|
|
|
|
|
|
|
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;
|
2016-05-06 00:05:45 +00:00
|
|
|
}
|
2016-08-01 14:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
2016-08-01 11:17:15 +00:00
|
|
|
};
|