small-state-machine

Small and simple state machine

Usage no npm install needed!

<script type="module">
  import smallStateMachine from 'https://cdn.skypack.dev/small-state-machine';
</script>

README

Small State Machine

State machine with simple syntax and usage.

TypeScript example:

import SmallStateMachine from 'small-state-machine'

enum States {
    Sunshine = 'Sunshine',
    Rain = 'Rain',
}

enum Triggers {
    makeClouds = 'make clouds',
    dispelClouds = 'dispel clouds',
}

const machine = new SmallStateMachine<States, Triggers>( States.Sunshine );

machine.configure( States.Sunshine )
    .onEntry( () => console.log( 'Let there be light!' ) )
    .onExit( () => console.log( 'I will be back!' ) )
    .permit( Triggers.makeClouds, States.Rain );
machine.configure( States.Rain )
    .permit( Triggers.dispelClouds, States.Sunshine );

machine.onStateChange( ( state ) => console.log( `Now in state ${state}!` ) );

machine.fire( Triggers.makeClouds );

Changelog

v1.3.0

  • Added default export for SmallStateMachine
  • Improved error message on nested fire() calls

v1.2.2

  • Added source code documentation.

v1.2.0 – 2021-04-13

  • Added SmallStateMachine.onStateChange to listen to state changes
  • Changed: Switch to jest for unit testing, remove non-dev dependencies

v1.1.0 – 2021-03-29

  • Changed: whenFired throws if the transition has no target state.
  • Changed: Dependencies updated, moved @types/jasmine to dev dependencies