Permalink
Browse files

Issue #22 - A Function to cancel a transition after StateMachine.ASYNC

  • Loading branch information...
1 parent 394c219 commit 1180b8ab3f0c23becf66899909a9163969be24f3 @jakesgordon committed May 9, 2012
Showing with 53 additions and 4 deletions.
  1. +2 −1 README.md
  2. +5 −0 RELEASE_NOTES.md
  3. +1 −1 demo/demo.js
  4. +5 −1 state-machine.js
  5. +1 −1 state-machine.min.js
  6. +39 −0 test/test_async.js
View
@@ -1,4 +1,4 @@
-Javascript Finite State Machine (v2.1.0)
+Javascript Finite State Machine (v2.2.0)
========================================
This standalone javascript micro-framework provides a finite state machine for your pleasure.
@@ -193,6 +193,7 @@ For example, using jQuery effects:
}
});
+>> _NOTE: If you decide to cancel the ASYNC event, you can call `fsm.transition.cancel();`
State Machine Classes
=====================
View
@@ -1,3 +1,8 @@
+Version 2.2.0 (unreleased)
+--------------------------
+
+ * Allow async event transition to be cancelled (issue #22)
+
Version 2.1.0 (January 7th 2012)
--------------------------------
View
@@ -46,7 +46,7 @@ Demo = function() {
onleavegreen: function(event, from, to) { log("LEAVE STATE: green"); },
onleaveyellow: function(event, from, to) { log("LEAVE STATE: yellow"); },
- onleavered: function(event, from, to) { log("LEAVE STATE: red"); async(to); return false; },
+ onleavered: function(event, from, to) { log("LEAVE STATE: red"); async(to); return StateMachine.ASYNC; },
ongreen: function(event, from, to) { log("ENTER STATE: green"); },
onyellow: function(event, from, to) { log("ENTER STATE: yellow"); },
View
@@ -4,7 +4,7 @@
//---------------------------------------------------------------------------
- VERSION: "2.1.0",
+ VERSION: "2.2.0",
//---------------------------------------------------------------------------
@@ -122,6 +122,10 @@
StateMachine.changeState(fsm, name, from, to, args);
StateMachine.afterEvent( fsm, name, from, to, args);
};
+ this.transition.cancel = function() { // provide a way for caller to cancel async transition if desired (issue #22)
+ fsm.transition = null;
+ StateMachine.afterEvent(fsm, name, from, to, args);
+ }
var leave = StateMachine.leaveState(this, name, from, to, args);
if (false === leave) {
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -246,6 +246,45 @@ test("state transition fired without completing previous transition", function()
//-----------------------------------------------------------------------------
+test("state transition can be cancelled (github issue #22)", function() {
+
+ var fsm = StateMachine.create({
+ initial: 'green',
+ events: [
+ { name: 'warn', from: 'green', to: 'yellow' },
+ { name: 'panic', from: 'yellow', to: 'red' },
+ { name: 'calm', from: 'red', to: 'yellow' },
+ { name: 'clear', from: 'yellow', to: 'green' }
+ ],
+ callbacks: {
+ onleavegreen: function() { return StateMachine.ASYNC; },
+ onleaveyellow: function() { return StateMachine.ASYNC; },
+ onleavered: function() { return StateMachine.ASYNC; }
+ }
+ });
+
+ equals(fsm.current, 'green', "initial state should be green");
+ fsm.warn(); equals(fsm.current, 'green', "should still be green because we haven't transitioned yet");
+ fsm.transition(); equals(fsm.current, 'yellow', "warn event should transition from green to yellow");
+ fsm.panic(); equals(fsm.current, 'yellow', "should still be yellow because we haven't transitioned yet");
+ equals(fsm.can('panic'), false, "but cannot panic a 2nd time because a transition is still pending")
+
+ raises(fsm.panic.bind(fsm), /event panic inappropriate because previous transition did not complete/);
+
+ fsm.transition.cancel();
+
+ equals(fsm.current, 'yellow', "should still be yellow because we cancelled the async transition");
+ equals(fsm.can('panic'), true, "can now panic again because we cancelled previous async transition");
+
+ fsm.panic();
+ fsm.transition();
+
+ equals(fsm.current, 'red', "should finally be red now that we completed the async transition");
+
+});
+
+//-----------------------------------------------------------------------------
+
test("callbacks are ordered correctly", function() {
var called = [];

0 comments on commit 1180b8a

Please sign in to comment.