Skip to content

Commit ffec3f4

Browse files
authored
Merge pull request jakesgordon#64 from QuentinRoy/muti_wildcard
Fix FSM getting stuck into "*" state
2 parents 3d71da4 + c761134 commit ffec3f4

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

state-machine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
return function() {
145145

146146
var from = this.current;
147-
var to = map[from] || map[StateMachine.WILDCARD] || from;
147+
var to = map[from] || (map[StateMachine.WILDCARD] != StateMachine.WILDCARD ? map[StateMachine.WILDCARD] : from) || from;
148148
var args = Array.prototype.slice.call(arguments); // turn arguments into pure array
149149

150150
if (this.transition)

test/test_advanced.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,25 @@ test("callbacks are called when appropriate for prototype based state machine",
278278

279279

280280

281+
//-----------------------------------------------------------------------------
282+
283+
test("double wildcard transition does not change current state", function() {
284+
285+
var fsm = StateMachine.create({
286+
initial: 'green',
287+
events: [
288+
{ name: 'warn', from: 'green', to: 'yellow' },
289+
{ name: 'panic', from: ['green', 'yellow'], to: 'red' },
290+
{ name: 'noop', from: ['green', 'yellow'] }, // NOTE: 'to' not specified
291+
{ name: 'calm', from: 'red', to: 'yellow' },
292+
{ name: 'clear', from: ['yellow', 'red'], to: 'green' },
293+
{ name: 'lightup', from: '*' }, // Note: "double wildcard"
294+
{ name: 'lightup', from: 'green', to: 'yellow' }
295+
]});
296+
297+
equal(fsm.current, 'green', "start with correct state");
298+
299+
fsm.lightup(); equal(fsm.current, 'yellow', "lightup event should switch green to yellow");
300+
fsm.lightup(); equal(fsm.current, 'yellow', "lightup event should have no effect effect in other state than green");
301+
302+
});

0 commit comments

Comments
 (0)