import { Machine, BaseMachine } from './index'; /** * Creates a factory for building type-safe transitions for a specific machine. * This higher-order function captures the machine's `transitions` object in a closure, * enabling a clean, functional pattern for defining state changes without directly * manipulating the machine's context. * * This pattern promotes: * - Pure functions for state transformations * - Separation of transition logic from machine construction * - Reusable transition factories across similar machines * - Type safety through generic constraints * * @template C The context type of the machine * @template C The context type of the machine * @returns A `createTransition` function that can create transitions for machines with context type C * * @example * ```typescript * // Define your machine's transitions object * const counterTransitions = { * increment: function(amount: number) { * return createMachine({ count: this.context.count + amount }, counterTransitions); * } * }; * * // Create a transition factory * const createCounterTransition = createTransitionFactory<{ count: number }>(); * * // Use the factory to create pure, type-safe transitions * const incrementBy = createCounterTransition( * (ctx, amount: number) => ({ count: ctx.count + amount }) * ); * * const counter = createMachine({ count: 0 }, counterTransitions); * const newCounter = counter.increment(5); // Direct call * // OR * const incremented = incrementBy.call(counter, 5); // Using factory * ``` */ export declare function createTransitionFactory(): (transformer: (ctx: C, ...args: TArgs) => C) => (this: BaseMachine, ...args: TArgs) => Machine; /** * Creates a factory for adding new, type-safe transitions to an existing machine instance. * This enables a functional, compositional approach to building up a machine's capabilities * incrementally, without modifying the original machine. * * This pattern supports: * - Progressive enhancement of machine behavior * - Plugin-like extension of existing machines * - Immutable composition (original machine unchanged) * - Type-safe addition of new transitions * * @template M The machine type being extended * @param machine The machine instance to extend * @returns An `addTransition` function pre-configured for this machine * * @example * ```typescript * // Start with a basic counter machine * const basicCounter = createMachine({ count: 0 }, { * increment: function() { * return createMachine({ count: this.context.count + 1 }, this); * } * }); * * // Create an extender for this machine * const extendCounter = createTransitionExtender(basicCounter); * * // Add new transitions functionally * const extendedCounter = extendCounter('decrement', * (ctx) => ({ count: ctx.count - 1 }) * ).addTransition('reset', * (ctx) => ({ count: 0 }) * ).addTransition('add', * (ctx, amount: number) => ({ count: ctx.count + amount }) * ); * * // The original machine is unchanged * console.log(basicCounter.count); // 0 * * // The extended machine has all transitions * const result = extendedCounter.increment().add(10).decrement(); * console.log(result.count); // 10 * ``` */ export declare function createTransitionExtender>(machine: M): { machine: M; addTransition: (name: TName, transformer: (ctx: M["context"], ...args: TArgs) => M["context"]) => { machine: any; addTransition: (name: TName_1, transformer: (ctx: any, ...args: TArgs_1) => any) => /*elided*/ any; }; }; /** * A mapped type that creates the final transition method signatures based on * an object of pure context transformers. It infers argument types and sets * the correct return type. */ type MachineTransitions C>, C extends object> = { [K in keyof T]: T[K] extends (ctx: C, ...args: infer A) => C ? (this: FunctionalMachine, ...args: A) => FunctionalMachine : never; }; type FunctionalMachine C>> = Machine & MachineTransitions; /** * Creates a complete, type-safe, functional state machine using a curried, two-step * approach that separates the initial data from the transition logic. * * This is a highly declarative and functional pattern for building single-state machines. * * @template C The context type of the machine. * @param initialContext The starting context (data) for the machine. * @returns A new function that takes an object of pure context-transformer * functions and returns a fully-formed machine instance. */ export declare function createFunctionalMachine(initialContext: C): C>>(transformers: T) => FunctionalMachine; /** * A concise dual-form constructor. With two arguments it delegates * to `createMachine`; with one argument it delegates to `createFunctionalMachine`. * * **Two Usage Patterns:** * * 1. **Traditional Pattern** (with transitions object): * ```typescript * const machine = state({ count: 0 }, { * increment() { return createMachine({ count: this.context.count + 1 }, this); } * }); * ``` * * 2. **Functional Pattern** (curried, with transformers): * ```typescript * const createCounter = state({ count: 0 }); * const machine = createCounter({ * increment: ctx => ({ count: ctx.count + 1 }), * add: (ctx, n: number) => ({ count: ctx.count + n }) * }); * ``` * * The overload is selected only by argument count; it does not inspect the model * or choose an implementation based on machine complexity. * * @template C The context type * @template T The transitions/transformers type * @param context The initial context object * @param transitions Optional transitions object (traditional pattern) * @returns Either a machine (traditional) or a factory function (functional) * @example * ```typescript * // Traditional pattern * const counter1 = state({ count: 0 }, { * increment() { return createMachine({ count: this.context.count + 1 }, this); }, * decrement() { return createMachine({ count: this.context.count - 1 }, this); } * }); * * // Functional pattern * const createCounter = state({ count: 0 }); * const counter2 = createCounter({ * increment: ctx => ({ count: ctx.count + 1 }), * decrement: ctx => ({ count: ctx.count - 1 }), * reset: ctx => ({ count: 0 }) * }); * ``` */ export declare function state(context: C): ReturnType>; /** * Creates a machine immediately from context and traditional transition methods. * * @typeParam C - Context shape. * @typeParam T - Transition record bound to the full machine as `this`. * @param context - Initial context. * @param transitions - Machine-returning transition methods. */ export declare function state, ...args: any[]) => any>>(context: C, transitions: T): Machine; export {}; //# sourceMappingURL=functional-combinators.d.ts.map