import { TaskEntityContext } from "./task-entity-context"; import { TaskEntityOperation } from "./task-entity-operation"; /** * The task entity contract. * * @remarks * This is the core interface that all entities must implement. * The state of an entity can be retrieved and updated via the operation's state property. */ export interface ITaskEntity { /** * Runs an operation for this entity. * * @param operation - The operation to run. * @returns The response to the caller, if any. Can be a Promise for async operations. */ run(operation: TaskEntityOperation): unknown | Promise; } /** * Type for entity factory functions that create entity instances. */ export type EntityFactory = () => T; /** * An ITaskEntity which dispatches its operations to methods on the class. * * @typeParam TState - The state type held by this entity. * * @remarks * **Method Binding** * * When using this base class, all public methods will be considered valid entity operations. * - Operation matching is case insensitive. * - Error is thrown if no matching method is found for an operation. * * **Entity State** * * Entity state will be hydrated into the `state` property. The state is initialized * via `initializeState()` when there is no current state. * * **Implicit Operations** * * This class supports the `delete` operation implicitly. When `delete` is called and no * explicit delete method exists, the entity state is set to null (deleted). * To override this behavior, implement a `delete()` method on your entity. */ export declare abstract class TaskEntity implements ITaskEntity { /** * Gets or sets the state for this entity. * * @remarks * This will be hydrated as part of `run()`. `initializeState()` will be called * when state is null/undefined at the start of an operation. * * Setting to null or undefined will delete the entity state. */ protected state: TState; /** * Gets the entity context. */ protected get context(): TaskEntityContext | undefined; /** * The current context. Set during run(). */ private _context; /** * Runs an operation for this entity. * * @param operation - The operation to run. * @returns The response to the caller, if any. */ run(operation: TaskEntityOperation): Promise; /** * Initializes the entity state. This is only called when there is no current state. * * @returns The initial entity state. * * @remarks * The default implementation returns an empty object (`{}`) cast to TState. * This is appropriate when TState is an object or record type, but will produce * incorrect values for primitive types (`number`, `string`, `boolean`) or arrays. * For non-object state types, override this method to return a proper default * (e.g., `0` for number, `[]` for arrays). */ protected initializeState(): TState; /** * Dispatches the operation to the appropriate method on this class. * * @param operation - The operation to dispatch. * @returns The result of the method invocation. */ private dispatch; /** * Finds a method on this class that matches the operation name (case-insensitive). * * @param operationName - The operation name (already lowercased). * @returns The actual method name if found, undefined otherwise. */ private findMethod; /** * Tries to dispatch implicit operations. * * @param operation - The operation to dispatch. * @returns True if an implicit operation was handled, false otherwise. */ private tryDispatchImplicit; }