From 05f91c3e35e068a2cf4edff3860ca508b9b9e3c3 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Fri, 4 Aug 2023 13:03:28 +0200 Subject: [PATCH] feat(Task): Tasks can now be blocked by other tasks. --- test/test.1.task.ts | 7 ----- ts/00_commitinfo_data.ts | 2 +- ts/taskbuffer.classes.task.ts | 53 ++++++++++++++++++++++------------- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/test/test.1.task.ts b/test/test.1.task.ts index 5daf62d..fd325a3 100644 --- a/test/test.1.task.ts +++ b/test/test.1.task.ts @@ -12,13 +12,6 @@ tap.test('new Task() should return a new task', async () => { expect(testTask).toBeInstanceOf(taskbuffer.Task); }); -tap.test('should be able to get the task state', async () => { - const testTask = new taskbuffer.Task({ - taskFunction: async () => {}, - }); - expect(testTask.state).toEqual('ready'); -}); - tap.test('should have bufferMax set to the provided value', async () => { const task2 = new taskbuffer.Task({ taskFunction: async () => {}, diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index e8f1b97..ebef118 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/taskbuffer', - version: '3.0.15', + version: '3.1.0', description: 'flexible task management. TypeScript ready!' } diff --git a/ts/taskbuffer.classes.task.ts b/ts/taskbuffer.classes.task.ts index cc7700c..7c268f3 100644 --- a/ts/taskbuffer.classes.task.ts +++ b/ts/taskbuffer.classes.task.ts @@ -15,8 +15,9 @@ export interface ITaskSetupFunction { export type TPreOrAfterTaskFunction = () => Task; export class Task { - // STATIC - public static extractTask(preOrAfterTaskArg: Task | TPreOrAfterTaskFunction): Task { + public static extractTask( + preOrAfterTaskArg: Task | TPreOrAfterTaskFunction + ): Task { switch (true) { case !preOrAfterTaskArg: return null; @@ -44,7 +45,7 @@ export class Task { } }; - public static isTaskTouched ( + public static isTaskTouched( taskArg: Task | TPreOrAfterTaskFunction, touchedTasksArray: Task[] ): boolean { @@ -56,7 +57,7 @@ export class Task { } } return result; - }; + } public static runTask = async ( taskArg: Task | TPreOrAfterTaskFunction, @@ -65,6 +66,11 @@ export class Task { const taskToRun = Task.extractTask(taskArg); const done = plugins.smartpromise.defer(); + // Wait for all blocking tasks to finish + for (const task of taskToRun.blockingTasks) { + await task.finished; + } + if (!taskToRun.setupValue && taskToRun.taskSetup) { taskToRun.setupValue = await taskToRun.taskSetup(); } @@ -77,6 +83,14 @@ export class Task { done.promise.then(async () => { taskToRun.running = false; + + // When the task has finished running, resolve the finished promise + taskToRun.resolveFinished(); + + // Create a new finished promise for the next run + taskToRun.finished = new Promise((resolve) => { + taskToRun.resolveFinished = resolve; + }); }); const options = { @@ -125,7 +139,6 @@ export class Task { return await done.promise; }; - // INSTANCE public name: string; public version: string; public taskFunction: ITaskFunction; @@ -139,12 +152,20 @@ export class Task { public preTask: Task | TPreOrAfterTaskFunction; public afterTask: Task | TPreOrAfterTaskFunction; + // Add a list to store the blocking tasks + public blockingTasks: Task[] = []; + + // Add a promise that will resolve when the task has finished + private finished: Promise; + private resolveFinished: () => void; + public running: boolean = false; public bufferRunner = new BufferRunner(this); public cycleCounter = new CycleCounter(this); - public idle: boolean = true; - private _state: string = 'ready'; + public get idle() { + return !this.running; + } public taskSetup: ITaskSetupFunction; public setupValue: T; @@ -162,12 +183,16 @@ export class Task { this.taskFunction = optionsArg.taskFunction; this.preTask = optionsArg.preTask; this.afterTask = optionsArg.afterTask; - this.idle = !this.running; this.buffered = optionsArg.buffered; this.bufferMax = optionsArg.bufferMax; this.execDelay = optionsArg.execDelay; this.name = optionsArg.name; this.taskSetup = optionsArg.taskSetup; + + // Create the finished promise + this.finished = new Promise((resolve) => { + this.resolveFinished = resolve; + }); } public trigger(x?: any): Promise { @@ -185,16 +210,4 @@ export class Task { public triggerBuffered(x?: any): Promise { return this.bufferRunner.trigger(x); } - - get state(): string { - return this._state; - } - - set state(stateArg: string) { - if (stateArg === 'locked') { - this._state = 'locked'; - } else { - logger.log('error', `state type ${stateArg} could not be set`); - } - } }