https://github.com/spearwolf/coffee_state_machine
a finite state machine made in and for coffeescript (javascript)
https://github.com/spearwolf/coffee_state_machine
Last synced: about 1 year ago
JSON representation
a finite state machine made in and for coffeescript (javascript)
- Host: GitHub
- URL: https://github.com/spearwolf/coffee_state_machine
- Owner: spearwolf
- License: mit
- Created: 2012-07-24T18:39:11.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2017-11-17T05:25:03.000Z (over 8 years ago)
- Last Synced: 2025-02-22T17:42:23.082Z (over 1 year ago)
- Language: CoffeeScript
- Homepage:
- Size: 465 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# coffee_state_machine [](http://travis-ci.org/spearwolf/coffee_state_machine)
a __finite state machine__ made in and for [CoffeeScript](http://coffeescript.org/) (javascript).
_currently there is no separated documentation - but just take a look into [tests/](test/). it is very easy to use._
this libray is licensed under the [MIT license](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](https://github.com/pluginaweek/state_machine), but now it's slightly different ..
* high code-coverage using the [mocha](http://visionmedia.github.com/mocha/) test framework
* written in 100% [CoffeeScript](http://coffeescript.org/)
#### cheat sheet
``` coffeescript
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
```