feat(Task): Tasks can now be blocked by other tasks.

This commit is contained in:
Philipp Kunz 2023-08-04 13:03:28 +02:00
parent 94e327c722
commit 05f91c3e35
3 changed files with 34 additions and 28 deletions

View File

@ -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 () => {},

View File

@ -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!'
}

View File

@ -15,8 +15,9 @@ export interface ITaskSetupFunction<T = undefined> {
export type TPreOrAfterTaskFunction = () => Task<any>;
export class Task<T = undefined> {
// STATIC
public static extractTask<T = undefined>(preOrAfterTaskArg: Task<T> | TPreOrAfterTaskFunction): Task<T> {
public static extractTask<T = undefined>(
preOrAfterTaskArg: Task<T> | TPreOrAfterTaskFunction
): Task<T> {
switch (true) {
case !preOrAfterTaskArg:
return null;
@ -56,7 +57,7 @@ export class Task<T = undefined> {
}
}
return result;
};
}
public static runTask = async <T>(
taskArg: Task<T> | TPreOrAfterTaskFunction,
@ -65,6 +66,11 @@ export class Task<T = undefined> {
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<T = undefined> {
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<T = undefined> {
return await done.promise;
};
// INSTANCE
public name: string;
public version: string;
public taskFunction: ITaskFunction<T>;
@ -139,12 +152,20 @@ export class Task<T = undefined> {
public preTask: Task<T> | TPreOrAfterTaskFunction;
public afterTask: Task<T> | 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<void>;
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<T>;
public setupValue: T;
@ -162,12 +183,16 @@ export class Task<T = undefined> {
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<any> {
@ -185,16 +210,4 @@ export class Task<T = undefined> {
public triggerBuffered(x?: any): Promise<any> {
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`);
}
}
}