Description
Add simple state machine to Javascript objects.
Installation
npm install states
Usage
// requirestates = obj = name: 'duh' job: 'duh' // initialize // returns current state (initially the first element of the array passed above)obj // "starting" // defines helpers for checking current stateobj // trueobj // falseobjisNotDoingADamnThing // falseobj // false // can attach (optional) state event handlers directlyobj; // or indirectly based on naming convention (onEnter{CamelizedStateName}, onExit{CamelizedStateName})obj { console}; // sets current state, calls callbacks if they're defined on objobj // returns list of possible statesobj // [ 'starting', 'working', 'not doing a damn thing', 'finished' ]
results in
'bye'
'OH NO'
Examples
Quick example in JavaScript
states = { ; this;} Simpleprototype { if this console; this if this console; } Simpleprototype { console;}; simple = ;simple;
outputs
it starts in the first state by default.
exiting starting state
entering done state
all done
Longer example in CoffeeScript
states = require'states' : statesthis 'TOTALLY NOT GONNA BARF' 'OH NO' 'GONNA BARF' 'BARFING' 'FEELING WAY BETTER' : consolelogthisstate consolelog"yeah, i swear, i'm barfing" thisstate'OH NO' thisstate'GONNA BARF' if thisisGonnaBarf consolelog"i'm not feeling so good" thisstate'BARFING' thisstate'FEELING WAY BETTER' consolelog"i am " : consolelog'oooooooh nooooooooo' : consolelog'here goes' : consolelog'eeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahh' : consolelog' - http://eeuauaughhhuauaahh.ytmnd.com' barfer = barfergoTimeGottaMakeItHappenOhhhhhYeaaaaahDuffman
outputs
TOTALLY NOT GONNA BARF
yeah, i swear, i'm not barfing
oooooooh nooooooooo
i'm not feeling so good
here goes
eeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahheeuauaughhhuauaahh
- http://eeuauaughhhuauaahh.ytmnd.com
i am FEELING WAY BETTER
Specs
cake spec