Socket
Socket
Sign inDemoInstall

state-machine-builder-utility

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    state-machine-builder-utility

States


Version published
Maintainers
1
Install size
13.2 kB
Created

Readme

Source

States

trafficLightStateMachine = createStateMachine({
  initial_state: 'green',
  states: {
    green: {
      /* ... */
    },
    red: {
      /* ... */
    }
  }
});

Transitions

State transitions are defined on state nodes, in the on property:

trafficLightStateMachine = createStateMachine({
  initial_state: 'green',
  states: {
    green: {
      on: {
        NEXT: "yellow"
      }
    },
    red: {
      on: {
        NEXT: {
          target: "green"
        }
      }
    }
  }
});

Making Transition

trafficLightStateMachine.transition("NEXT")

Timed Transitions

trafficLightStateMachine = createStateMachine({
  initial_state: 'green',
  states: {
    green: {
      on: {
        after: {
          duration: 5000,
          actions: [{
            type: 'EVENT_TRIGGER',
            event: 'NEXT'
          }]
        }
        NEXT: "yellow"
      }
    },
    red: {
      on: {
        NEXT: {
          target: "green"
        }
      }
    }
  }
});

Transient Transitions

This type of transition is immediately taken without triggering an event as long as any given conditions are met

states: {
  failure: {
    on: {
      "": [
        {
          target: "trigger_request",
          condition: "gotBadResponse",
          actions: [
            {
              type: "ASSIGN_CONTEXT",
              eval_new_context: (context) => ({
                retry_count: context.retry_count + 1
              })
            }
          ]
        },
        { target: "fetching_failed", condition: "maxRetryError" }
      ]
    }
  }
}

Guards

This type of transition between states to only take place if certain conditions on the state are met.

maxRetryError: {
  gotBadResponse: (context, event, data) => {
    return context.retry_count > 3;
  }
}

Logging

you can enable logging by setting it true in the state machine configuration. This will log the when the events are triggered and a state of the state machine has changed.

createStateMachine({
  initial_state: 'green',
  logging: true,
});

FAQs

Last updated on 30 Nov 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc