Skip to content

Commit f6596f0

Browse files
author
Mathew Goldsborough
committed
Added method which returned allowable transitions from current state.
1 parent 37b255c commit f6596f0

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

state-machine.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,19 @@
4444
var callbacks = cfg.callbacks || {};
4545
var map = {};
4646

47+
// Key/Value pair to track allowable
48+
// transitions (events) from current state
49+
var transitions = {};
50+
4751
var add = function(e) {
4852
var from = (e.from instanceof Array) ? e.from : (e.from ? [e.from] : [StateMachine.WILDCARD]); // allow 'wildcard' transition if 'from' is not specified
4953
map[e.name] = map[e.name] || {};
50-
for (var n = 0 ; n < from.length ; n++)
54+
for (var n = 0 ; n < from.length ; n++) {
55+
if(!transitions[e.from]) transitions[e.from] = [];
56+
transitions[e.from].push(e.name);
57+
5158
map[e.name][from[n]] = e.to || from[n]; // allow no-op transition if 'to' is not specified
59+
}
5260
};
5361

5462
if (initial) {
@@ -75,6 +83,8 @@
7583
fsm.cannot = function(event) { return !this.can(event); };
7684
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)
7785

86+
fsm.transitions = function() { return transitions[this.current]; };
87+
7888
fsm.isFinished = function() { return this.is(terminal); };
7989

8090
if (initial && !initial.defer)

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_basics.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,39 @@ test("is", function() {
111111

112112
//-----------------------------------------------------------------------------
113113

114+
test("transitions", function() {
115+
116+
var fsm = StateMachine.create({
117+
initial: 'green',
118+
events: [
119+
{ name: 'warn', from: 'green', to: 'yellow' },
120+
{ name: 'panic', from: 'yellow', to: 'red' },
121+
{ name: 'calm', from: 'red', to: 'yellow' },
122+
{ name: 'clear', from: 'yellow', to: 'green' }
123+
]});
124+
125+
equal(fsm.current, 'green', 'current state should be yellow');
126+
deepEqual(fsm.transitions(), ['warn'], 'current transition(s) should be yellow');
127+
128+
fsm.warn();
129+
equal(fsm.current, 'yellow', 'current state should be yellow');
130+
deepEqual(fsm.transitions(), ['panic', 'clear'], 'current transition(s) should be panic and clear');
131+
132+
fsm.panic();
133+
equal(fsm.current, 'red', 'current state should be red');
134+
deepEqual(fsm.transitions(), ['calm'], 'current transition(s) should be calm');
135+
136+
fsm.calm();
137+
equal(fsm.current, 'yellow', 'current state should be yellow');
138+
deepEqual(fsm.transitions(), ['panic', 'clear'], 'current transion(s) should be panic and clear');
139+
140+
fsm.clear();
141+
equal(fsm.current, 'green', 'current state should be green');
142+
deepEqual(fsm.transitions(), ['warn'], 'current transion(s) should be warn');
143+
});
144+
145+
//-----------------------------------------------------------------------------
146+
114147
test("isFinished", function() {
115148

116149
var fsm = StateMachine.create({

0 commit comments

Comments
 (0)