import type { AnyArray, IndexesOfArray, NotUndefined, PrimitiveObject, Require } from '../libs/bemedev/globals/types'; import type { EventObject } from '../events/index.js'; import type { Observable } from 'rxjs'; import type { Action, ActionConfig, FromActionConfig } from '../actions/index.js'; import type { ActorConfig, ChildConfig, EmitterConfig } from '../actor.types'; import type { FromGuard, GuardConfig, Predicate } from '../guards/index.js'; import type { Emitter } from '../emitters/index.js'; import type { Child } from '../machine/index.js'; import type { Identify, RecordS, ReduceArray, SingleOrArrayL } from '../types/index.js'; /** * Represents the simpliest configuration map for a transition. * Used as Helper */ type _TransitionConfigMap = { readonly target?: Paths; readonly actions?: SingleOrArrayL; readonly guards?: SingleOrArrayL; readonly description?: string; }; /** * Extracts actions from a transition configuration. * * @template T - The transition configuration type. * @returns The actions extracted from the transition configuration. * * @see {@linkcode ActionConfig} for the structure of action configurations. * @see {@linkcode FromActionConfig} for converting action configurations to actions. * @see {@linkcode ReduceArray} for reducing arrays to their elements. * @see {@linkcode SingleOrArrayL} for handling single or array */ export type ExtractActionsFromTransition; }> = ReduceArray extends infer R extends ActionConfig ? FromActionConfig : never; /** * Extracts guards from a transition configuration. * * @template T - The transition configuration type. * @returns The guards extracted from the transition configuration. * * @see {@linkcode GuardConfig} for the structure of guard configurations. * @see {@linkcode FromGuard} for converting guard configurations to predicates. * @see {@linkcode ReduceArray} for reducing arrays to their elements. * @see {@linkcode SingleOrArrayL} for handling single or array */ export type ExtractGuardKeysFromTransition; }> = ReduceArray extends infer R extends GuardConfig ? FromGuard : never; /** * A {@linkcode _TransitionConfigMap} that requires actions. */ export type TransitionConfigMapA = Require<_TransitionConfigMap, 'actions'>; export type TransitionConfigA = Require, 'actions'> | Paths; export type TransitionConfigMapF = Require<_TransitionConfigMap, 'target'>; export type TransitionConfigF = Require, 'target'> | Paths; export type TransitionConfigMap = TransitionConfigMapF | TransitionConfigMapA; /** * A better version {@linkcode _TransitionConfigMap}. * * This type is used to ensure that the transition configuration * has either a target or actions defined, but not both. * * @see {@linkcode TransitionConfigMapF} for a version that requires a target. * @see {@linkcode TransitionConfigMapA} for a version that requires actions. */ export type TransitionConfig = Paths | TransitionConfigMap; /** * A version {@linkcode TransitionConfig} with string declaration. */ /** * An array of transitions that can be used in a state machine. * * More difficult than a simple array of {@linkcode TransitionConfig} * * @see {@linkcode TransitionConfigMapF} for a version that requires a target. * @see {@linkcode TransitionConfigMapA} for a version that requires actions. * @see {@linkcode TransitionConfig} for a version that can be a string or a {@linkcode TransitionConfig}. * @see {@linkcode Require} */ export type ArrayTransitions = readonly [ ...Require, 'guards'>[], TransitionConfig ]; /** * A type that can be either an array of transitions or a single transition configuration. * * @see {@linkcode ArrayTransitions} for an array of transitions. * @see {@linkcode TransitionConfig} for a single transition configuration. */ export type SingleOrArrayT = ArrayTransitions | TransitionConfig; /** * Representation of a always transition config. * * @see {@linkcode ArrayTransitions} for an array of transitions. * @see {@linkcode TransitionConfigF} for a single transition configuration with a target. * @see {@linkcode Require} */ export type AlwaysConfig = readonly [ ...Require, 'guards'>[], TransitionConfigF ] | TransitionConfigF; /** * A type used to represent a record of transitions. * * @remarks For the purpose of delay transition config. */ export type DelayedTransitions = RecordS>; export type GetEventKeysFromDelayed = { [key in keyof T & string]: T[key] extends AnyArray ? `${key}.[${IndexesOfArray}]` : key; }[keyof T & string]; /** * Extracts action keys from a {@linkcode DelayedTransitions}. * * @template T - The delayed transitions type. * * @see {@linkcode ExtractActionsFromTransition} for extracting actions from a transition configuration. * @see {@linkcode ReduceArray} for reducing arrays to their elements. * @see {@linkcode SingleOrArrayL} for handling single or array * @see {@linkcode ActionConfig} for the structure of action configurations. * * @see {@linkcode ExtractGuardKeysFromTransition} for extracting guards from a transition configuration. */ export type ExtractActionKeysFromDelayed = ExtractActionsFromTransition, { actions: SingleOrArrayL; }>>; /** * Extracts guards from a {@linkcode DelayedTransitions}. * * @template T - The delayed transitions type. * * @see {@linkcode ExtractGuardKeysFromTransition} for extracting guards from a transition configuration. * @see {@linkcode ReduceArray} for reducing arrays to their elements. * @see {@linkcode SingleOrArrayL} for handling single or array * @see {@linkcode GuardConfig} for the structure of guard configurations. * * @see {@linkcode ExtractActionsFromTransition} for extracting actions from a transition configuration. */ export type ExtractGuardKeysFromDelayed = ExtractGuardKeysFromTransition, { guards: SingleOrArrayL; }>>; /** * Represents a JSON configuration for delayed transitions. * * @remarks This type is used to define transitions that occur after a delay. * It can include actions, guards, and promises. * * @see {@linkcode DelayedTransitions} for the structure of delayed transitions. * @see {@linkcode AlwaysConfig} for always transitions configuration. * @see {@linkcode PromiseeConfig} for promise configurations. * @see {@linkcode SingleOrArrayL} for handling single or array */ export type TransitionsConfig = { readonly on?: DelayedTransitions; readonly always?: AlwaysConfig; readonly after?: DelayedTransitions; readonly actors?: RecordS>; }; export type GetEventKeysFromEmitter = GetEventKeysFromDelayed>; export type GetEventKeysFromMachineConfig = `on.${GetEventKeysFromDelayed}`; export type GetEventKeysFromActor = T extends EmitterConfig ? GetEventKeysFromEmitter : T extends ChildConfig ? GetEventKeysFromMachineConfig : never; export type GetEventKeysFromTransitions = ('on' extends keyof T ? `on.${GetEventKeysFromDelayed>}` : never) | ('after' extends keyof T ? `after.${GetEventKeysFromDelayed>}` : never) | ('always' extends keyof T ? T['always'] extends infer TA extends AnyArray ? `always.[${IndexesOfArray}]` : 'always' : never) | ('actors' extends keyof T ? `${NotUndefined extends infer TP ? `actors.${{ [key in keyof TP & string]: `${key}.${GetEventKeysFromActor}`; }[keyof TP & string]}` : never}` : never); /** * Extracts delay keys from a {@linkcode TransitionsConfig}. * * @template : {@linkcode TransitionsConfig} [T] - The transitions configuration type. * @returns The keys of the delays extracted from the transitions configuration. * * @see {@linkcode ExtractMaxFromPromisee} for extracting the maximum delay from promises. * @see {@linkcode ReduceArray} for reducing arrays to their elements. * @see {@linkcode Extract} for extracting specific types from a union. * @see {@linkcode NotUndefined} for ensuring the type is not undefined. */ export type ExtractDelayKeysFromTransitions = T['after'] extends undefined ? never : keyof T['after']; type _ExtractActionsFromMap = ExtractActionsFromTransition>, { actions: SingleOrArrayL; }>>; /** * Extracts actions from the finally part of a completion configuration. */ type _ExtractActionsFromFinally = ReduceArray extends infer Tr ? Tr extends ActionConfig ? FromActionConfig : _ExtractActionsFromMap : never; export type ExtractActionKeysFromEmitter = _ExtractActionsFromMap | _ExtractActionsFromMap | _ExtractActionsFromFinally>; export type ExtractActionKeysFromChild = ExtractActionKeysFromDelayed; export type ExtractActionKeysFromActor = T extends EmitterConfig ? ExtractActionKeysFromEmitter : T extends ChildConfig ? ExtractActionKeysFromChild : never; /** * Extracts actions keys from a {@linkcode TransitionsConfig}. * * @template : {@linkcode TransitionsConfig} [T] - The transitions configuration type. * @returns The actions keys extracted from the transitions configuration. * * @see {@linkcode ExtractActionsFromTransition} for extracting actions from a transition configuration. * @see {@linkcode ExtractActionsFromDelayed} for extracting actions from delayed transitions. * @see {@linkcode ExtractActionKeysFromPromisee} for extracting actions from promises. * @see {@linkcode ReduceArray} for reducing arrays to their elements. * @see {@linkcode SingleOrArrayL} for handling single or array * @see {@linkcode ActionConfig} for the structure of action configurations. * @see {@linkcode NotUndefined} for ensuring the type is not undefined. * @see {@linkcode Extract} */ export type ExtractActionKeysFromTransitions = ExtractActionKeysFromDelayed | ExtractActionKeysFromDelayed | ExtractActionsFromTransition, { actions: SingleOrArrayL; }>> | (NotUndefined extends infer Ta ? { [K in keyof Ta]: ExtractActionKeysFromActor; }[keyof Ta] : never); type _ExtractGuardKeysFromMap = ExtractGuardKeysFromTransition>, { guards: SingleOrArrayL; }>>; export type ExtractGuardKeysFromEmitter = _ExtractGuardKeysFromMap | _ExtractGuardKeysFromMap | ExtractGuardKeysFromDelayed; export type ExtractGuardsKeysFromChild = ExtractGuardKeysFromDelayed; export type ExtractGuardsKeysFromActor = T extends EmitterConfig ? ExtractGuardKeysFromEmitter : T extends ChildConfig ? ExtractGuardsKeysFromChild : never; /** * Extracts guard keys from a {@linkcode TransitionsConfig}. * * @template : {@linkcode TransitionsConfig} [T] - The transitions configuration type. * @returns The guard keys extracted from the transitions configuration. * * @see {@linkcode ExtractGuardKeysFromTransition} for extracting guards from a transition configuration. * @see {@linkcode ExtractGuardKeysFromDelayed} for extracting guards from delayed transitions. * @see {@linkcode ExtractGuardKeysFromPromisee} for extracting guards from promises. * @see {@linkcode ReduceArray} for reducing arrays to their elements. * @see {@linkcode SingleOrArrayL} for handling single or array * @see {@linkcode GuardConfig} for the structure of guard configurations. * @see {@linkcode NotUndefined} for ensuring the type is not undefined. * @see {@linkcode Extract} */ export type ExtractGuardKeysFromTransitions = ExtractGuardKeysFromDelayed | ExtractGuardKeysFromDelayed | ExtractGuardKeysFromTransition, { guards: SingleOrArrayL; }>> | (NotUndefined extends infer Ta ? { [K in keyof Ta]: ExtractGuardsKeysFromActor; }[keyof Ta] : never); export type ExtractSrcKeyFromTransitions = NotUndefined> = { [K in keyof A]: A[K] extends Filter ? K : never; }[keyof A]; export type ExtractEmitterSrcKeyFromTransitions = ExtractSrcKeyFromTransitions; export type ExtractChildKeysFromActors> = { [key in keyof T]: T[key] extends infer Tk extends ChildConfig ? { src: key; contexts: NotUndefined; on: keyof NotUndefined; } : never; }[keyof T]; export type ExtractChildKeysFromTransitions = ExtractChildKeysFromActors>; /** * Represents a transition in a state machine with full defined functions. * * @template : {@linkcode EventsMap} [E] - The events map used in the transition. * @template : {@linkcode PromiseeMap} [P] - The promisees map used in the transition. * @template : [Pc] - The private context type for the transition. * @template : {@linkcode types} [Tc] - The context for the transition. * * @see {@linkcode Action} for the structure of actions in the transition. * @see {@linkcode Predicate} for the structure of guards in the transition. */ export type Transition = { readonly target?: string; readonly actions: Action[]; readonly guards: Predicate[]; readonly description?: string; }; export type Emiter4 = { src: Observable; description?: string; then: Transition[]; catch: Transition[]; finally: Transition[]; }; /** * Represents all transitions inside a state config with full defined functions. * * @template : {@linkcode EventsMap} [E] - The events map used in the transitions. * @template : [Pc] - The private context type for the transitions. * @template : {@linkcode PrimitiveObject} [Tc] - The context for the transitions * * @see {@linkcode Transition} for the structure of a single transition. * @see {@linkcode Identify} for identifying properties in the transitions. */ export type Transitions = { on: Identify>[]; always: Transition[]; after: Identify>[]; emitters: Emitter[]; children: Child[]; }; export {}; //# sourceMappingURL=types.d.ts.map