Options
All
  • Public
  • Public/Protected
  • All
Menu

Class TypedStateMachine<T>

A strongly typed state machine inspired by finite-state-machine

Type parameters

  • T

Hierarchy

  • TypedStateMachine

Implements

Index

Constructors

constructor

Properties

Private _config

The initial configuration

Private _isInitialized

_isInitialized: boolean

Indicate if the current instance is initialized or not

Private _isTransitionPending

_isTransitionPending: boolean

Indicate is a transition is pending

Private _state

_state: T

The current internal state

Methods

Private _beginTransition

  • _beginTransition(): void

Private _endTransition

  • _endTransition(): void

Private _transitAsync

  • Changes the state of the machine

    Parameters

    • transition: Transition<T>

      The transition that eventually contains the hooks to invoke

    • newState: T

      The new state to reach

    • Optional options: TransitOptions

      The options to ignore hooks or events

    Returns Promise<boolean>

bindHookHandler

  • Add a new hook handler in the specified state and hook type.

    If an hook already exist with the same combination of state and hookType it will be replaced.

    Parameters

    • applyToStates: T | T[]

      The state associated to this hook

    • hookType: StateHookType

      The hook type

    • handler: HookHandler<T>

      The executed handler

    Returns void

can

  • can(newState: T): boolean

getAllStates

getAllTransitions

getConfig

getNextStates

  • getNextStates(): Array<T>

getState

  • getState(): T

Private getTransition

  • Return a transition from the current state to the newState, if it exists.

    Return null if the transition does not exist

    Parameters

    • newState: T

      The new state

    Returns Transition<T>

goto

  • goto(newState: T): boolean

gotoAsync

  • gotoAsync(newState: T): Promise<void>
  • Perform a transition from the current state to the given new state (without checking at the given transitions).

    During this transition all life cycles events will be triggered.

    Parameters

    • newState: T

      The destination state

    Returns Promise<void>

initialize

initializeAsync

Private invokeHook

  • invokeHook(targetState: T, hookType: StateHookType): false | true | Promise<boolean>
  • Execute the first hook that matches the target state and the hookType

    Parameters

    • targetState: T

      The state of which you want to execute the hook

    • hookType: StateHookType

      The hook type that you want to execute

    Returns false | true | Promise<boolean>

isPending

  • isPending(): boolean

isSelfLoop

  • isSelfLoop(newState: T): boolean
  • Return true if the transition to the target state is a self loop from the current state or not.

    Parameters

    • newState: T

      The target state

    Returns boolean

Private throwIfNotInitialized

  • throwIfNotInitialized(): void

transit

  • transit(targetState: T): boolean

transitAsync

  • transitAsync(newState: T): Promise<boolean>
  • Perform a transition from the current state to the given new state, if possible. If the transition is not possible return false, true if the transition succeeded.

    Lifecycle events:

    • onBeforeEveryTransition
    • onBeforeTransition
    • OnBeforeLeave [state]
    • onStateLeave
    • OnAfterLeave [state]
    • OnBeforeEnter [state]
    • onStateEnter
    • OnAfterEnter [state]
    • onAfterTransition
    • onAfterEveryTransition

    During this transition all life cycles events will be triggered.

    Parameters

    • newState: T

      The destination state

    Returns Promise<boolean>

transitByName

  • transitByName(transitionName: string): boolean

transitByNameAsync

  • transitByNameAsync(transitionName: string, options?: TransitOptions): Promise<boolean>
  • Make a transition from the current state to the first applicable state of the transition with the specified name.

    If multiple transition have the same name, the first applicable state is is picked up traversing transitions in order of declaration.

    If the transition has an array in "to" the first applicable state is is picked up.

    Parameters

    • transitionName: string

      The target transition name

    • Optional options: TransitOptions

    Returns Promise<boolean>

updateConfig

Object literals

Private _defaultTransitOptions

_defaultTransitOptions: object

The default transit options

fireEvents

fireEvents: true = true

ignoreHooksResults

ignoreHooksResults: false = false

invokeHooks

invokeHooks: true = true

Generated using TypeDoc