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
4 changes: 4 additions & 0 deletions dist/state-machine-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ module.exports = function(options) { options = options || {};
}
},

cancel: function(instance, lifecycle) {
instance[past].pop()
},

methods: {},
properties: {}

Expand Down
2 changes: 1 addition & 1 deletion dist/state-machine-history.min.js

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

29 changes: 25 additions & 4 deletions dist/state-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,17 @@ mixin(JSM.prototype, {

beginTransit: function() { this.pending = true; },
endTransit: function(result) { this.pending = false; return result; },
failTransit: function(result) { this.pending = false; throw result; },
failTransit: function(result, args) {
var from = args[0].from;
var to = args[0].to;

if (this.state === to) {
this.state = from;
plugin.hook(this, 'cancel', args);
}
this.pending = false;
throw result;
},
doTransit: function(lifecycle) { this.state = lifecycle.to; },

observe: function(args) {
Expand All @@ -489,6 +499,14 @@ mixin(JSM.prototype, {
return [ event, result, true ]
},

callObserver: function(event, observer, args) {
try {
return observer[event].apply(observer, args);
} catch (error) {
this.failTransit.call(this, error, args);
}
},

observeEvents: function(events, args, previousEvent, previousResult) {
if (events.length === 0) {
return this.endTransit(previousResult === undefined ? true : previousResult);
Expand All @@ -507,11 +525,14 @@ mixin(JSM.prototype, {
return this.observeEvents(events, args, event, previousResult);
}
else {
var observer = observers.shift(),
result = observer[event].apply(observer, args);
var observer = observers.shift();
var result = this.callObserver.call(this, event, observer, args);
if (result && typeof result.then === 'function') {
var jsm = this
return result.then(this.observeEvents.bind(this, events, args, event))
.catch(this.failTransit.bind(this))
.catch(function (error) {
jsm.failTransit.call(jsm, error, args);
});
}
else if (result === false) {
return this.endTransit(false);
Expand Down
2 changes: 1 addition & 1 deletion dist/state-machine.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/lifecycle-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,6 @@ lifecycle events:

All subsequent lifecycle events will be cancelled and the state will remain unchanged.

To cancel a transition, you can also throw an exception `throw new Error('')` or await for a promise that is rejected inside any lifecycle handler.
The transition state will be cancelled and the following lifecycle events will not be triggered

4 changes: 4 additions & 0 deletions lib/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ module.exports = function(options) { options = options || {};
}
},

cancel: function(instance, lifecycle) {
instance[past].pop()
},

methods: {},
properties: {}

Expand Down
29 changes: 25 additions & 4 deletions lib/state-machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,17 @@ mixin(JSM.prototype, {

beginTransit: function() { this.pending = true; },
endTransit: function(result) { this.pending = false; return result; },
failTransit: function(result) { this.pending = false; throw result; },
failTransit: function(result, args) {
var from = args[0].from;
var to = args[0].to;

if (this.state === to) {
this.state = from;
plugin.hook(this, 'cancel', args);
}
this.pending = false;
throw result;
},
doTransit: function(lifecycle) { this.state = lifecycle.to; },

observe: function(args) {
Expand All @@ -489,6 +499,14 @@ mixin(JSM.prototype, {
return [ event, result, true ]
},

callObserver: function(event, observer, args) {
try {
return observer[event].apply(observer, args);
} catch (error) {
this.failTransit.call(this, error, args);
}
},

observeEvents: function(events, args, previousEvent, previousResult) {
if (events.length === 0) {
return this.endTransit(previousResult === undefined ? true : previousResult);
Expand All @@ -507,11 +525,14 @@ mixin(JSM.prototype, {
return this.observeEvents(events, args, event, previousResult);
}
else {
var observer = observers.shift(),
result = observer[event].apply(observer, args);
var observer = observers.shift();
var result = this.callObserver.call(this, event, observer, args);
if (result && typeof result.then === 'function') {
var jsm = this
return result.then(this.observeEvents.bind(this, events, args, event))
.catch(this.failTransit.bind(this))
.catch(function (error) {
jsm.failTransit.call(jsm, error, args);
});
}
else if (result === false) {
return this.endTransit(false);
Expand Down
29 changes: 25 additions & 4 deletions src/jsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,17 @@ mixin(JSM.prototype, {

beginTransit: function() { this.pending = true; },
endTransit: function(result) { this.pending = false; return result; },
failTransit: function(result) { this.pending = false; throw result; },
failTransit: function(result, args) {
var from = args[0].from;
var to = args[0].to;

if (this.state === to) {
this.state = from;
plugin.hook(this, 'cancel', args);
}
this.pending = false;
throw result;
},
doTransit: function(lifecycle) { this.state = lifecycle.to; },

observe: function(args) {
Expand All @@ -132,6 +142,14 @@ mixin(JSM.prototype, {
return [ event, result, true ]
},

callObserver: function(event, observer, args) {
try {
return observer[event].apply(observer, args);
} catch (error) {
this.failTransit.call(this, error, args);
}
},

observeEvents: function(events, args, previousEvent, previousResult) {
if (events.length === 0) {
return this.endTransit(previousResult === undefined ? true : previousResult);
Expand All @@ -150,11 +168,14 @@ mixin(JSM.prototype, {
return this.observeEvents(events, args, event, previousResult);
}
else {
var observer = observers.shift(),
result = observer[event].apply(observer, args);
var observer = observers.shift();
var result = this.callObserver.call(this, event, observer, args);
if (result && typeof result.then === 'function') {
var jsm = this
return result.then(this.observeEvents.bind(this, events, args, event))
.catch(this.failTransit.bind(this))
.catch(function (error) {
jsm.failTransit.call(jsm, error, args);
});
}
else if (result === false) {
return this.endTransit(false);
Expand Down
4 changes: 4 additions & 0 deletions src/plugin/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ module.exports = function(options) { options = options || {};
}
},

cancel: function(instance, lifecycle) {
instance[past].pop()
},

methods: {},
properties: {}

Expand Down
55 changes: 55 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,58 @@ test('pending transition handler can be customized', t => {
})

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

test('exception thrown in handler cancels state transition', t => {

var fsm = new StateMachine({
transitions: [
{ name: 'step1', from: 'none', to: 'A' },
{ name: 'step2', from: 'A', to: 'B' }
],
methods: {
onA:function () {
throw new Error('Error thrown in observer')
}
}
});

t.is(fsm.state, 'none')
t.is(fsm.can('step1'), true)
t.is(fsm.can('step2'), false)

const error = t.throws(() => {
fsm.step1();
})

t.is(error.message, 'Error thrown in observer')
t.is(fsm.state, 'none')

})

test('exception with promise thrown in handler cancels state transition', async t => {

var fsm = new StateMachine({
transitions: [
{ name: 'step1', from: 'none', to: 'A' },
{ name: 'step2', from: 'A', to: 'B' }
],
methods: {
onA: async function () {
return Promise.reject(new Error('Error thrown in observer'))
}
}
});

t.is(fsm.state, 'none')
t.is(fsm.can('step1'), true)
t.is(fsm.can('step2'), false)

try{
await fsm.step1()
t.is('Should fail', 'fail')
}catch(error){
t.is(error.message, 'Error thrown in observer')
}

t.is(fsm.state, 'none')
})
32 changes: 32 additions & 0 deletions test/plugin/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,35 @@ test('history can be used with a state machine factory applied to existing class
})

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

test('exception thrown in handler cancels state transition', t => {

var fsm = new StateMachine({
transitions: [
{ name: 'step1', from: 'none', to: 'A' },
{ name: 'step2', from: 'A', to: 'B' }
],
plugins:[
StateMachineHistory
],
methods: {
onB:function () {
throw new Error('Error thrown in observer')
}
}
});

t.is(fsm.state, 'none'); t.deepEqual(fsm.history, [ ]);
t.is(fsm.can('step1'), true)
t.is(fsm.can('step2'), false)

fsm.step1(); t.is(fsm.state, 'A'); t.deepEqual(fsm.history, [ 'A' ]);

const error = t.throws(() => {
fsm.step2();
})

t.is(fsm.state, 'A'); t.deepEqual(fsm.history, [ 'A' ]);

t.is(error.message, 'Error thrown in observer')
})