Skip to content

Commit 131af49

Browse files
committed
extended fsm.is() to accept an array of states (in addition to just a single state)
1 parent f7ebd5d commit 131af49

5 files changed

Lines changed: 36 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ state and you would need to provide an event to take it out of this state:
274274
fsm.startup();
275275
alert(fsm.current); // "green"
276276

277-
If you specify the name of your initial event (as in all the earlier examples), then an
277+
If you specify the name of your initial state (as in all the earlier examples), then an
278278
implicit `startup` event will be created for you and fired when the state machine is constructed.
279279

280280
var fsm = StateMachine.create({

RELEASE_NOTES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Version 2.2.0 (unreleased)
22
--------------------------
3-
3+
4+
* extended `fsm.is()` to accept an array of states (in addition to a single state)
45
* Added generic event callbacks 'onbeforeevent' and 'onafterevent' (issue #28)
56
* Added generic state callbacks 'onleavestate' and 'onenterstate' (issue #28)
67
* Fixed 'undefined' event return codes (issue #34) - pull from gentooboontoo (thanks!)

state-machine.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
}
7070

7171
fsm.current = 'none';
72-
fsm.is = function(state) { return this.current == state; };
72+
fsm.is = function(state) { return (state instanceof Array) ? (state.indexOf(this.current) >= 0) : (this.current === state); };
7373
fsm.can = function(event) { return !this.transition && (map[event].hasOwnProperty(this.current) || map[event].hasOwnProperty(StateMachine.WILDCARD)); }
7474
fsm.cannot = function(event) { return !this.can(event); };
7575
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)

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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,37 @@ test("can & cannot", function() {
8080

8181
//-----------------------------------------------------------------------------
8282

83+
test("is", function() {
84+
85+
var fsm = StateMachine.create({
86+
initial: 'green',
87+
events: [
88+
{ name: 'warn', from: 'green', to: 'yellow' },
89+
{ name: 'panic', from: 'yellow', to: 'red' },
90+
{ name: 'calm', from: 'red', to: 'yellow' },
91+
{ name: 'clear', from: 'yellow', to: 'green' }
92+
]});
93+
94+
equals(fsm.current, 'green', "initial state should be green");
95+
96+
equals(fsm.is('green'), true, 'current state should match');
97+
equals(fsm.is('yellow'), false, 'current state should NOT match');
98+
equals(fsm.is(['green', 'red']), true, 'current state should match when included in array');
99+
equals(fsm.is(['yellow', 'red']), false, 'current state should NOT match when not included in array');
100+
101+
fsm.warn();
102+
103+
equals(fsm.current, 'yellow', "current state should be yellow");
104+
105+
equals(fsm.is('green'), false, 'current state should NOT match');
106+
equals(fsm.is('yellow'), true, 'current state should match');
107+
equals(fsm.is(['green', 'red']), false, 'current state should NOT match when not included in array');
108+
equals(fsm.is(['yellow', 'red']), true, 'current state should match when included in array');
109+
110+
});
111+
112+
//-----------------------------------------------------------------------------
113+
83114
test("inappropriate events", function() {
84115

85116
var fsm = StateMachine.create({

0 commit comments

Comments
 (0)