2017-06-09 21:33:41 +00:00
|
|
|
import plugins = require('./taskbuffer.plugins')
|
2017-02-15 21:52:29 +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-02-15 21:52:29 +00:00
|
|
|
let done = plugins.q.defer()
|
|
|
|
done.resolve()
|
|
|
|
return done.promise
|
|
|
|
}
|
2016-05-04 00:49:43 +00:00
|
|
|
|
2017-01-19 16:26:35 +00:00
|
|
|
export let isTask = function (taskArg: Task): boolean {
|
2017-02-15 21:52:29 +00:00
|
|
|
if (
|
|
|
|
taskArg instanceof Task
|
|
|
|
&& typeof taskArg.taskFunction === 'function'
|
|
|
|
) {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 00:49:43 +00:00
|
|
|
|
2016-08-01 14:10:00 +00:00
|
|
|
export let isTaskTouched = (taskArg: Task, touchedTasksArray: Task[]): boolean => {
|
2017-02-15 21:52:29 +00:00
|
|
|
let result = false
|
|
|
|
for (let keyArg in touchedTasksArray) {
|
|
|
|
if (taskArg === touchedTasksArray[ keyArg ]) {
|
|
|
|
result = true
|
2016-05-06 00:05:45 +00:00
|
|
|
}
|
2017-02-15 21:52:29 +00:00
|
|
|
}
|
|
|
|
return result
|
2016-05-05 16:06:04 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 10:42:06 +00:00
|
|
|
export let runTask = async (taskArg: Task, optionsArg: { x?, touchedTasksArray?: Task[] }) => {
|
2017-02-15 21:52:29 +00:00
|
|
|
let done = plugins.q.defer()
|
|
|
|
|
2017-07-10 10:42:06 +00:00
|
|
|
// pay respect to execDelay
|
|
|
|
if (taskArg.execDelay) {
|
|
|
|
await plugins.smartdelay.delayFor(taskArg.execDelay)
|
|
|
|
}
|
|
|
|
|
2017-02-15 21:52:29 +00:00
|
|
|
// set running params
|
|
|
|
taskArg.running = true
|
2017-07-10 10:42:06 +00:00
|
|
|
|
|
|
|
done.promise.then(async () => { taskArg.running = false })
|
2016-08-02 09:09:42 +00:00
|
|
|
|
2017-02-15 21:52:29 +00:00
|
|
|
// handle options
|
|
|
|
let options = plugins.lodash.merge(
|
|
|
|
{ x: undefined, touchedTasksArray: [] },
|
|
|
|
optionsArg
|
|
|
|
)
|
|
|
|
let x = options.x
|
|
|
|
let touchedTasksArray: Task[] = options.touchedTasksArray
|
|
|
|
|
|
|
|
touchedTasksArray.push(taskArg)
|
|
|
|
|
|
|
|
// run the task cascade
|
|
|
|
let localDeferred = plugins.q.defer()
|
|
|
|
localDeferred.promise
|
|
|
|
.then(() => {
|
|
|
|
if (taskArg.preTask && !isTaskTouched(taskArg.preTask, touchedTasksArray)) {
|
|
|
|
return runTask(taskArg.preTask, { x: x, touchedTasksArray: touchedTasksArray })
|
|
|
|
} else {
|
|
|
|
let done2 = plugins.q.defer()
|
|
|
|
done2.resolve(x)
|
|
|
|
return done2.promise
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(x => {
|
|
|
|
return taskArg.taskFunction(x)
|
|
|
|
})
|
|
|
|
.then(x => {
|
|
|
|
if (taskArg.afterTask && !isTaskTouched(taskArg.afterTask, touchedTasksArray)) {
|
|
|
|
return runTask(taskArg.afterTask, { x: x, touchedTasksArray: touchedTasksArray })
|
|
|
|
} else {
|
|
|
|
let done2 = plugins.q.defer()
|
|
|
|
done2.resolve(x)
|
|
|
|
return done2.promise
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(x => {
|
|
|
|
done.resolve(x)
|
|
|
|
}).catch((err) => {
|
|
|
|
console.log(err)
|
|
|
|
})
|
|
|
|
localDeferred.resolve()
|
2017-07-10 10:42:06 +00:00
|
|
|
return await done.promise
|
2017-02-15 21:52:29 +00:00
|
|
|
}
|
2016-05-05 17:21:01 +00:00
|
|
|
|
2016-08-01 14:10:00 +00:00
|
|
|
export interface cycleObject {
|
2017-02-15 21:52:29 +00:00
|
|
|
cycleCounter: number,
|
|
|
|
deferred: plugins.q.Deferred<any>
|
2016-08-01 14:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CycleCounter {
|
2017-02-15 21:52:29 +00:00
|
|
|
task: Task
|
|
|
|
cycleObjectArray: cycleObject[] = []
|
|
|
|
constructor(taskArg: Task) {
|
|
|
|
this.task = taskArg
|
2017-06-09 21:33:41 +00:00
|
|
|
}
|
|
|
|
getPromiseForCycle (cycleCountArg: number) {
|
2017-02-15 21:52:29 +00:00
|
|
|
let done = plugins.q.defer()
|
|
|
|
let cycleObject: cycleObject = {
|
|
|
|
cycleCounter: cycleCountArg,
|
|
|
|
deferred: done
|
2016-05-06 00:05:45 +00:00
|
|
|
}
|
2017-02-15 21:52:29 +00:00
|
|
|
this.cycleObjectArray.push(cycleObject)
|
|
|
|
return done.promise
|
2017-06-09 21:33:41 +00:00
|
|
|
}
|
|
|
|
informOfCycle (x) {
|
2017-02-15 21:52:29 +00:00
|
|
|
let newCycleObjectArray: cycleObject[] = []
|
|
|
|
this.cycleObjectArray.forEach(cycleObjectArg => {
|
|
|
|
cycleObjectArg.cycleCounter--
|
|
|
|
if (cycleObjectArg.cycleCounter <= 0) {
|
|
|
|
cycleObjectArg.deferred.resolve(x)
|
|
|
|
} else {
|
|
|
|
newCycleObjectArray.push(cycleObjectArg)
|
2017-06-09 21:33:41 +00:00
|
|
|
}
|
2017-02-15 21:52:29 +00:00
|
|
|
})
|
|
|
|
this.cycleObjectArray = newCycleObjectArray
|
|
|
|
}
|
2016-08-01 14:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class BufferRunner {
|
2017-02-15 21:52:29 +00:00
|
|
|
task: Task
|
|
|
|
// initialze by default
|
|
|
|
bufferCounter: number = 0
|
|
|
|
running: boolean = false
|
|
|
|
constructor(taskArg: Task) {
|
|
|
|
this.task = taskArg
|
2017-06-09 21:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trigger (x): Promise<any> {
|
2017-07-10 10:42:06 +00:00
|
|
|
if (!(this.bufferCounter >= this.task.bufferMax)) {
|
2017-06-09 21:33:41 +00:00
|
|
|
this.bufferCounter++
|
|
|
|
}
|
|
|
|
let returnPromise: Promise<any> = this.task.cycleCounter.getPromiseForCycle(this.bufferCounter + 1)
|
|
|
|
if (!this.running) {
|
|
|
|
this._run(x)
|
|
|
|
}
|
|
|
|
return returnPromise
|
|
|
|
}
|
|
|
|
|
|
|
|
private _run (x) {
|
2017-02-15 21:52:29 +00:00
|
|
|
let recursiveBufferRunner = (x) => {
|
|
|
|
if (this.bufferCounter >= 0) {
|
|
|
|
this.running = true
|
|
|
|
this.task.running = true
|
|
|
|
runTask(this.task, { x: x })
|
|
|
|
.then((x) => {
|
|
|
|
this.bufferCounter--
|
|
|
|
this.task.cycleCounter.informOfCycle(x)
|
|
|
|
recursiveBufferRunner(x)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this.running = false
|
|
|
|
this.task.running = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
recursiveBufferRunner(x)
|
2017-06-09 21:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|