MCPcopy Index your code
hub / github.com/jakesgordon/javascript-state-machine

github.com/jakesgordon/javascript-state-machine @3.1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 3.1.0 ↗ · + Follow
41 symbols 104 edges 37 files 0 documented · 0% 1 cross-repo links updated 13mo ago★ 8,75066 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Javascript State Machine

NPM version Build Status

A library for finite state machines.

matter state machine

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:

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

after downloading the source or the minified version

Using npm:

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

In Node.js:

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

Usage

A state machine can be constructed using:

  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

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 jake@codeincomplete.com, or via my website: Code inComplete

Core symbols most depended-on inside this repo

logger
called by 22
test/helpers/lifecycle_logger.js
pause
called by 16
test/lifecycle.js
dotcfg
called by 14
lib/visualize.js
camelize
called by 12
lib/history.js
dotify
called by 10
lib/visualize.js
factory
called by 9
lib/state-machine.js
visualize
called by 8
lib/visualize.js
log
called by 8
examples/demo/demo.js

Shape

Function 41

Languages

TypeScript100%

Modules by API surface

lib/state-machine.js8 symbols
lib/visualize.js6 symbols
src/plugin/visualize.js5 symbols
src/app.js4 symbols
test/lifecycle.js3 symbols
lib/history.js2 symbols
examples/demo/demo.js2 symbols
test/util/mixin.js1 symbols
test/plugins.js1 symbols
test/plugin/history.js1 symbols
test/issues.js1 symbols
test/helpers/lifecycle_logger.js1 symbols

Dependencies from manifests, versioned

ava0.17.0 · 1×
fs-sync1.0.3 · 1×
glob7.1.1 · 1×
nyc10.0.0 · 1×
pascal-case2.0.0 · 1×
uglify-js2.7.5 · 1×
webpack2.2.0-rc.1 · 1×

For agents

$ claude mcp add javascript-state-machine \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact