feat(Task): Tasks can now be blocked by other tasks.
This commit is contained in:
parent
94e327c722
commit
05f91c3e35
@ -12,13 +12,6 @@ tap.test('new Task() should return a new task', async () => {
|
|||||||
expect(testTask).toBeInstanceOf(taskbuffer.Task);
|
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 () => {
|
tap.test('should have bufferMax set to the provided value', async () => {
|
||||||
const task2 = new taskbuffer.Task({
|
const task2 = new taskbuffer.Task({
|
||||||
taskFunction: async () => {},
|
taskFunction: async () => {},
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/taskbuffer',
|
name: '@push.rocks/taskbuffer',
|
||||||
version: '3.0.15',
|
version: '3.1.0',
|
||||||
description: 'flexible task management. TypeScript ready!'
|
description: 'flexible task management. TypeScript ready!'
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,9 @@ export interface ITaskSetupFunction<T = undefined> {
|
|||||||
export type TPreOrAfterTaskFunction = () => Task<any>;
|
export type TPreOrAfterTaskFunction = () => Task<any>;
|
||||||
|
|
||||||
export class Task<T = undefined> {
|
export class Task<T = undefined> {
|
||||||
// STATIC
|
public static extractTask<T = undefined>(
|
||||||
public static extractTask<T = undefined>(preOrAfterTaskArg: Task<T> | TPreOrAfterTaskFunction): Task<T> {
|
preOrAfterTaskArg: Task<T> | TPreOrAfterTaskFunction
|
||||||
|
): Task<T> {
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case !preOrAfterTaskArg:
|
case !preOrAfterTaskArg:
|
||||||
return null;
|
return null;
|
||||||
@ -56,7 +57,7 @@ export class Task<T = undefined> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
};
|
}
|
||||||
|
|
||||||
public static runTask = async <T>(
|
public static runTask = async <T>(
|
||||||
taskArg: Task<T> | TPreOrAfterTaskFunction,
|
taskArg: Task<T> | TPreOrAfterTaskFunction,
|
||||||
@ -65,6 +66,11 @@ export class Task<T = undefined> {
|
|||||||
const taskToRun = Task.extractTask(taskArg);
|
const taskToRun = Task.extractTask(taskArg);
|
||||||
const done = plugins.smartpromise.defer();
|
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) {
|
if (!taskToRun.setupValue && taskToRun.taskSetup) {
|
||||||
taskToRun.setupValue = await taskToRun.taskSetup();
|
taskToRun.setupValue = await taskToRun.taskSetup();
|
||||||
}
|
}
|
||||||
@ -77,6 +83,14 @@ export class Task<T = undefined> {
|
|||||||
|
|
||||||
done.promise.then(async () => {
|
done.promise.then(async () => {
|
||||||
taskToRun.running = false;
|
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 = {
|
const options = {
|
||||||
@ -125,7 +139,6 @@ export class Task<T = undefined> {
|
|||||||
return await done.promise;
|
return await done.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
// INSTANCE
|
|
||||||
public name: string;
|
public name: string;
|
||||||
public version: string;
|
public version: string;
|
||||||
public taskFunction: ITaskFunction<T>;
|
public taskFunction: ITaskFunction<T>;
|
||||||
@ -139,12 +152,20 @@ export class Task<T = undefined> {
|
|||||||
public preTask: Task<T> | TPreOrAfterTaskFunction;
|
public preTask: Task<T> | TPreOrAfterTaskFunction;
|
||||||
public afterTask: 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 running: boolean = false;
|
||||||
public bufferRunner = new BufferRunner(this);
|
public bufferRunner = new BufferRunner(this);
|
||||||
public cycleCounter = new CycleCounter(this);
|
public cycleCounter = new CycleCounter(this);
|
||||||
|
|
||||||
public idle: boolean = true;
|
public get idle() {
|
||||||
private _state: string = 'ready';
|
return !this.running;
|
||||||
|
}
|
||||||
|
|
||||||
public taskSetup: ITaskSetupFunction<T>;
|
public taskSetup: ITaskSetupFunction<T>;
|
||||||
public setupValue: T;
|
public setupValue: T;
|
||||||
@ -162,12 +183,16 @@ export class Task<T = undefined> {
|
|||||||
this.taskFunction = optionsArg.taskFunction;
|
this.taskFunction = optionsArg.taskFunction;
|
||||||
this.preTask = optionsArg.preTask;
|
this.preTask = optionsArg.preTask;
|
||||||
this.afterTask = optionsArg.afterTask;
|
this.afterTask = optionsArg.afterTask;
|
||||||
this.idle = !this.running;
|
|
||||||
this.buffered = optionsArg.buffered;
|
this.buffered = optionsArg.buffered;
|
||||||
this.bufferMax = optionsArg.bufferMax;
|
this.bufferMax = optionsArg.bufferMax;
|
||||||
this.execDelay = optionsArg.execDelay;
|
this.execDelay = optionsArg.execDelay;
|
||||||
this.name = optionsArg.name;
|
this.name = optionsArg.name;
|
||||||
this.taskSetup = optionsArg.taskSetup;
|
this.taskSetup = optionsArg.taskSetup;
|
||||||
|
|
||||||
|
// Create the finished promise
|
||||||
|
this.finished = new Promise((resolve) => {
|
||||||
|
this.resolveFinished = resolve;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public trigger(x?: any): Promise<any> {
|
public trigger(x?: any): Promise<any> {
|
||||||
@ -185,16 +210,4 @@ export class Task<T = undefined> {
|
|||||||
public triggerBuffered(x?: any): Promise<any> {
|
public triggerBuffered(x?: any): Promise<any> {
|
||||||
return this.bufferRunner.trigger(x);
|
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`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user