import { type PrimativeKey, type ReadKeyFunction } from '../key'; import { type ArrayOrValue } from '../array/array'; import { type PromiseOrValue } from '../promise/promise.type'; /** * Special key used to register a catch-all handler that matches any unhandled key. */ export declare const CATCH_ALL_HANDLE_RESULT_KEY = "__CATCH_ALL_HANDLE_RESULT_KEY__"; /** * Whether or not the input value was handled. */ export type HandleResult = boolean; /** * An internal type for the result of a handler function. * * If void is returned, assumes true. */ export type InternalHandlerFunctionHandleResult = HandleResult | void; /** * The type of the catch-all handler key constant. */ export type HandlerCatchAllKey = typeof CATCH_ALL_HANDLE_RESULT_KEY; /** * A handler key that is either a specific key type or the catch-all key. */ export type HandlerKey = K | HandlerCatchAllKey; /** * Used to perform a task on the input value. * * If the value is not used/"handled", returns false. */ export type HandlerFunction = (value: T) => Promise; /** * HandleFunction, but used only by Handler that can return undefined. */ export type InternalHandlerFunction = (value: T) => PromiseOrValue; /** * Provides methods to register handler functions for specific keys. */ export interface HandlerSetAccessor { /** * Adds a new handler function to the current handler. * * @param key * @param handle */ set(key: ArrayOrValue>, handle: InternalHandlerFunction): void; /** * Sets the catch-all handler function to the current handler. * * @param handle */ setCatchAll(handle: InternalHandlerFunction): void; } /** * Extends {@link HandlerSetAccessor} with a key reader and a bind-aware set method. */ export interface HandlerAccessor extends HandlerSetAccessor { /** * Used to read a handler key from the input value. */ readonly readKey: ReadKeyFunction; /** * Convenience function for binding a function. Useful for use within classes that pass their function and still need to be bound for when the function runs. * * @param bindTo * @param key * @param handle */ bindSet(bindTo: unknown, key: ArrayOrValue>, handle: InternalHandlerFunction): void; } /** * A callable handler function combined with its accessor interface for registering sub-handlers. */ export type Handler = HandlerFunction & HandlerAccessor; /** * Factory that creates new {@link Handler} instances. */ export type HandlerFactory = () => Handler; /** * Options for configuring default and negative results in a {@link HandlerFactory}. */ export interface HandlerFactoryOptions { /** * The result returned when a handler function returns void. */ readonly defaultResult: R; /** * The result returned when no handler matches the input key. */ readonly negativeResult: R; } /** * Creates a {@link HandlerFactory} that produces key-based dispatch handlers. * Each handler routes incoming values to registered handler functions based on a key extracted from the value. * * @param readKey - Function to extract the dispatch key from an input value. * @param options - Optional configuration for default and negative result values. * @returns A factory that creates new Handler instances. */ export declare function handlerFactory(readKey: ReadKeyFunction): HandlerFactory; export declare function handlerFactory(readKey: ReadKeyFunction, options: HandlerFactoryOptions): HandlerFactory; /** * Convenience function that creates a new {@link Handler} from the given key reader using default options. * * @param readKey - Function to extract the dispatch key from an input value. * @returns A new Handler instance. */ export declare function makeHandler(readKey: ReadKeyFunction): Handler; /** * Returns the {@link CATCH_ALL_HANDLE_RESULT_KEY} constant, useful for registering a catch-all handler. * * @returns The catch-all handler key. */ export declare function catchAllHandlerKey(): typeof CATCH_ALL_HANDLE_RESULT_KEY;