Skip to content

Commit 44a7162

Browse files
committed
added "Asynchronous State Transitions" section to README
1 parent 5462141 commit 44a7162

2 files changed

Lines changed: 52 additions & 9 deletions

File tree

README.md

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ Callbacks
135135
* onenter**state** - fired when entering the new state
136136
* onafter**event** - fired after the event
137137

138+
You can affect the event in 2 ways:
139+
140+
* return `false` from an `onbeforeevent` handler then you can cancel the event.
141+
* return `false` from an `onleavestate` handler then you can perform an asynchronous state transition (see next section)
142+
138143
For convenience, the 2 most useful callbacks can be shortened:
139144

140145
* on**event** - convenience shorthand for onafter**event**
@@ -179,15 +184,53 @@ Additionally, they can be added and removed from the state machine at any time:
179184
fsm.onred = null;
180185
fsm.onchangestate = function(event, from, to) { document.body.className = to; };
181186

182-
**NOTES:**
183-
184-
* If you return `false` from an `onbeforeevent` handler then you can cancel the event.
185-
* If you return `false` from an `onleavestate` handler then you can perform an asynchronous state transition (see next section)
186-
187187
Asynchronous State Transitions
188188
==============================
189189

190-
* **TODO**
190+
Sometimes, you need to execute some asynchronous code during a state transition and ensure the
191+
new state is not entered until your code has completed.
192+
193+
A good example of this is when you transition out of a `menu` state, perhaps you want to gradually
194+
fade the menu away, or slide it off the screen and don't want to transition to your `game` state
195+
until after that animation has been performed.
196+
197+
**New in v2.0** you can now return `false` from your `onleavestate` handler and the state machine
198+
will be _'put on hold'_ until you trigger the transition when ready using the new `transition()`
199+
method.
200+
201+
For example, using jQuery effects:
202+
203+
var fsm = StateMachine.create({
204+
205+
initial: 'menu',
206+
207+
events: [
208+
{ name: 'play', from: 'menu', to: 'game' },
209+
{ name: 'quit', from: 'game', to: 'menu' }
210+
],
211+
212+
callbacks: {
213+
214+
onentermenu: function() { $('#menu').show(); },
215+
onentergame: function() { $('#game').show(); },
216+
217+
onleavemenu: function() {
218+
$('#menu').fadeOut('fast', function() {
219+
fsm.transition();
220+
});
221+
return false; // tell StateMachine to defer next state until we call transition (in fade callback above)
222+
},
223+
224+
onleavegame: function() {
225+
$('#game').slideDown('slow', function() {
226+
fsm.transition();
227+
};
228+
return false; // tell StateMachine to defer next state until we call transition (in fade callback above)
229+
}
230+
231+
}
232+
});
233+
191234

192235
State Machine Classes
193236
=====================
@@ -206,7 +249,7 @@ instances:
206249
onpanic: function(event, from, to) { alert('panic'); },
207250
onclear: function(event, from, to) { alert('all is clear'); },
208251

209-
// other prototype methods
252+
// my other prototype methods
210253

211254
};
212255

RELEASE_NOTES.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
Version 2.0.0 (unreleased)
33
--------------------------
44

5+
* adding support for asynchronous state transitions (see README).
56
* consistent arguments for ALL callbacks, first 3 args are ALWAYS event name, from state and to state, followed by whatever arguments the user passed to the original event method.
6-
* adding support for asynchronous state transitions (see README)
7-
* added a generic `onchangestate(event,from,to)` callback to detect all state changes with a single function
7+
* added a generic `onchangestate(event,from,to)` callback to detect all state changes with a single function.
88

99
Version 1.2.0 (June 21st 2011)
1010
------------------------------

0 commit comments

Comments
 (0)