StateNode.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { Event, StateValue, StateTransition, MachineOptions, EventObject, HistoryValue, StateNodeDefinition, TransitionDefinition, DelayedTransitionDefinition, ActivityDefinition, StateNodeConfig, StateSchema, StateNodesConfig, InvokeDefinition, ActionObject, Mapper, PropertyMapper, SCXML, Typestate, TransitionDefinitionMap, MachineSchema, InternalMachineOptions, ServiceMap, StateConfig, PredictableActionArgumentsExec } from './types';
  2. import { State } from './State';
  3. import { TypegenDisabled } from './typegenTypes';
  4. declare class StateNode<TContext = any, TStateSchema extends StateSchema = any, TEvent extends EventObject = EventObject, TTypestate extends Typestate<TContext> = {
  5. value: any;
  6. context: TContext;
  7. }, TServiceMap extends ServiceMap = ServiceMap, TResolvedTypesMeta = TypegenDisabled> {
  8. /**
  9. * The raw config used to create the machine.
  10. */
  11. config: StateNodeConfig<TContext, TStateSchema, TEvent>;
  12. /**
  13. * The initial extended state
  14. */
  15. private _context;
  16. /**
  17. * The relative key of the state node, which represents its location in the overall state value.
  18. */
  19. key: string;
  20. /**
  21. * The unique ID of the state node.
  22. */
  23. id: string;
  24. /**
  25. * The machine's own version.
  26. */
  27. version?: string;
  28. /**
  29. * The type of this state node:
  30. *
  31. * - `'atomic'` - no child state nodes
  32. * - `'compound'` - nested child state nodes (XOR)
  33. * - `'parallel'` - orthogonal nested child state nodes (AND)
  34. * - `'history'` - history state node
  35. * - `'final'` - final state node
  36. */
  37. type: 'atomic' | 'compound' | 'parallel' | 'final' | 'history';
  38. /**
  39. * The string path from the root machine node to this node.
  40. */
  41. path: string[];
  42. /**
  43. * The initial state node key.
  44. */
  45. initial?: keyof TStateSchema['states'];
  46. /**
  47. * (DEPRECATED) Whether the state node is a parallel state node.
  48. *
  49. * Use `type: 'parallel'` instead.
  50. */
  51. parallel?: boolean;
  52. /**
  53. * Whether the state node is "transient". A state node is considered transient if it has
  54. * an immediate transition from a "null event" (empty string), taken upon entering the state node.
  55. */
  56. private _transient;
  57. /**
  58. * The child state nodes.
  59. */
  60. states: StateNodesConfig<TContext, TStateSchema, TEvent>;
  61. /**
  62. * The type of history on this state node. Can be:
  63. *
  64. * - `'shallow'` - recalls only top-level historical state value
  65. * - `'deep'` - recalls historical state value at all levels
  66. */
  67. history: false | 'shallow' | 'deep';
  68. /**
  69. * The action(s) to be executed upon entering the state node.
  70. */
  71. onEntry: Array<ActionObject<TContext, TEvent>>;
  72. /**
  73. * The action(s) to be executed upon exiting the state node.
  74. */
  75. onExit: Array<ActionObject<TContext, TEvent>>;
  76. /**
  77. * The activities to be started upon entering the state node,
  78. * and stopped upon exiting the state node.
  79. */
  80. activities: Array<ActivityDefinition<TContext, TEvent>>;
  81. strict: boolean;
  82. /**
  83. * The parent state node.
  84. */
  85. parent?: StateNode<TContext, any, TEvent, any, any, any>;
  86. /**
  87. * The root machine node.
  88. */
  89. machine: StateNode<TContext, any, TEvent, TTypestate>;
  90. /**
  91. * The meta data associated with this state node, which will be returned in State instances.
  92. */
  93. meta?: TStateSchema extends {
  94. meta: infer D;
  95. } ? D : any;
  96. /**
  97. * The data sent with the "done.state._id_" event if this is a final state node.
  98. */
  99. doneData?: Mapper<TContext, TEvent, any> | PropertyMapper<TContext, TEvent, any>;
  100. /**
  101. * The string delimiter for serializing the path to a string. The default is "."
  102. */
  103. delimiter: string;
  104. /**
  105. * The order this state node appears. Corresponds to the implicit SCXML document order.
  106. */
  107. order: number;
  108. /**
  109. * The services invoked by this state node.
  110. */
  111. invoke: Array<InvokeDefinition<TContext, TEvent>>;
  112. options: MachineOptions<TContext, TEvent>;
  113. schema: MachineSchema<TContext, TEvent>;
  114. __xstatenode: true;
  115. description?: string;
  116. private __cache;
  117. private idMap;
  118. tags: string[];
  119. constructor(
  120. /**
  121. * The raw config used to create the machine.
  122. */
  123. config: StateNodeConfig<TContext, TStateSchema, TEvent>, options?: MachineOptions<TContext, TEvent>,
  124. /**
  125. * The initial extended state
  126. */
  127. _context?: Readonly<TContext> | (() => Readonly<TContext>), // TODO: this is unsafe, but we're removing it in v5 anyway
  128. _stateInfo?: {
  129. parent: StateNode<any, any, any, any, any, any>;
  130. key: string;
  131. });
  132. private _init;
  133. /**
  134. * Clones this state machine with custom options and context.
  135. *
  136. * @param options Options (actions, guards, activities, services) to recursively merge with the existing options.
  137. * @param context Custom context (will override predefined context)
  138. */
  139. withConfig(options: InternalMachineOptions<TContext, TEvent, TResolvedTypesMeta, true>, context?: TContext | (() => TContext)): StateNode<TContext, TStateSchema, TEvent, TTypestate, TServiceMap, TResolvedTypesMeta>;
  140. /**
  141. * Clones this state machine with custom context.
  142. *
  143. * @param context Custom context (will override predefined context, not recursive)
  144. */
  145. withContext(context: TContext | (() => TContext)): StateNode<TContext, TStateSchema, TEvent, TTypestate>;
  146. get context(): TContext;
  147. /**
  148. * The well-structured state node definition.
  149. */
  150. get definition(): StateNodeDefinition<TContext, TStateSchema, TEvent>;
  151. toJSON(): StateNodeDefinition<TContext, TStateSchema, TEvent>;
  152. /**
  153. * The mapping of events to transitions.
  154. */
  155. get on(): TransitionDefinitionMap<TContext, TEvent>;
  156. get after(): Array<DelayedTransitionDefinition<TContext, TEvent>>;
  157. /**
  158. * All the transitions that can be taken from this state node.
  159. */
  160. get transitions(): Array<TransitionDefinition<TContext, TEvent>>;
  161. private getCandidates;
  162. /**
  163. * All delayed transitions from the config.
  164. */
  165. private getDelayedTransitions;
  166. /**
  167. * Returns the state nodes represented by the current state value.
  168. *
  169. * @param state The state value or State instance
  170. */
  171. getStateNodes(state: StateValue | State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>): Array<StateNode<TContext, any, TEvent, TTypestate, TServiceMap, TResolvedTypesMeta>>;
  172. /**
  173. * Returns `true` if this state node explicitly handles the given event.
  174. *
  175. * @param event The event in question
  176. */
  177. handles(event: Event<TEvent>): boolean;
  178. /**
  179. * Resolves the given `state` to a new `State` instance relative to this machine.
  180. *
  181. * This ensures that `.events` and `.nextEvents` represent the correct values.
  182. *
  183. * @param state The state to resolve
  184. */
  185. resolveState(state: State<TContext, TEvent, any, any> | StateConfig<TContext, TEvent>): State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>;
  186. private transitionLeafNode;
  187. private transitionCompoundNode;
  188. private transitionParallelNode;
  189. private _transition;
  190. getTransitionData(state: State<TContext, TEvent, any, any, any>, event: Event<TEvent> | SCXML.Event<TEvent>): StateTransition<TContext, TEvent> | undefined;
  191. private next;
  192. private getPotentiallyReenteringNodes;
  193. private getActions;
  194. /**
  195. * Determines the next state given the current `state` and sent `event`.
  196. *
  197. * @param state The current State instance or state value
  198. * @param event The event that was sent at the current state
  199. * @param context The current context (extended state) of the current state
  200. */
  201. transition(state: StateValue | State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta> | undefined, event: Event<TEvent> | SCXML.Event<TEvent>, context?: TContext, exec?: PredictableActionArgumentsExec): State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>;
  202. private resolveRaisedTransition;
  203. private resolveTransition;
  204. /**
  205. * Returns the child state node from its relative `stateKey`, or throws.
  206. */
  207. getStateNode(stateKey: string): StateNode<TContext, any, TEvent, TTypestate, TServiceMap, TResolvedTypesMeta>;
  208. /**
  209. * Returns the state node with the given `stateId`, or throws.
  210. *
  211. * @param stateId The state ID. The prefix "#" is removed.
  212. */
  213. getStateNodeById(stateId: string): StateNode<TContext, any, TEvent, any, TServiceMap, TResolvedTypesMeta>;
  214. /**
  215. * Returns the relative state node from the given `statePath`, or throws.
  216. *
  217. * @param statePath The string or string array relative path to the state node.
  218. */
  219. getStateNodeByPath(statePath: string | string[]): StateNode<TContext, any, TEvent, any, any, any>;
  220. /**
  221. * Resolves a partial state value with its full representation in this machine.
  222. *
  223. * @param stateValue The partial state value to resolve.
  224. */
  225. resolve(stateValue: StateValue): StateValue;
  226. private getResolvedPath;
  227. private get initialStateValue();
  228. getInitialState(stateValue: StateValue, context?: TContext): State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>;
  229. /**
  230. * The initial State instance, which includes all actions to be executed from
  231. * entering the initial state.
  232. */
  233. get initialState(): State<TContext, TEvent, TStateSchema, TTypestate, TResolvedTypesMeta>;
  234. /**
  235. * The target state value of the history state node, if it exists. This represents the
  236. * default state value to transition to if no history value exists yet.
  237. */
  238. get target(): StateValue | undefined;
  239. /**
  240. * Returns the leaf nodes from a state path relative to this state node.
  241. *
  242. * @param relativeStateId The relative state path to retrieve the state nodes
  243. * @param history The previous state to retrieve history
  244. * @param resolve Whether state nodes should resolve to initial child state nodes
  245. */
  246. getRelativeStateNodes(relativeStateId: StateNode<TContext, any, TEvent>, historyValue?: HistoryValue, resolve?: boolean): Array<StateNode<TContext, any, TEvent>>;
  247. get initialStateNodes(): Array<StateNode<TContext, any, TEvent, any, any, any>>;
  248. /**
  249. * Retrieves state nodes from a relative path to this state node.
  250. *
  251. * @param relativePath The relative path from this state node
  252. * @param historyValue
  253. */
  254. getFromRelativePath(relativePath: string[]): Array<StateNode<TContext, any, TEvent, any, any, any>>;
  255. private historyValue;
  256. /**
  257. * Resolves to the historical value(s) of the parent state node,
  258. * represented by state nodes.
  259. *
  260. * @param historyValue
  261. */
  262. private resolveHistory;
  263. /**
  264. * All the state node IDs of this state node and its descendant state nodes.
  265. */
  266. get stateIds(): string[];
  267. /**
  268. * All the event types accepted by this state node and its descendants.
  269. */
  270. get events(): Array<TEvent['type']>;
  271. /**
  272. * All the events that have transitions directly from this state node.
  273. *
  274. * Excludes any inert events.
  275. */
  276. get ownEvents(): Array<TEvent['type']>;
  277. private resolveTarget;
  278. private formatTransition;
  279. private formatTransitions;
  280. }
  281. export { StateNode };
  282. //# sourceMappingURL=StateNode.d.ts.map