@fsmoothy/core
Version:
FSMoothy is a feature-rich and easy-to-use finite state machine for TypeScript.
182 lines • 6.57 kB
TypeScript
import { TransitionOptions } from './transition';
import { AllowedNames, Callback, FsmContext, Guard, IStateMachine, INestedStateMachine, HydratedState, Nested, StateMachineConstructor, StateMachineParameters, IStateMachineInspectRepresentation } from './types';
export declare class _StateMachine<const State extends AllowedNames, const Event extends AllowedNames, Context extends FsmContext<unknown>> {
#private;
protected _context: Context;
constructor(parameters: StateMachineParameters<State, Event, Context>);
get context(): Context;
/**
* Current state.
*/
get current(): State;
/**
* Data object.
*/
get data(): Context['data'];
/**
* Active child state machine.
*/
get child(): INestedStateMachine<any, any, any> | null;
/**
* All events in the state machine.
*/
get events(): Array<Event>;
/**
* All states in the state machine.
*/
get states(): Array<State>;
get nested(): Array<Nested>;
/**
* Add transition to the state machine.
*
* @param transition - Transition to add.
* @returns New state machine.
*/
addTransition<const NewState extends State, const NewEvent extends Event>(from: ReadonlyArray<NewState> | NewState, event: Event, to: NewState, guardOrOptions?: Guard<Context> | TransitionOptions<Context>): IStateMachine<State | NewState, Event | NewEvent, Context>;
/**
* Add nested state machine.
*
* @param state - State to add.
* @param nestedState - Nested state machine.
*/
addNestedMachine(state: State, nestedState: Nested): this;
/**
* Removes all nested state machines by state.
*
* @param state - State to remove.
*/
removeState(state: State): this;
/**
* Checks if the state machine is in the given state.
*
* @param state - State to check.
*/
is(state: State): boolean;
/**
* Checks if the event can be triggered in the current state.
*
* @param event - Event to check.
*/
can<Arguments extends Array<unknown> = Array<unknown>>(event: Event, ...arguments_: Arguments): Promise<boolean>;
/**
* Subscribe to event. Will execute after transition.
*
* @param event - Event to subscribe to.
* @param callback - Callback to execute.
*
* @overload
* Without providing event will subscribe to `All` event.
*
* @param callback - Callback to execute.
*/
on(event: Event, callback: Callback<Context>): this;
on(callback: Callback<Context>): this;
/**
* Unsubscribe from event.
*
* @param event - Event to unsubscribe from.
* @param callback - Callback to unsubscribe.
*
* @overload
* Unsubscribe from `All` event.
*
* @param callback - Callback to unsubscribe.
*/
off(event: Event, callback: Callback<Context>): this;
off(callback: Callback<Context>): this;
/**
* Transitions the state machine to the next state.
*
* @param event - Event to trigger.
* @param arguments_ - Arguments to pass to lifecycle hooks.
*/
transition<Arguments extends Array<unknown> = Array<unknown>>(event: Event, ...arguments_: Arguments): Promise<this>;
/**
* Tries to transition the state machine to the next state.
* Returns `false` if the transition is not allowed instead of throwing an error.
*/
tryTransition<Arguments extends Array<unknown> = Array<unknown>>(event: Event, ...arguments_: Arguments): Promise<boolean>;
/**
* Will remove all transitions with the given from, to and event.
*/
removeTransition(from: State, event: Event, to: State): this;
/**
* Binds external context to the state machine callbacks.
*
* @param this - Context to bind.
* @returns state machine instance.
*/
bind<T>(_this: T): this;
/**
* Injects service into the state machine context.
*/
inject<const Key extends keyof Omit<Context, 'data'>>(key: Key, service: Context[Key]): this;
/**
* Injects service into the state machine context using factory function.
*
* @param key - Key to inject.
* @param service - Service factory function.
*/
injectAsync<const Key extends keyof Omit<Context, 'data'>>(key: Key, service: (fsm: this) => Promise<Context[Key]> | Context[Key]): this;
/**
* Hydrates the state machine to plain object.
*
* @returns Hydrated JSON.
*/
dehydrate(): HydratedState<State, Context['data']>;
/**
* Apply hydrated plain object to the state machine.
*
* @param hydrated - Hydrated JSON.
*/
hydrate(hydrated: HydratedState<State, Context['data']>): this;
/**
* Inspects the state machine.
*
* @returns representation of the state machine.
*/
inspect(): IStateMachineInspectRepresentation;
protected populateContext(parameters: StateMachineParameters<any, any, any>): void;
private makeNestedTransition;
private getAllowedTransition;
/**
* This method binds the callbacks of the transition to the state machine instance.
* It useful in case if we need to access the state machine instance from the callbacks.
*/
private bindToCallbacks;
private switchNestedState;
private makeTransition;
private executeTransition;
}
/**
* Creates a new state machine.
*
* @param parameters - State machine parameters.
* @param parameters.id - State machine id. Used in error messages.
* @param parameters.initial - Initial state.
* @param parameters.ctx - Context object.
* @param parameters.transitions - Transitions.
* @param parameters.transitions[].from - From state.
* @param parameters.transitions[].from[] - From states.
* @param parameters.transitions[].event - Event name.
* @param parameters.transitions[].to - To state.
* @param parameters.transitions[].onEnter - Callback to execute on enter.
* @param parameters.transitions[].onExit - Callback to execute on exit.
* @param parameters.transitions[].onLeave - Callback to execute on transition to the next state.
* @param parameters.transitions[].guard - Guard function.
*
* @example
* const stateMachine = new StateMachine({
* id: '1',
* initial: State.idle,
* transitions: [
* t(State.idle, Event.fetch, State.pending),
* t(State.pending, Event.resolve, State.idle),
* ],
* });
*
* @returns New state machine.
*
*/
export declare const StateMachine: StateMachineConstructor;
//# sourceMappingURL=fsm.d.ts.map