import { default as EventEmitter } from 'eventemitter3'; import { RefObject } from 'react'; import { Model, HandlerContext, Actions, Props, Tasks, ActionId, Phase, Filter } from '../types/index.js'; import { BroadcastEmitter } from '../boundary/components/broadcast/utils.js'; import { ScopeContext } from '../boundary/components/scope/types.js'; /** * Function signature for action handlers registered via `useAction`. * Receives the reactive context and payload, returning void or a promise/generator. * * @template M - The model type * @template A - The actions class type * @template D - The data props type */ export type Handler = (context: HandlerContext, payload: unknown) => void | Promise | AsyncGenerator | Generator; /** * Entry for an action handler with a reactive channel getter. * When getChannel returns undefined, the handler fires for all dispatches. * When getChannel returns a channel, dispatches must match. */ export type HandlerEntry = { handler: Handler; getChannel: () => Filter | undefined; }; /** * Internal scope for tracking registered action handlers. * Maps action IDs to sets of handler entries (with optional channels). * * @template M - The model type * @template A - The actions class type * @template D - The data props type */ export type Scope = { /** All handlers for each action, with optional channels */ handlers: Map>>; }; /** * Function type for the data snapshot passed to useActions. * Returns the current reactive values to be captured in the context. * * @template D - The data props type */ export type Data = () => D; /** * Return type for useDispatchers hook. */ export type Dispatchers = { /** Set of registered broadcast action IDs */ broadcast: Set; /** Set of registered multicast action IDs */ multicast: Set; }; /** * Configuration for {@link useLifecycles}. */ export type LifecycleConfig = { /** Component-local event emitter for unicast action dispatch */ unicast: EventEmitter; /** Shared broadcast emitter with cached values for cross-component events */ broadcast: BroadcastEmitter; /** Global set of all in-flight tasks across components */ tasks: Tasks; /** Tracked broadcast and multicast action sets for cached replay on mount */ dispatchers: Dispatchers; /** Scope context for multicast cached replay (null when outside any scope) */ scope: ScopeContext; /** Mutable ref tracking the component's current lifecycle phase */ phase: RefObject; /** Current snapshot of reactive data props for change detection */ data: Props; /** Handler registry for lifecycle action discovery */ handlers: Map>; };