@fabiospampinato/hsm
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.1 • Public • Published

    HSM

    Hierarchical State Machine implementation, with support for guards and enter/exit events.

    It extends FSM, read its documentation before reading this.

    Install

    $ npm install --save @fabiospampinato/hsm

    Usage

    import HSM from '@fabiospampinato/hsm';
     
    // The `states` object is similar to FSM's.
    // The only difference being that an optional `states` key can be used to nest states.
     
    const states = {
      alive: {
        states: {
          standing: {
            transitions: {
              walk: 'walking'
            }
          },
          walking: {
            transitions: {
              stop: 'standing',
              speedUp: 'running'
            }
          },
          running: {
            transitions: {
              slowDown: 'walking'
            }
          }
        }
      },
      dead {}
    };
     
    // The `model` object doesn't differ from the one FSM uses.
     
    const Model = new class {
      walk () {
        console.log ( 'Starting to walk...' );
      }
      walkingEnter () {
        console.log ( 'Now I\'m walking' );
      }
      walkingExit () {
        console.log ( 'Now I\'n not walking anymore' );
      }
    };
     
    const machine = new HSM ( Model, states, 'standing' );
     
    machine.transition ( 'walk' );
     
    machine.is ( 'walking' ); // true
    machine.is ( 'alive' ); // true
    machine.is ( 'alive', true ); // false

    API

    The API is the same that FSM provides, except for the following differences:

    new HSM ( model, states, initial? )

    Here the initial state can be implicit, in that case the root state will be setted as the initial one.

    If there are multiple states at the root level an error will be thrown.

    .is ( state: string, exactly?: boolean ): boolean

    By default it will return true also for parent states, if you only want it to match the current state pass true as the exactly argument.

    Related

    • FSM - Finite State Machine implementation, with support for guards and enter/exit events.

    License

    MIT © Fabio Spampinato

    Install

    npm i @fabiospampinato/hsm

    DownloadsWeekly Downloads

    5

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    23.8 kB

    Total Files

    13

    Last publish

    Collaborators

    • fabiospampinato