Skip to content

Commit

Permalink
fix: already beeing in the target state of an action should result in…
Browse files Browse the repository at this point in the history
… a resolved promise.
  • Loading branch information
arlac77 committed Dec 29, 2015
1 parent 369027c commit 80e6427
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 12 additions & 2 deletions StateTransitionMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ module.exports.prepareActions = function (as) {
const a = as[actionName];
const initialTransitions = {};
const duringTransitions = {};
let target;

Object.keys(a).forEach(initialState => {
const t = a[initialState];
initialTransitions[initialState] = t;
Expand All @@ -34,11 +36,13 @@ module.exports.prepareActions = function (as) {
addState(t.initial, t);
addState(t.during, t);
addState(t.target);
target = t.target;
});
actions[actionName] = {
name: actionName,
initial: initialTransitions,
during: duringTransitions
during: duringTransitions,
target: target
};
});

Expand Down Expand Up @@ -216,7 +220,13 @@ module.exports.defineActionMethods = function (object, actionsAndStates) {

Object.defineProperty(object, actionName, {
value: function () {
// normal start

// target state already reached
if (this.state === action.target) {
return Promise.resolve(this);
}

// normal start we are in the initial state of the action
if (action.initial[this.state]) {
this._transition = action.initial[this.state];
this.state = this._transition.during;
Expand Down
7 changes: 7 additions & 0 deletions tests/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ describe('states', function () {
}, done);
});

it('can be started while running', function (done) {
o.start().then(() => {
assert.equal(o.state, 'running');
done();
}, done);
});

it('and stoped', function (done) {
o.stop().then(() => {
assert.equal(o.state, 'stopped');
Expand Down

0 comments on commit 80e6427

Please sign in to comment.