Skip to content

Commit 9a67a16

Browse files
committed
ensure before/after event hooks are called even when there is no state transition
1 parent 216b691 commit 9a67a16

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

state-machine.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ StateMachine = {
9696
var to = map[from];
9797
var args = Array.prototype.slice.call(arguments); // turn arguments into pure array
9898

99-
if (this.current != to) {
99+
if (false === StateMachine.beforeEvent.call(this, name, from, to, args))
100+
return;
100101

101-
if (false === StateMachine.beforeEvent.call(this, name, from, to, args))
102-
return;
102+
if (from !== to) {
103103

104104
var self = this;
105105
this.transition = function() { // prepare transition method for use either lower down, or by caller if they want an async transition (indicated by a false return value from leaveState)
@@ -114,8 +114,12 @@ StateMachine = {
114114
if (this.transition) // in case user manually called it but forgot to return false
115115
this.transition();
116116
}
117+
118+
return; // transition method took care of (or, if async, will take care of) the afterEvent, DONT fall through
117119
}
118120

121+
StateMachine.afterEvent.call(this, name, from, to, args); // this is only ever called if there was NO transition (e.g. if from === to)
122+
119123
};
120124
}
121125

0 commit comments

Comments
 (0)