|
| 1 | +Javascript Finite State Machine |
| 2 | +=============================== |
| 3 | + |
| 4 | +This standalone javascript micro-framework provides a finite state machine for your pleasure. |
| 5 | + |
| 6 | + * You can find the [code here](https://github.com/jakesgordon/javascript-state-machine) |
| 7 | + * You can find a [description here](https://github.com/jakesgordon/javascript-state-machine) - COMING SOON |
| 8 | + * You can find a [working demo here](https://github.com/jakesgordon/javascript-state-machine) - COMING SOON |
| 9 | + |
| 10 | + * All code is in state-machine.js |
| 11 | + * No 3rd party library is required |
| 12 | + * Demo can be found in /index.html |
| 13 | + * QUnit tests can be found in /test/index.html |
| 14 | + |
| 15 | +Usage |
| 16 | +===== |
| 17 | + |
| 18 | +Include `state-machine.min.js` in your application. |
| 19 | + |
| 20 | +In its simplest form, create a standalone state machine using: |
| 21 | + |
| 22 | + var fsm = StateMachine.create({ |
| 23 | + state: 'green', |
| 24 | + events: [ |
| 25 | + { name: 'warn', from: 'green', to: 'yellow' }, |
| 26 | + { name: 'panic', from: 'yellow', to: 'red' }, |
| 27 | + { name: 'calm', from: 'red', to: 'yellow' }, |
| 28 | + { name: 'clear', from: 'yellow', to: 'green' } |
| 29 | + ]}); |
| 30 | + |
| 31 | +... will create an object with a method for each event: |
| 32 | + |
| 33 | + * fsm.warn() - transition from 'green' to 'yellow' |
| 34 | + * fsm.panic() - transition from 'yellow' to 'red' |
| 35 | + * fsm.calm() - transition from 'red' to 'yellow' |
| 36 | + * fsm.clear() - transition from 'yellow' to 'green' |
| 37 | + |
| 38 | +along with the following members: |
| 39 | + |
| 40 | + * fsm.current - contains the current state |
| 41 | + * fsm.is(s) - return true if state `s` is the current state |
| 42 | + * fsm.can(e) - return true if event `e` can be fired in the current state |
| 43 | + * fsm.cannot(e) - return true if event `e` cannot be fired in the current state |
| 44 | + |
| 45 | +Multiple Transitions |
| 46 | +==================== |
| 47 | + |
| 48 | +If an event should be available from multiple states, simply use an array in the event `from` argument: |
| 49 | + |
| 50 | + var fsm = StateMachine.create({ |
| 51 | + state: 'green', |
| 52 | + events: [ |
| 53 | + { name: 'warn', from: ['green'], to: 'yellow' }, |
| 54 | + { name: 'panic', from: ['green', 'yellow'], to: 'red' }, |
| 55 | + { name: 'calm', from: ['red'], to: 'yellow' }, |
| 56 | + { name: 'clear', from: ['red', 'yellow'], to: 'green' } |
| 57 | + ]}); |
| 58 | + |
| 59 | +Hooks |
| 60 | +===== |
| 61 | + |
| 62 | +4 hooks are available if FSM has methods using the following naming conventions: |
| 63 | + |
| 64 | + * onbefore**event** - fired before an event |
| 65 | + * onafter**event** - fired after an event |
| 66 | + * onenter**state** - fired when entering a state |
| 67 | + * onleave**state** - fired when leaving a state |
| 68 | + |
| 69 | +The order of the hooks should be as expected: |
| 70 | + |
| 71 | + * onbefore**event** - fired before an event |
| 72 | + * onleave**state** - fired when leaving existing state |
| 73 | + * onenter**state** - fired when entering new state |
| 74 | + * onafter**event** - fired after event |
| 75 | + |
| 76 | +For convenience, the 2 most useful hooks can be shortened: |
| 77 | + |
| 78 | + * on**event** - convenience shorthand for onafter**event** |
| 79 | + * on**state** - convenience shorthand for onenter**state** |
| 80 | + |
| 81 | +Hooks can be added after the FSM is created: |
| 82 | + |
| 83 | + var fsm = StateMachine.create({ |
| 84 | + state: 'green', |
| 85 | + events: [ |
| 86 | + { name: 'warn', from: 'green', to: 'yellow' }, |
| 87 | + { name: 'panic', from: 'yellow', to: 'red' }, |
| 88 | + { name: 'calm', from: 'red', to: 'yellow' }, |
| 89 | + { name: 'clear', from: 'yellow', to: 'green' } |
| 90 | + ]}); |
| 91 | + |
| 92 | + fsm.onpanic = function() { alert('panic!'); }; |
| 93 | + fsm.onclear = function() { alert('all clear!'); }; |
| 94 | + fsm.ongreen = function() { document.body.className = 'green'; }; |
| 95 | + fsm.onyellow = function() { document.body.className = 'yellow'; }; |
| 96 | + fsm.onred = function() { document.body.className = 'red'; }; |
| 97 | + |
| 98 | + fsm.panic() |
| 99 | + fsm.clear() |
| 100 | + ... |
| 101 | + |
| 102 | +Alternatively, hooks can be added before the FSM is created (to be sure of including the |
| 103 | +initial state transition), by turning an existing object into an FSM using the `target` |
| 104 | +option: |
| 105 | + |
| 106 | + var fsm = { |
| 107 | + onpanic : function() { alert('panic!'); }, |
| 108 | + onclear : function() { alert('all clear!'); }, |
| 109 | + ongreen : function() { document.body.className = 'green'; }, |
| 110 | + onyellow : function() { document.body.className = 'yellow'; }, |
| 111 | + onred : function() { document.body.className = 'red'; } |
| 112 | + }; |
| 113 | + |
| 114 | + StateMachine.create({ |
| 115 | + target: fsm, |
| 116 | + state: 'green', |
| 117 | + events: [ |
| 118 | + { name: 'warn', from: 'green', to: 'yellow' }, |
| 119 | + { name: 'panic', from: 'yellow', to: 'red' }, |
| 120 | + { name: 'calm', from: 'red', to: 'yellow' }, |
| 121 | + { name: 'clear', from: 'yellow', to: 'green' }, |
| 122 | + ]}); |
| 123 | + |
| 124 | + |
| 125 | + fsm.panic() |
| 126 | + fsm.clear() |
| 127 | + ... |
| 128 | + |
| 129 | +In this way, you can also turn all instances of a _class_ into an FSM by applying |
| 130 | +the state machine functionality in a constructor function, and adding your hooks |
| 131 | +into the prototype: |
| 132 | + |
| 133 | + MyFSM = function() { |
| 134 | + StateMachine.create({ |
| 135 | + target: this, |
| 136 | + state: 'green', |
| 137 | + events: [ |
| 138 | + { name: 'warn', from: 'green', to: 'yellow' }, |
| 139 | + { name: 'panic', from: 'yellow', to: 'red' }, |
| 140 | + { name: 'calm', from: 'red', to: 'yellow' }, |
| 141 | + { name: 'clear', from: 'yellow', to: 'green' } |
| 142 | + ]}); |
| 143 | + |
| 144 | + // other constructor behavior |
| 145 | + |
| 146 | + }; |
| 147 | + |
| 148 | + MyFSM.prototype = { |
| 149 | + |
| 150 | + onpanic: function() { alert('panic'); }, |
| 151 | + onclear: function() { alert('all is clear'); }, |
| 152 | + |
| 153 | + // other prototype methods |
| 154 | + |
| 155 | + }; |
| 156 | + |
| 157 | +This should be easy to adjust to fit your appropriate mechanism for object construction. |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
0 commit comments