export type Task = { status: string; data: any; error: any; updatedAt: string; }; /** * @typedef {object} Task * @property {string} status * @property {*} data * @property {*} error * @property {string} updatedAt */ export class StateMachine extends EventEmitter<[never]> { /** * @param {{ file?: string|null }} [options={}] * @when you need to track task lifecycle (pending/running/done/failed/waiting/cancelled) with enforced transitions and optional file persistence * @fails rejects an illegal state transition and never throws on a valid one; state changes are emitted as events. * @example * const sm = new StateMachine(); * sm.create('t1'); sm.transition('t1', 'running'); */ constructor(options?: { file?: string | null; }); file: string | null; /** @type {Map} */ tasks: Map; /** * Transition a task to a new state. * @param {string} taskId - Task identifier. * @param {string} event - Transition event (start, complete, fail, pause, resume, cancel, retry). * @param {*} [data] - Optional data to attach to the task. * @returns {string} The new status. * @throws {Error} `[StateMachine] Invalid transition` — when the event is not valid for the current state. */ transition(taskId: string, event: string, data?: any): string; /** @param {string} taskId */ getStatus(taskId: string): Task | null; /** @param {(event: { taskId: string, from: string, to: string, event: string, data: * }) => void} callback */ onTransition(callback: (event: { taskId: string; from: string; to: string; event: string; data: any; }) => void): () => this; getAll(): Record; _load(): void; _save(): void; } import { EventEmitter } from "events";