Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
, "author": {
"name": "Michael Wheeler"
}
, "version": "0.0.3"
, "contributors": [
{
"name": "Dave Timmerman"
}
]
, "version": "0.0.4"
, "licenses": [
{
"type": "MIT"
Expand Down
32 changes: 32 additions & 0 deletions spec/stateMachineSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,38 @@
ok(_out);
});

test('enter method is called with event method parameters', function () {
var _in1, _in2;
newStates.build()
.state('simple', {
enter: function (str, data) {
_in1 = str;
_in2 = data;
}
})
.event('withParams', 'young', 'simple')
;

newStates.withParams('a string', {some: 'data'});
q.equal(_in1, 'a string');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These specs are failing for me: https://cl.ly/2C283C1l1w2y

q.equal(_in2, {some: 'data'});
});

test('eventName is added to enter method parameters', function () {
var _in;
newStates.build()
.state('simple', {
enter: function (str, data, name) {
_in = name;
}
})
.event('withParams', 'young', 'simple')
;

newStates.withEventName('a string', {some: 'data'});
q.equal(_in, 'simple');
});

test('does not overwrite existing properties', function () {
var st = states(function () {
this.state('simple');
Expand Down
15 changes: 9 additions & 6 deletions stateMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@

function buildEventShortcut(name) {
states[name] = states[name] || function () {
states.trigger(name);
var args = Array.prototype.slice.call(arguments);
var arr = args.sort();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this sort call? This is really unexpected in my experience, I'd expect the order or arguments to never change?

states.trigger(name, arr);
};
}

Expand Down Expand Up @@ -114,18 +116,19 @@
return curr && curr.name;
}

, setState: function (name) {
, setState: function (name, args) {
var prev = me.currentState(true);
currentState = me._states[name];

me.onChange(me.currentState(), prev.name);
is(currentState.enter, 'Function') && currentState.enter();
me.onChange(me.currentState(), prev.name, args);
args.push(me.currentState());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why put the current state at the end of the list of arguments? Wouldn't it be more predictable to have the current state as the first argument?

is(prev.leave, 'Function') && prev.leave();
is(currentState.enter, 'Function') && currentState.enter.apply(undefined, args);
}

, trigger: function (eventName) {
, trigger: function (eventName, args) {
var nextState = me.currentState(true).events[eventName];
nextState && me.setState(nextState);
nextState && me.setState(nextState, args);
}

, _states: {}
Expand Down