• Stars
    star
    8,661
  • Rank 4,085 (Top 0.09 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 13 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A javascript finite state machine library

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 [email protected], or via my website: Code inComplete

More Repositories

1

javascript-racer

How to build an outrun-style racing game in javascript
HTML
1,033
star
2

bin-packing

A javascript binary tree based algorithm for 2d bin-packing suitable for generating CSS sprites
JavaScript
628
star
3

sprite-factory

Automatic CSS Sprite Generator
Ruby
620
star
4

javascript-tetris

A simple javascript tetris game
HTML
586
star
5

javascript-pong

A javascript version of the classic 1972 arcade game Pong!
JavaScript
202
star
6

javascript-tiny-platformer

A very minimal javascript platform game
JavaScript
172
star
7

javascript-gauntlet

An HTML5 Gauntlet-style game
JavaScript
155
star
8

javascript-audio-fx

A Simple HTML5 Audio Helper Library
JavaScript
111
star
9

javascript-boulderdash

An HTML5 implementation of the c64 classic Boulderdash game
JavaScript
103
star
10

javascript-snakes

An HTML5 Snakes Game
HTML
95
star
11

javascript-tower-platformer

A prototype for an html5 rotating tower platformer game
JavaScript
86
star
12

javascript-breakout

An HTML5 canvas implementation of breakout
JavaScript
72
star
13

rack-rabbit

A Unicorn-style forking server for hosting rabbitMQ consumers as load balanced Rack apps
Ruby
62
star
14

javascript-starfield

An old school 2d starfield using HTML5 <canvas>
JavaScript
46
star
15

javascript-delta

An HTML5 javascript shoot em up in the style of the old c64 classic game "Delta"
JavaScript
43
star
16

ruby-sample-daemon

A simple example of daemonizing a long running Ruby process
Ruby
13
star
17

tetris-shoes

A Tetris game in Ruby Shoes
Ruby
5
star
18

javascript-stutter

Can anyone make this minimalist canvas animation smooth ?
JavaScript
3
star
19

dotfiles

Personal dotfiles
Vim Script
2
star
20

sprite-factory-and-rails31

Experimenting with sprite-factory and the rails 3.1 asset pipeline
Ruby
2
star
21

snowpack-coverage-bug

Reproducing a snowpack/web-test-runner coverage issue
JavaScript
1
star
22

unified-assets

Easily unify and minify static javascript and css assets
Ruby
1
star
23

ruby-serializer

Declarative serialization of PORO's to JSON
Ruby
1
star
24

vue-template-brunch

Adds support to Brunch for pre-compiling standalone (NOT single-file) vue templates.
JavaScript
1
star