Skip to content

Commit 1180b8a

Browse files
committed
Issue jakesgordon#22 - A Function to cancel a transition after StateMachine.ASYNC
1 parent 394c219 commit 1180b8a

File tree

6 files changed

+53
-4
lines changed

6 files changed

+53
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Javascript Finite State Machine (v2.1.0)
1+
Javascript Finite State Machine (v2.2.0)
22
========================================
33

44
This standalone javascript micro-framework provides a finite state machine for your pleasure.
@@ -193,6 +193,7 @@ For example, using jQuery effects:
193193
}
194194
});
195195

196+
>> _NOTE: If you decide to cancel the ASYNC event, you can call `fsm.transition.cancel();`
196197
197198
State Machine Classes
198199
=====================

RELEASE_NOTES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Version 2.2.0 (unreleased)
2+
--------------------------
3+
4+
* Allow async event transition to be cancelled (issue #22)
5+
16
Version 2.1.0 (January 7th 2012)
27
--------------------------------
38

demo/demo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Demo = function() {
4646

4747
onleavegreen: function(event, from, to) { log("LEAVE STATE: green"); },
4848
onleaveyellow: function(event, from, to) { log("LEAVE STATE: yellow"); },
49-
onleavered: function(event, from, to) { log("LEAVE STATE: red"); async(to); return false; },
49+
onleavered: function(event, from, to) { log("LEAVE STATE: red"); async(to); return StateMachine.ASYNC; },
5050

5151
ongreen: function(event, from, to) { log("ENTER STATE: green"); },
5252
onyellow: function(event, from, to) { log("ENTER STATE: yellow"); },

state-machine.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
//---------------------------------------------------------------------------
66

7-
VERSION: "2.1.0",
7+
VERSION: "2.2.0",
88

99
//---------------------------------------------------------------------------
1010

@@ -122,6 +122,10 @@
122122
StateMachine.changeState(fsm, name, from, to, args);
123123
StateMachine.afterEvent( fsm, name, from, to, args);
124124
};
125+
this.transition.cancel = function() { // provide a way for caller to cancel async transition if desired (issue #22)
126+
fsm.transition = null;
127+
StateMachine.afterEvent(fsm, name, from, to, args);
128+
}
125129

126130
var leave = StateMachine.leaveState(this, name, from, to, args);
127131
if (false === leave) {

state-machine.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test_async.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,45 @@ test("state transition fired without completing previous transition", function()
246246

247247
//-----------------------------------------------------------------------------
248248

249+
test("state transition can be cancelled (github issue #22)", function() {
250+
251+
var fsm = StateMachine.create({
252+
initial: 'green',
253+
events: [
254+
{ name: 'warn', from: 'green', to: 'yellow' },
255+
{ name: 'panic', from: 'yellow', to: 'red' },
256+
{ name: 'calm', from: 'red', to: 'yellow' },
257+
{ name: 'clear', from: 'yellow', to: 'green' }
258+
],
259+
callbacks: {
260+
onleavegreen: function() { return StateMachine.ASYNC; },
261+
onleaveyellow: function() { return StateMachine.ASYNC; },
262+
onleavered: function() { return StateMachine.ASYNC; }
263+
}
264+
});
265+
266+
equals(fsm.current, 'green', "initial state should be green");
267+
fsm.warn(); equals(fsm.current, 'green', "should still be green because we haven't transitioned yet");
268+
fsm.transition(); equals(fsm.current, 'yellow', "warn event should transition from green to yellow");
269+
fsm.panic(); equals(fsm.current, 'yellow', "should still be yellow because we haven't transitioned yet");
270+
equals(fsm.can('panic'), false, "but cannot panic a 2nd time because a transition is still pending")
271+
272+
raises(fsm.panic.bind(fsm), /event panic inappropriate because previous transition did not complete/);
273+
274+
fsm.transition.cancel();
275+
276+
equals(fsm.current, 'yellow', "should still be yellow because we cancelled the async transition");
277+
equals(fsm.can('panic'), true, "can now panic again because we cancelled previous async transition");
278+
279+
fsm.panic();
280+
fsm.transition();
281+
282+
equals(fsm.current, 'red', "should finally be red now that we completed the async transition");
283+
284+
});
285+
286+
//-----------------------------------------------------------------------------
287+
249288
test("callbacks are ordered correctly", function() {
250289

251290
var called = [];

0 commit comments

Comments
 (0)