Skip to main content

Class: FiniteStateMachine<TInput, TState>

Defined in: finite-state-machine/finite-state-machine.ts:11

A generic finite state machine (FSM) implementation. TInput represents the type of input used to trigger state transitions. TState represents the type of states managed by the FSM.

Type Parameters

TInput

TInput

TState

TState extends State

Constructors

Constructor

new FiniteStateMachine<TInput, TState>(states, startingState?): FiniteStateMachine<TInput, TState>

Defined in: finite-state-machine/finite-state-machine.ts:22

Creates an instance of FiniteStateMachine.

Parameters

states

TState[]

The states to include in the FSM.

startingState?

TState

The initial state of the FSM. If not provided, the first state in the states array will be used.

Returns

FiniteStateMachine<TInput, TState>

Throws

Will throw an error if no states are provided or if the starting state is not in the provided states.

Accessors

currentState

Get Signature

get currentState(): TState

Defined in: finite-state-machine/finite-state-machine.ts:91

Gets the current state of the finite state machine.

Returns

TState

The current state.

Methods

addState()

addState(state): void

Defined in: finite-state-machine/finite-state-machine.ts:77

Adds a new state to the finite state machine.

Parameters

state

TState

The state to add.

Returns

void

Throws

Will throw an error if the state already exists in the FSM.


addTransition()

addTransition(fromState, toState, transition): void

Defined in: finite-state-machine/finite-state-machine.ts:49

Adds a transition to the finite state machine.

Parameters

fromState

TState

The state to transition from.

toState

TState

The state to transition to.

transition

Transition<TInput>

The transition to add.

Returns

void

Throws

Will throw an error if either the fromState or toState does not exist in the FSM.


update()

update(input): boolean

Defined in: finite-state-machine/finite-state-machine.ts:100

Updates the finite state machine based on the provided input.

Parameters

input

TInput

The input used to evaluate transitions.

Returns

boolean

True if a state transition occurred; otherwise, false.