import { createMemo, createSignal, type Accessor, untrack } from "solid-js"; import { INTERNAL_OPTIONS } from "@solid-primitives/utils"; /** * Used for restricting the user type input in {@link createMachine} generic. */ export type StatesBase = { [K in TStateNames]: { /** * Value to be passed to the state callback when the machine enters that state. */ readonly input?: any; /** * Value returned from the state callback. */ readonly value?: any; /** * Array of state names that can be transitioned to from this state. */ readonly to?: TStateNames; }; }; /** * Extracts the input type from a state definition. */ export type StateInput = T extends { input: infer Input } ? Input : void; /** * Extracts the value type from a state definition. */ export type StateValue = T extends { value: infer Value } ? Value : undefined; /** * States object with function implementations. * @example * ```ts * const states: MachineStates<{ * idle: { value: "foo"; to: ["loading"] }; * loading: { value: "bar"; to: ["idle"] }; * }> = { * idle: () => "foo", * loading(input, next) { * next("idle"); * return "bar"; * } * } * ``` */ export type MachineStates> = { [K in keyof T]: (input: StateInput, next: MachineNext) => StateValue; }; type MachineInitialDiscriminator> = { [K in keyof T]: { type: K; input: StateInput } | (T[K] extends { input: any } ? never : K); }; /** * Union of all possible initial states. */ export type MachineInitial> = MachineInitialDiscriminator[keyof T]; /** * Options object for {@link createMachine}. */ export type MachineOptions> = { states: MachineStates; initial: MachineInitial; }; type MachineStateDiscriminator> = { [K in keyof T]: { readonly type: K; readonly value: StateValue; readonly to: MachineNext; }; }; /** * Type of the state object returned by {@link createMachine}. * * Use with a specific state name to get the state object for that state. * @example * ```ts * type IdleState = MachineState * ``` * * Use with `keyof States` to get the union of all state objects. * @example * ```ts * type AllStates = MachineState * ``` */ export type MachineState< T extends StatesBase, K extends keyof T, > = MachineStateDiscriminator[K]; type PossibleNextKeys, TKey extends keyof T> = Exclude< Extract, TKey | symbol >; /** * Type of the `next` function passed to state functions. Or the `to` property of the state object. * * Use with a specific state name to get the `next` function for that state. * @example * ```ts * type IdleNext = MachineNext * ``` * * Use with `keyof States` to get the union of all `next` functions. * @example * ```ts * type AllNext = MachineNext * ``` */ export type MachineNext, TKey extends keyof T> = { readonly [K in PossibleNextKeys]: (input: StateInput) => void; } & (>( ...args: T[K] extends { input: infer Input } ? [to: K, input: Input] : [to: K, input?: undefined] ) => void); const STATE_OPTIONS = { ...INTERNAL_OPTIONS, equals: (a: any, b: any) => a.type === b.type }; /** * Creates a reactive state machine. * * @param options {@link MachineOptions} object. * * @returns A signal with the current state object. * * @example * ```ts * const state = createMachine<{ * idle: { value: "foo"; to: "loading" }; * loading: { input: number; value: "bar"; to: "idle" }; * }>({ * initial: "idle", * states: { * idle(input, to) { * return "foo"; * }, * loading(input, to) { * setTimeout(() => to("idle"), input); * return "bar"; * } * } * }) * * state.type // "idle" * state.value // "foo" * * if (state.type === "idle") { * state.to.loading(1000) * * state.type // "loading" * state.value // "bar" * } * ``` */ export function createMachine>( options: MachineOptions, ): Accessor> & MachineState { const { states, initial } = options, to: any = (type: keyof T, value: any) => { setPayload({ type, value, to }); }, [payload, setPayload] = createSignal( typeof initial === "object" ? { type: initial.type, value: initial.input as any, to } : { type: initial as keyof T, value: undefined, to }, STATE_OPTIONS, ); for (const key of Object.keys(states)) { to[key as any] = (input: any) => to(key, input); } const memo = createMemo(() => { let type = payload().type as keyof T; let value: any = payload().value; let pendingType: keyof T | undefined; let pendingValue: any; const next: any = (nextType: keyof T, nextValue?: any) => { if (nextType !== type) { pendingType = nextType; pendingValue = nextValue; } }; for (const key of Object.keys(states)) { next[key as any] = (input: any) => next(key, input); } // oxlint-disable-next-line @typescript-eslint/no-unnecessary-condition while (true) { pendingType = undefined; value = untrack(() => (states as any)[type](value, next)); if (pendingType === undefined) break; // oxlint-disable-line @typescript-eslint/no-unnecessary-condition type = pendingType; value = pendingValue; } return { type, value, to }; }) as any; Object.defineProperties(memo, { type: { get: () => memo().type }, value: { get: () => memo().value }, to: { value: to }, }); return memo; }