implemented execDelay

This commit is contained in:
Philipp Kunz 2017-07-10 12:42:06 +02:00
parent 01d469960e
commit 3f275ebc08
6 changed files with 108 additions and 45 deletions

View File

@ -21,10 +21,8 @@ export declare class CycleCounter {
export declare class BufferRunner { export declare class BufferRunner {
task: Task; task: Task;
bufferCounter: number; bufferCounter: number;
bufferMax: number;
running: boolean; running: boolean;
constructor(taskArg: Task); constructor(taskArg: Task);
setBufferMax(bufferMaxArg: number): void;
trigger(x: any): Promise<any>; trigger(x: any): Promise<any>;
private _run(x); private _run(x);
} }

File diff suppressed because one or more lines are too long

View File

@ -6,6 +6,8 @@ export declare class Task {
name: string; name: string;
taskFunction: ITaskFunction; taskFunction: ITaskFunction;
buffered: boolean; buffered: boolean;
bufferMax: number;
execDelay: number;
preTask: Task; preTask: Task;
afterTask: Task; afterTask: Task;
running: boolean; running: boolean;
@ -14,11 +16,34 @@ export declare class Task {
idle: boolean; idle: boolean;
private _state; private _state;
constructor(optionsArg: { constructor(optionsArg: {
/**
* the task function to run, must return promise
*/
taskFunction: ITaskFunction; taskFunction: ITaskFunction;
/**
* any other task to run before
*/
preTask?: Task; preTask?: Task;
/**
* any other task to run after
*/
afterTask?: Task; afterTask?: Task;
/**
* wether this task should run buffered
*/
buffered?: boolean; buffered?: boolean;
/**
* the maximum buffer
*/
bufferMax?: number; bufferMax?: number;
/**
* the execution delay, before the task is executed
* only makes sense when running in buffered mode
*/
execDelay?: number;
/**
* the name of the task
*/
name?: string; name?: string;
}); });
/** /**

View File

@ -10,14 +10,14 @@ class Task {
this.cycleCounter = new helpers.CycleCounter(this); this.cycleCounter = new helpers.CycleCounter(this);
this.idle = true; this.idle = true;
this._state = 'ready'; this._state = 'ready';
let options = optionsArg;
this.taskFunction = optionsArg.taskFunction; this.taskFunction = optionsArg.taskFunction;
this.preTask = options.preTask; this.preTask = optionsArg.preTask;
this.afterTask = options.afterTask; this.afterTask = optionsArg.afterTask;
this.idle = !this.running; this.idle = !this.running;
this.buffered = options.buffered; this.buffered = optionsArg.buffered;
this.bufferRunner.setBufferMax(options.bufferMax); this.bufferMax = optionsArg.bufferMax;
this.name = options.name; this.execDelay = optionsArg.execDelay;
this.name = optionsArg.name;
} }
/** /**
* trigger the task. Will trigger buffered if this.buffered is true * trigger the task. Will trigger buffered if this.buffered is true
@ -55,4 +55,4 @@ class Task {
} }
} }
exports.Task = Task; exports.Task = Task;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFza2J1ZmZlci5jbGFzc2VzLnRhc2suanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy90YXNrYnVmZmVyLmNsYXNzZXMudGFzay50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLGdEQUErQztBQUMvQyx3REFBdUQ7QUFNdkQ7SUFjRSxZQUFhLFVBT1o7UUFkRCx3QkFBd0I7UUFDeEIsWUFBTyxHQUFZLEtBQUssQ0FBQTtRQUN4QixpQkFBWSxHQUFHLElBQUksT0FBTyxDQUFDLFlBQVksQ0FBQyxJQUFJLENBQUMsQ0FBQTtRQUM3QyxpQkFBWSxHQUFHLElBQUksT0FBTyxDQUFDLFlBQVksQ0FBQyxJQUFJLENBQUMsQ0FBQTtRQUM3QyxTQUFJLEdBQVksSUFBSSxDQUFBO1FBQ1osV0FBTSxHQUFXLE9BQU8sQ0FBQTtRQVU5QixJQUFJLE9BQU8sR0FBRyxVQUFVLENBQUE7UUFDeEIsSUFBSSxDQUFDLFlBQVksR0FBRyxVQUFVLENBQUMsWUFBWSxDQUFBO1FBQzNDLElBQUksQ0FBQyxPQUFPLEdBQUcsT0FBTyxDQUFDLE9BQU8sQ0FBQTtRQUM5QixJQUFJLENBQUMsU0FBUyxHQUFHLE9BQU8sQ0FBQyxTQUFTLENBQUE7UUFDbEMsSUFBSSxDQUFDLElBQUksR0FBRyxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7UUFDekIsSUFBSSxDQUFDLFFBQVEsR0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4QixDQUFDO1FBQUMsSUFBSSxDQUFDLENBQUM7WUFDTixPQUFPLENBQUMsU0FBUyxDQUFDLEtBQUssQ0FBQyxhQUFhLEdBQUcsUUFBUSxHQUFHLG1CQUFtQixDQUFDLENBQUE7UUFDekUsQ0FBQztJQUNILENBQUM7Q0FDRjtBQWxHRCxvQkFrR0MifQ==

View File

@ -28,12 +28,18 @@ export let isTaskTouched = (taskArg: Task, touchedTasksArray: Task[]): boolean =
return result return result
} }
export let runTask = function (taskArg: Task, optionsArg: { x?, touchedTasksArray?: Task[] }) { export let runTask = async (taskArg: Task, optionsArg: { x?, touchedTasksArray?: Task[] }) => {
let done = plugins.q.defer() let done = plugins.q.defer()
// pay respect to execDelay
if (taskArg.execDelay) {
await plugins.smartdelay.delayFor(taskArg.execDelay)
}
// set running params // set running params
taskArg.running = true taskArg.running = true
done.promise.then(function () { taskArg.running = false })
done.promise.then(async () => { taskArg.running = false })
// handle options // handle options
let options = plugins.lodash.merge( let options = plugins.lodash.merge(
@ -75,7 +81,7 @@ export let runTask = function (taskArg: Task, optionsArg: { x?, touchedTasksArra
console.log(err) console.log(err)
}) })
localDeferred.resolve() localDeferred.resolve()
return done.promise return await done.promise
} }
export interface cycleObject { export interface cycleObject {
@ -116,18 +122,13 @@ export class BufferRunner {
task: Task task: Task
// initialze by default // initialze by default
bufferCounter: number = 0 bufferCounter: number = 0
bufferMax: number = 1
running: boolean = false running: boolean = false
constructor(taskArg: Task) { constructor(taskArg: Task) {
this.task = taskArg this.task = taskArg
} }
setBufferMax (bufferMaxArg: number) {
this.bufferMax = bufferMaxArg
}
trigger (x): Promise<any> { trigger (x): Promise<any> {
if (!(this.bufferCounter >= this.bufferMax)) { if (!(this.bufferCounter >= this.task.bufferMax)) {
this.bufferCounter++ this.bufferCounter++
} }
let returnPromise: Promise<any> = this.task.cycleCounter.getPromiseForCycle(this.bufferCounter + 1) let returnPromise: Promise<any> = this.task.cycleCounter.getPromiseForCycle(this.bufferCounter + 1)

View File

@ -6,9 +6,15 @@ export interface ITaskFunction {
} }
export class Task { export class Task {
// man datory properties
name: string name: string
taskFunction: ITaskFunction taskFunction: ITaskFunction
buffered: boolean buffered: boolean
bufferMax: number
execDelay: number
// tasks to run before and after
preTask: Task preTask: Task
afterTask: Task afterTask: Task
@ -16,31 +22,55 @@ export class Task {
running: boolean = false running: boolean = false
bufferRunner = new helpers.BufferRunner(this) bufferRunner = new helpers.BufferRunner(this)
cycleCounter = new helpers.CycleCounter(this) cycleCounter = new helpers.CycleCounter(this)
idle: boolean = true idle: boolean = true
private _state: string = 'ready' private _state: string = 'ready'
constructor (optionsArg: { constructor(optionsArg: {
taskFunction: ITaskFunction, /**
preTask?: Task, * the task function to run, must return promise
afterTask?: Task, */
buffered?: boolean, taskFunction: ITaskFunction
bufferMax?: number, /**
* any other task to run before
*/
preTask?: Task
/**
* any other task to run after
*/
afterTask?: Task
/**
* wether this task should run buffered
*/
buffered?: boolean
/**
* the maximum buffer
*/
bufferMax?: number
/**
* the execution delay, before the task is executed
* only makes sense when running in buffered mode
*/
execDelay?: number
/**
* the name of the task
*/
name?: string name?: string
}) { }) {
let options = optionsArg
this.taskFunction = optionsArg.taskFunction this.taskFunction = optionsArg.taskFunction
this.preTask = options.preTask this.preTask = optionsArg.preTask
this.afterTask = options.afterTask this.afterTask = optionsArg.afterTask
this.idle = !this.running this.idle = !this.running
this.buffered = options.buffered this.buffered = optionsArg.buffered
this.bufferRunner.setBufferMax(options.bufferMax) this.bufferMax = optionsArg.bufferMax
this.name = options.name this.execDelay = optionsArg.execDelay
this.name = optionsArg.name
} }
/** /**
* trigger the task. Will trigger buffered if this.buffered is true * trigger the task. Will trigger buffered if this.buffered is true
*/ */
trigger (x?): Promise<any> { trigger(x?): Promise<any> {
if (this.buffered) { if (this.buffered) {
return this.triggerBuffered(x) return this.triggerBuffered(x)
} else { } else {
@ -51,21 +81,22 @@ export class Task {
/** /**
* trigger task unbuffered. * trigger task unbuffered.
*/ */
triggerUnBuffered (x?): Promise<any> { triggerUnBuffered(x?): Promise<any> {
return helpers.runTask(this, { x: x }) return helpers.runTask(this, { x: x })
} }
/** /**
* trigger task buffered. * trigger task buffered.
*/ */
triggerBuffered (x?): Promise<any> { triggerBuffered(x?): Promise<any> {
return this.bufferRunner.trigger(x) return this.bufferRunner.trigger(x)
} }
get state (): string { get state(): string {
return this._state return this._state
} }
set state (stateArg: string) {
set state(stateArg: string) {
if (stateArg === 'locked') { if (stateArg === 'locked') {
this._state = 'locked' this._state = 'locked'
} else { } else {