import { InjectStoreConfig } from '@zedux/atoms'; import { MachineStore } from './MachineStore'; import { MachineHook, MachineStateShape } from './types'; type ArrToUnion = S extends [infer K, ...infer Rest] ? Rest extends string[] ? K | ArrToUnion : never : never; export type InjectMachineStoreParams | undefined = undefined> = [ statesFactory: (state: (stateName: Name) => MachineState) => [...States], initialContext?: Context, config?: { guard?: (currentState: MachineStateShape, Context>, nextValue: MapStatesToStateNames) => boolean; onTransition?: MachineHook, MapStatesToEvents, Context>; } & InjectStoreConfig ]; export interface MachineState | undefined = any, Name extends string = string, Events extends string[] = [], ChildStates extends string[] = [Name]> { on: (eventName: E, nextState: S, guard?: (context: Context) => boolean) => MachineState; onEnter: (listener: MachineHook, ArrToUnion, Context>) => MachineState; onLeave: (listener: MachineHook, ArrToUnion, Context>) => MachineState; stateName: Name; } type MapStatesToStateNames | undefined = undefined> = States extends [infer K, ...infer Rest] ? K extends MachineState ? Rest extends MachineState[] ? StateNameType | ArrToUnion> | MapStatesToStateNames : never : never : never; type MapStatesToEvents | undefined = undefined> = States extends [infer K, ...infer Rest] ? K extends MachineState ? Rest extends MachineState[] ? ArrToUnion> | MapStatesToEvents : ArrToUnion> : never : never; type StateChildStatesType = S extends MachineState ? ChildStates : never; type StateEventsType = S extends MachineState ? Events : never; type StateNameType = S extends MachineState ? Name : never; /** * Create a MachineStore. Pass a statesFactory * * The first state in the state list returned from your statesFactory will * become the initial state (`.value`) of the store. * * Registers an effect that listens to all store changes and calls the * configured listeners appropriately. * * ```ts * const store = injectMachineStore(state => [ * state('a') * .on('next', 'b', localGuard) * .onEnter(enterListener) * .onLeave(leaveListener), * state('b').on('next', 'a') * ], initialContext, { guard, onTransition }) * ``` * * Set a universal transition guard via the 3rd `config` object param. This * guard will be called every time a valid transition is about to occur. It will * be called with the current `.context` value and should return a boolean. * Return true to allow the transition, or any falsy value to deny it. * * Set a universal `onTransition` listener via the 3rd `config` object param. * This listener will be called every time the machine transitions to a new * state (after the state is updated). It will be called with 2 params: The * current MachineStore and the storeEffect of the action that transitioned the * store. For example, use `storeEffect.oldState.value` to see what state the * machine just transitioned from. * * @param statesFactory Required. A function. Use the received state factory to * create a list of states for the machine and specify their transitions, * guards, and listeners. * @param initialContext Optional. An object or undefined. Will be set as the * initial `.context` value of the machine store's state. * @param config Optional. An object with 2 additional properties: `guard` and * `onTransition`. */ export declare const injectMachineStore: | undefined = undefined>(...[statesFactory, initialContext, config]: InjectMachineStoreParams) => MachineStore, MapStatesToEvents, Context>; export {};