Skip to content

Commit 8936ebd

Browse files
committed
added some asyncronous transitions to demo page... not really happy with the declaration model yet.
1 parent 8300ad1 commit 8936ebd

8 files changed

Lines changed: 38 additions & 8 deletions

File tree

demo/demo.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#controls { text-align: center; }
44

5+
#demo #notes { margin-bottom: 1em; }
56
#demo #diagram { width: 400px; height: 275px; }
67
#demo #output { width: 100%; height: 30em; }
78

demo/demo.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,32 @@ Demo = {
2626

2727
onleavegreen: function() { this.log("LEAVE STATE: green"); },
2828
onleaveyellow: function() { this.log("LEAVE STATE: yellow"); },
29-
onleavered: function() { this.log("LEAVE STATE: red"); },
29+
onleavered: function() { this.log("LEAVE STATE: red"); this.asyncTransition(); return false; },
3030

3131
ongreen: function() { this.log("ENTER STATE: green"); },
3232
onyellow: function() { this.log("ENTER STATE: yellow"); },
3333
onred: function() { this.log("ENTER STATE: red"); },
3434

35+
asyncTransition: function() {
36+
var self = this;
37+
self.logTransition(3);
38+
setTimeout(function() {
39+
self.logTransition(2);
40+
setTimeout(function() {
41+
self.logTransition(1);
42+
setTimeout(function() {
43+
self.logTransition(0);
44+
self.transition();
45+
}, 1000);
46+
}, 1000);
47+
}, 1000);
48+
},
49+
50+
logTransition: function(n) {
51+
if (n)
52+
this.log("PENDING " + this.transition.event + " from " + this.transition.from + " to " + this.transition.to + " in : " + n + "...");
53+
},
54+
3555
log: function(msg, separate) {
3656
this.count = (this.count || 0) + (separate ? 1 : 0);
3757

demo/images/alerts.green.png

-846 Bytes
Loading

demo/images/alerts.red.png

-855 Bytes
Loading

demo/images/alerts.yellow.png

-845 Bytes
Loading

index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ <h1> Finite State Machine </h1>
2222
<div id="diagram">
2323
</div>
2424

25+
<div id="notes">
26+
<i>dashed lines are async events that take 3 seconds</i>
27+
</div>
28+
2529
<textarea id="output">
2630
</textarea>
2731

state-machine.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ StateMachine = {
8787
return function() {
8888

8989
if (this.transition)
90-
throw "event " + name + " innapropriate because previous async transition from " + this.transition.from + " to " + this.transition.to + " did not complete"
90+
throw "event " + name + " innapropriate because previous async transition (" + this.transition.event + ") from " + this.transition.from + " to " + this.transition.to + " did not complete"
9191

9292
if (this.cannot(name))
9393
throw "event " + name + " innapropriate in current state " + this.current;
@@ -103,9 +103,10 @@ StateMachine = {
103103
if (false === StateMachine.beforeEvent.call(this, name, args))
104104
return;
105105

106-
this.transition = function() { StateMachine.transition.call(self, name, from, to, args); self.transition = null; };
107-
this.transition.from = from;
108-
this.transition.to = to;
106+
this.transition = function() { StateMachine.transition.call(self, name, from, to, args); self.transition = null; };
107+
this.transition.event = name;
108+
this.transition.from = from;
109+
this.transition.to = to;
109110

110111
if (false === StateMachine.leaveState.call(this, this.current, args))
111112
async = true;

test/test_async.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ test("state transitions using onleavestate run-time return value instead of desi
174174

175175
fsm.onleavegreen = function(async) {
176176
if (async) {
177-
setTimeout(function() { fsm.transition() }, 10);
177+
setTimeout(function() {
178+
fsm.transition(); equals(fsm.current, 'yellow', "warn event should transition from green to yellow");
179+
start(); // move on to next test
180+
}, 10);
178181
return false;
179182
}
180183
}
@@ -183,7 +186,8 @@ test("state transitions using onleavestate run-time return value instead of desi
183186
fsm.warn(false); equals(fsm.current, 'yellow', "expected synchronous transition from green to yellow");
184187
fsm.clear(); equals(fsm.current, 'green', "clear event should transition from yellow to green");
185188
fsm.warn(true); equals(fsm.current, 'green', "should still be green because we haven't transitioned yet");
186-
fsm.transition(); equals(fsm.current, 'yellow', "warn event should transition from green to yellow");
189+
190+
stop(); // doing async stuff - dont run next qunit test until I call start() in callback above
187191

188192
});
189193

@@ -206,7 +210,7 @@ test("state transition fired without completing previous transition", function()
206210
fsm.transition(); equals(fsm.current, 'yellow', "warn event should transition from green to yellow");
207211
fsm.panic(); equals(fsm.current, 'yellow', "should still be yellow because we haven't transitioned yet");
208212

209-
raises(fsm.calm.bind(fsm), /event calm innapropriate because previous async transition from yellow to red did not complete/);
213+
raises(fsm.calm.bind(fsm), /event calm innapropriate because previous async transition \(panic\) from yellow to red did not complete/);
210214

211215
});
212216

0 commit comments

Comments
 (0)