Skip to content

Commit

Permalink
Merge branch 'transitions' of https://github.com/ForeverOceans/javasc…
Browse files Browse the repository at this point in the history
…ript-state-machine into ForeverOceans-transitions
  • Loading branch information
jakesgordon committed Jan 17, 2015
2 parents 1a664fa + f6596f0 commit 737bd08
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
12 changes: 11 additions & 1 deletion state-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,19 @@
var callbacks = cfg.callbacks || {};
var map = {};

// Key/Value pair to track allowable
// transitions (events) from current state
var transitions = {};

var add = function(e) {
var from = (e.from instanceof Array) ? e.from : (e.from ? [e.from] : [StateMachine.WILDCARD]); // allow 'wildcard' transition if 'from' is not specified
map[e.name] = map[e.name] || {};
for (var n = 0 ; n < from.length ; n++)
for (var n = 0 ; n < from.length ; n++) {
if(!transitions[e.from]) transitions[e.from] = [];
transitions[e.from].push(e.name);

map[e.name][from[n]] = e.to || from[n]; // allow no-op transition if 'to' is not specified
}
};

if (initial) {
Expand All @@ -75,6 +83,8 @@
fsm.cannot = function(event) { return !this.can(event); };
fsm.error = cfg.error || function(name, from, to, args, error, msg, e) { throw e || msg; }; // default behavior when something unexpected happens is to throw an exception, but caller can override this behavior if desired (see github issue #3 and #17)

fsm.transitions = function() { return transitions[this.current]; };

fsm.isFinished = function() { return this.is(terminal); };

if (initial && !initial.defer)
Expand Down
2 changes: 1 addition & 1 deletion state-machine.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions test/test_basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,39 @@ test("is", function() {

//-----------------------------------------------------------------------------

test("transitions", 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' }
]});

equal(fsm.current, 'green', 'current state should be yellow');
deepEqual(fsm.transitions(), ['warn'], 'current transition(s) should be yellow');

fsm.warn();
equal(fsm.current, 'yellow', 'current state should be yellow');
deepEqual(fsm.transitions(), ['panic', 'clear'], 'current transition(s) should be panic and clear');

fsm.panic();
equal(fsm.current, 'red', 'current state should be red');
deepEqual(fsm.transitions(), ['calm'], 'current transition(s) should be calm');

fsm.calm();
equal(fsm.current, 'yellow', 'current state should be yellow');
deepEqual(fsm.transitions(), ['panic', 'clear'], 'current transion(s) should be panic and clear');

fsm.clear();
equal(fsm.current, 'green', 'current state should be green');
deepEqual(fsm.transitions(), ['warn'], 'current transion(s) should be warn');
});

//-----------------------------------------------------------------------------

test("isFinished", function() {

var fsm = StateMachine.create({
Expand Down

0 comments on commit 737bd08

Please sign in to comment.