0.3.2 • Published 10 years ago

coffee_state_machine v0.3.2

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

coffee_state_machine Build Status

a finite state machine made in and for CoffeeScript (javascript).

currently there is no separated documentation - but just take a look into tests/. it is very easy to use.

this libray is licensed under the MIT license.

some brief, high-level features include:

  • defining state machines on any javascript object or class (constructor)

  • state-driven instance behavior (methods and attributes)

  • hierarchical states (states could have parent/child relationships)

  • state (enter, exit) and transitions callbacks

  • flexible and very readable DSL based around states, events and transitions. design original based on pluginaweek/state_machine, but now it's slightly different ..

  • high code-coverage using the mocha test framework

  • written in 100% CoffeeScript

cheat sheet

sm = state_machine 'state', (state, event, transition) ->

   state.initial 'foo'

   state 'plah', ->

      one: -> yes
      two: -> no

   state 'bar', parent: 'plah', enter: (-> "onEnterBar"), exit: (-> "onExitBar"), ->

      one: -> no
      two: -> yes


   event 'boom', ->

      transition
         plah: foo
         bar: plah

      transition.from 'foo', to: 'bar', if: -> true, unless: -> false


   event 'bang', ->

      transition.from ['foo', 'bar'], to: 'bar', action: (oldState, newState) -> "..."

      transition.all except: 'bar', only: ['foo'], to: 'plah'



   sm.two = 2
   sm.boom three: 42

   sm.three is 42             # => yes
   sm.two()                   # => yes
   sm.state is 'bar'          # => yes
   sm.is_state('plah')        # => yes