Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
J

javascript-state-machine

> 编程语言
Open source

A javascript finite state machine library

8.8K stars0 likes1 views
WebsiteGitHub

About

A javascript finite state machine library

Javascript State Machine

A library for finite state machines.


NOTE for existing users

VERSION 3.0 Is a significant rewrite from earlier versions. Existing 2.x users should be sure to read the Upgrade Guide.


Installation

In a browser:

xml
  <script src='state-machine.js'></script>

after downloading the source or the minified version

Using npm:

bash
  npm install --save-dev javascript-state-machine

In Node.js:

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

Usage

A state machine can be constructed using:

javascript
  var 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' }
    ],
    methods: {
      onMelt:     function() { console.log('I melted')    },
      onFreeze:   function() { console.log('I froze')     },
      onVaporize: function() { console.log('I vaporized') },
      onCondense: function() { console.log('I condensed') }
    }
  });

... which creates an object with a current state property:

  • fsm.state

... methods to transition to a different state:

  • fsm.melt()
  • fsm.freeze()
  • fsm.vaporize()
  • fsm.condense()

... observer methods called automatically during the lifecycle of a transition:

  • onMelt()
  • onFreeze()
  • onVaporize()
  • onCondense()

... along with the following helper methods:

  • fsm.is(s) - return true if state s is the current state
  • fsm.can(t) - return true if transition t can occur from the current state
  • fsm.cannot(t) - return true if transition t cannot occur from the current state
  • fsm.transitions() - return list of transitions that are allowed from the current state
  • fsm.allTransitions() - return list of all possible transitions
  • fsm.allStates() - return list of all possible states

Terminology

A state machine consists of a set of States

  • solid
  • liquid
  • gas

A state machine changes state by using Transitions

  • melt
  • freeze
  • vaporize
  • condense

A state machine can perform actions during a transition by observing Lifecycle Events

  • onBeforeMelt
  • onAfterMelt
  • onLeaveSolid
  • onEnterLiquid
  • ...

A state machine can also have arbitrary Data and Methods.

Multiple instances of a state machine can be created using a State Machine Factory.

Documentation

Read more about

  • States and Transitions
  • Data and Methods
  • Lifecycle Events
  • Asynchronous Transitions
  • Initialization
  • Error Handling
  • State History
  • Visualization
  • State Machine Factory
  • Upgrading from 2.x

Contributing

You can Contribute to this project with issues or pull requests.

Release Notes

See RELEASE NOTES file.

License

See MIT LICENSE file.

Contact

If you have any ideas, feedback, requests or bug reports, you can reach me at [email protected], or via my website: jakesgordon.com

Issues· 74 open

View all issuesOpen on GitHub
  • #149

    [question] How is it possible add different data for each state?

    Updated Nov 21, 2024
  • #212

    Warning: .can will cause a conditional transaction function to be called twice

    Updated Jul 31, 2023
  • #211

    Hierarchical state machine via the factory design?

    Updated Jun 23, 2023
  • #210

    Trailing slash on void elements has no effect and interacts badly with unquoted attribute values.

    Updated Mar 16, 2023
  • #209

    Trailing slash on void elements has no effect and interacts badly with unquoted attribute values.

    Updated Mar 16, 2023
  • #208

    Consider adding a lang attribute to the html start tag to declare the language of this document. (Validation)

    Updated Mar 16, 2023
  • #207

    .can(transition) doesn't work as expected.

    Updated Dec 7, 2022
  • #91

    Any typings for this cool library?

    FRUpdated May 12, 2022
  • #112

    [V3]TypeError: StateMachine is not a constructor

    v3.0QUESTIONUpdated Apr 27, 2022
  • #148

    Within the transition callback, the cannot not work right

    Updated Jun 1, 2021

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 29, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言