Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

javascript-state-machine

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

javascript-state-machine

A finite state machine library

  • 3.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
101K
decreased by-22.82%
Maintainers
1
Weekly downloads
 
Created

What is javascript-state-machine?

The `javascript-state-machine` npm package is a lightweight, flexible, and powerful state machine library for JavaScript. It allows you to define states and transitions in a clear and concise manner, making it easier to manage complex state logic in your applications.

What are javascript-state-machine's main functionalities?

Basic State Machine

This code demonstrates how to create a basic state machine with initial state 'solid' and transitions between 'solid', 'liquid', and 'gas' states.

const StateMachine = require('javascript-state-machine');

const fsm = new StateMachine({
  init: 'solid',
  transitions: [
    { name: 'melt', from: 'solid', to: 'liquid' },
    { name: 'freeze', from: 'liquid', to: 'solid' },
    { name: 'vaporize', from: 'liquid', to: 'gas' },
    { name: 'condense', from: 'gas', to: 'liquid' }
  ]
});

console.log(fsm.state); // 'solid'
fsm.melt();
console.log(fsm.state); // 'liquid'

Callbacks

This code demonstrates how to add callbacks to state transitions. The `onMelt` and `onFreeze` methods are called when the respective transitions occur.

const StateMachine = require('javascript-state-machine');

const fsm = new StateMachine({
  init: 'solid',
  transitions: [
    { name: 'melt', from: 'solid', to: 'liquid' },
    { name: 'freeze', from: 'liquid', to: 'solid' }
  ],
  methods: {
    onMelt: function() { console.log('I melted'); },
    onFreeze: function() { console.log('I froze'); }
  }
});

fsm.melt(); // 'I melted'
fsm.freeze(); // 'I froze'

State Machine with Data

This code demonstrates how to create a state machine with additional data. The `food` property is accessible within the state machine methods.

const StateMachine = require('javascript-state-machine');

const fsm = new StateMachine({
  init: 'hungry',
  transitions: [
    { name: 'eat', from: 'hungry', to: 'full' }
  ],
  data: { food: 'pizza' },
  methods: {
    onEat: function() { console.log(`Eating ${this.food}`); }
  }
});

fsm.eat(); // 'Eating pizza'

Other packages similar to javascript-state-machine

Keywords

FAQs

Package last updated on 12 Jul 2018

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc