API Reference
This document contains a description of the low-level API to define and execute processes.
FsmProcess
Callbacks
Note: All callback methods return a function unregistering the callback.
onStateCreate(handler: (state: FsmState) => void)
- registers a function called each time a new state is created. This method should use theFsmState#onEnter
/FsmState#onExit
methods to activate/deactivate states. This method can be called multiple times to register various state handlers.onStateError(handler: (state: FsmState, error: unknown) => void | Promise<void>)
- registers an error handler; this handler is called each time state activation/deactivation code raises an error.
FsmProcess
Methods
async dispatch(event: string): Promise<boolean>
– dispatches the specified event and triggers state transitions in this processasync dump(...args: unknown[]): Promise<FsmProcessDump>
- creates a dump of this process; a new process can be restored in the current state using theFsmProcess#restore(...)
method.async restore(dump: FsmProcessDump, ...args: unknown[]): Promise<void>
- restores the process using the dumped data. Note that the process should not be started yet.async shutdown(event?: string): Promise<void>
- shuts down the running process with an optional exit event
FsmState
Callbacks
onEnter(callback: () => void | Promise<void>)
- registers a callback function used to activate this state. It can be an asynchronous callback - the process will wait until all registered state handlers are resolved.onExit(callback: () => void | Promise<void>)
- registers a function to call when the process leaves this state. The process waits until all state handlers return control.onStateError(handler: FsmStateErrorHandler)
- registers an error handler; this handler is called whenonEnter
oronExit
methods raise an errordump(handler: (state: FsmState, dump: Record<string, unknown>) => void | Promise<void>)
- registers a handler to call when the process dumps its internal current state; the handler receives the state to dump and an object where the internal data should be copiedrestore(handler: (state: FsmState, dump: Record<string, unknown>) => void | Promise<void>)
- registers a handler restoring the current state; the handler receives the restored state and an object containing dumped information
FsmState
Methods
getData<T>(key: string, recursive: boolean = true): T | undefined
- returns a state-specific data object associated with the specified string key; if this method cannot find information associated with the given key then it recursively searches the data in parent states.useData<T>(key: string): [ (recursive: boolean = true) => this.getData<T>(key, recursive), (value: T) => this.setData(key, value)]
returns a pair of methods allowing to get and set data in the current state