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-02 09:09:42 +00:00
|
|
|
export let emptyTaskFunction: ITaskFunction = function (x) {
|
2017-01-19 16:26:35 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-05-04 00:49:43 +00:00
|
|
|
done.resolve();
|
|
|
|
return done.promise;
|
|
|
|
};
|
|
|
|
|
2017-01-19 16:26:35 +00:00
|
|
|
export let isTask = function (taskArg: Task): boolean {
|
2016-08-01 14:10:00 +00:00
|
|
|
if (
|
2016-05-05 16:36:31 +00:00
|
|
|
taskArg instanceof Task
|
2017-01-19 16:26:35 +00:00
|
|
|
&& typeof taskArg.taskFunction === "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-02 09:09:42 +00:00
|
|
|
export let runTask = function (taskArg: Task, optionsArg: {x?, touchedTasksArray?: Task[] }) {
|
2017-01-19 16:26:35 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-08-02 09:09:42 +00:00
|
|
|
|
|
|
|
// set running params
|
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-08-02 09:09:42 +00:00
|
|
|
|
|
|
|
// handle options
|
|
|
|
let options = plugins.lodash.merge(
|
|
|
|
{x:undefined,touchedTasksArray: []},
|
|
|
|
optionsArg
|
|
|
|
)
|
|
|
|
let x = options.x;
|
|
|
|
let touchedTasksArray: Task[] = options.touchedTasksArray;
|
|
|
|
|
2016-05-05 16:06:04 +00:00
|
|
|
touchedTasksArray.push(taskArg);
|
2016-08-02 09:09:42 +00:00
|
|
|
|
|
|
|
// run the task cascade
|
2017-01-19 16:26:35 +00:00
|
|
|
let localDeferred = plugins.q.defer();
|
2016-05-05 16:06:04 +00:00
|
|
|
localDeferred.promise
|
2016-08-01 14:10:00 +00:00
|
|
|
.then(() => {
|
|
|
|
if (taskArg.preTask && !isTaskTouched(taskArg.preTask, touchedTasksArray)) {
|
2016-08-02 09:09:42 +00:00
|
|
|
return runTask(taskArg.preTask, {x:x, touchedTasksArray: touchedTasksArray })
|
2016-05-05 16:06:04 +00:00
|
|
|
} else {
|
2017-01-19 16:26:35 +00:00
|
|
|
let done2 = plugins.q.defer();
|
2016-08-02 09:09:42 +00:00
|
|
|
done2.resolve(x);
|
2016-05-05 16:06:04 +00:00
|
|
|
return done2.promise;
|
|
|
|
}
|
|
|
|
})
|
2016-08-02 09:09:42 +00:00
|
|
|
.then(x => {
|
|
|
|
return taskArg.taskFunction(x);
|
2016-05-05 16:06:04 +00:00
|
|
|
})
|
2016-08-02 09:09:42 +00:00
|
|
|
.then(x => {
|
2016-08-01 14:10:00 +00:00
|
|
|
if (taskArg.afterTask && !isTaskTouched(taskArg.afterTask, touchedTasksArray)) {
|
2016-08-02 09:09:42 +00:00
|
|
|
return runTask(taskArg.afterTask, {x:x, touchedTasksArray: touchedTasksArray });
|
2016-05-05 16:06:04 +00:00
|
|
|
} else {
|
2017-01-19 16:26:35 +00:00
|
|
|
let done2 = plugins.q.defer();
|
2016-08-02 09:09:42 +00:00
|
|
|
done2.resolve(x);
|
2016-05-05 16:06:04 +00:00
|
|
|
return done2.promise;
|
|
|
|
}
|
|
|
|
})
|
2016-08-02 09:09:42 +00:00
|
|
|
.then(x => {
|
|
|
|
done.resolve(x);
|
2016-08-03 09:28:13 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
console.log(err);
|
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,
|
2017-01-19 16:26:35 +00:00
|
|
|
deferred: plugins.q.Deferred<any>
|
2016-08-01 14:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CycleCounter {
|
|
|
|
task:Task;
|
|
|
|
cycleObjectArray:cycleObject[] = [];
|
|
|
|
constructor(taskArg:Task){
|
|
|
|
this.task = taskArg;
|
|
|
|
};
|
|
|
|
getPromiseForCycle(cycleCountArg:number){
|
2017-01-19 16:26:35 +00:00
|
|
|
let done = plugins.q.defer();
|
2016-08-01 14:10:00 +00:00
|
|
|
let cycleObject:cycleObject = {
|
|
|
|
cycleCounter:cycleCountArg,
|
|
|
|
deferred:done
|
|
|
|
};
|
|
|
|
this.cycleObjectArray.push(cycleObject);
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-08-03 12:51:43 +00:00
|
|
|
informOfCycle(x){
|
2016-08-01 14:10:00 +00:00
|
|
|
let newCycleObjectArray:cycleObject[] = [];
|
|
|
|
this.cycleObjectArray.forEach(cycleObjectArg => {
|
|
|
|
cycleObjectArg.cycleCounter--;
|
|
|
|
if(cycleObjectArg.cycleCounter <= 0){
|
2016-08-03 12:51:43 +00:00
|
|
|
cycleObjectArg.deferred.resolve(x);
|
2016-08-01 14:10:00 +00:00
|
|
|
} 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;
|
|
|
|
};
|
2016-08-02 09:09:42 +00:00
|
|
|
private _run(x) {
|
|
|
|
let recursiveBufferRunner = (x) => {
|
2016-08-01 14:10:00 +00:00
|
|
|
if (this.bufferCounter >= 0) {
|
|
|
|
this.running = true;
|
|
|
|
this.task.running = true;
|
2016-08-02 09:09:42 +00:00
|
|
|
runTask(this.task,{x:x})
|
|
|
|
.then((x) => {
|
2016-08-01 14:10:00 +00:00
|
|
|
this.bufferCounter--;
|
2016-08-03 12:51:43 +00:00
|
|
|
this.task.cycleCounter.informOfCycle(x);
|
2016-08-02 09:09:42 +00:00
|
|
|
recursiveBufferRunner(x);
|
2016-08-01 14:10:00 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.running = false;
|
|
|
|
this.task.running = false;
|
|
|
|
}
|
|
|
|
};
|
2016-08-02 09:09:42 +00:00
|
|
|
recursiveBufferRunner(x);
|
2016-08-01 14:10:00 +00:00
|
|
|
};
|
|
|
|
setBufferMax(bufferMaxArg:number){
|
|
|
|
this.bufferMax = bufferMaxArg;
|
|
|
|
};
|
2016-08-03 12:51:43 +00:00
|
|
|
trigger(x): Promise<any> {
|
2016-08-01 14:10:00 +00:00
|
|
|
if(!(this.bufferCounter >= this.bufferMax)){
|
|
|
|
this.bufferCounter++
|
|
|
|
};
|
2016-08-03 12:51:43 +00:00
|
|
|
let returnPromise:Promise<any> = this.task.cycleCounter.getPromiseForCycle(this.bufferCounter + 1);
|
2016-08-01 14:10:00 +00:00
|
|
|
if(!this.running){
|
2016-08-02 09:09:42 +00:00
|
|
|
this._run(x);
|
2016-08-01 14:10:00 +00:00
|
|
|
}
|
|
|
|
return returnPromise;
|
|
|
|
};
|
2016-08-01 11:17:15 +00:00
|
|
|
};
|