import { DelayedTaskOps, ITask, TaskRegistry } from "../../interfaces"; import { DelayedTask } from "./delayed-task"; export class DelayedTaskRegistry implements TaskRegistry { protected taskRegistry = new Map(); public async register(options: DelayedTaskOps): Promise { const delayedTask = new DelayedTask(options); this.taskRegistry.set(delayedTask.id, delayedTask); return delayedTask; } public async count(): Promise { return this.taskRegistry.size; } public async getTasks(): Promise { return Object.values(this.taskRegistry); } public async getTaskById(id: string): Promise { return this.taskRegistry.get(id); } public async cancelTaskById(id: string): Promise { const task = this.taskRegistry.get(id); if (!task) { throw new Error(`task with id ${id} not exist`); } if (!task.isRunning) { return; } await task.cancel(); this.taskRegistry.delete(id); } public async startTaskById(id: string): Promise { const task = this.taskRegistry.get(id); if (!task) { throw new Error(`task with id ${id} not exist`); } if (task.isRunning) { return; } return task.start(); } }