Skip to content

Commit 6533ce8

Browse files
committed
further refactoring to break down buildEvent into its component parts
1 parent 92a4730 commit 6533ce8

1 file changed

Lines changed: 41 additions & 44 deletions

File tree

state-machine.js

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ StateMachine = {
1717
events[e.name] = events[e.name] || {};
1818
for (var n = 0 ; n < from.length ; n++)
1919
events[e.name][from[n]] = e;
20-
}
20+
};
2121

2222
if (initial) {
2323
initial.event = initial.event || 'startup';
@@ -29,7 +29,7 @@ StateMachine = {
2929

3030
for(var name in events) {
3131
if (events.hasOwnProperty(name))
32-
fsm[name] = this.buildEvent(name, events[name]);
32+
fsm[name] = StateMachine.buildEvent(name, events[name]);
3333
}
3434

3535
fsm.current = 'none';
@@ -41,48 +41,48 @@ StateMachine = {
4141
fsm[initial.event]();
4242

4343
return fsm;
44-
},
4544

46-
//---------------------------------------------------------------------------
45+
},
4746

48-
buildEvent: function(name, map) {
47+
//===========================================================================
4948

50-
var beforeEvent = function(name, args) {
51-
var func = this['onbefore' + name];
52-
if (func && (false === func.apply(this, args)))
53-
return false;
54-
};
49+
beforeEvent: function(name, args) {
50+
var func = this['onbefore' + name];
51+
if (func && (false === func.apply(this, args)))
52+
return false;
53+
},
5554

56-
var exitState = function(from, args) {
57-
var func = this['onleave' + from];
58-
if (func)
59-
func.apply(this, args);
60-
};
55+
exitState: function(from, args) {
56+
var func = this['onleave' + from];
57+
if (func)
58+
func.apply(this, args);
59+
},
6160

62-
var enterState = function(to, args) {
63-
var func = this['onenter' + to] || this['on' + to];
64-
if (func)
65-
func.apply(this, args);
66-
};
61+
enterState: function(to, args) {
62+
var func = this['onenter' + to] || this['on' + to];
63+
if (func)
64+
func.apply(this, args);
65+
},
6766

68-
var changeState = function(from, to, args) {
69-
var func = this['onchangestate'];
70-
if (func)
71-
func.apply(this, [from,to].concat(args));
72-
};
67+
changeState: function(from, to, args) {
68+
var func = this['onchangestate'];
69+
if (func)
70+
func.apply(this, [from,to].concat(args));
71+
},
7372

74-
var afterEvent = function(name, args) {
75-
var func = this['onafter' + name] || this['on' + name];
76-
if (func)
77-
func.apply(this, args);
78-
};
73+
afterEvent: function(name, args) {
74+
var func = this['onafter' + name] || this['on' + name];
75+
if (func)
76+
func.apply(this, args);
77+
},
7978

80-
var transition = function(from, to, args) {
81-
this.current = to;
82-
enterState.call(this, to, args);
83-
changeState.call(this, from, to, args);
84-
};
79+
transition: function(from, to, args) {
80+
this.current = to;
81+
StateMachine.enterState.call(this, to, args);
82+
StateMachine.changeState.call(this, from, to, args);
83+
},
8584

85+
buildEvent: function(name, map) {
8686
return function() {
8787

8888
if (this.cannot(name))
@@ -94,25 +94,22 @@ StateMachine = {
9494
var self = this;
9595
var args = Array.prototype.slice.call(arguments);
9696

97-
if (beforeEvent.call(this, name, args) === false)
97+
if (StateMachine.beforeEvent.call(this, name, args) === false)
9898
return;
9999

100100
if (this.current != to) {
101-
102-
this.transition = function() { transition.call(self, from, to, args); self.transition = null; };
103-
104-
exitState.call(this, this.current, arguments);
105-
101+
this.transition = function() { StateMachine.transition.call(self, from, to, args); self.transition = null; };
102+
StateMachine.exitState.call(this, this.current, arguments);
106103
if (!async)
107104
this.transition();
108105
}
109106

110-
afterEvent.call(this, name, arguments);
111-
}
107+
StateMachine.afterEvent.call(this, name, arguments);
112108

109+
};
113110
}
114111

115-
//---------------------------------------------------------------------------
112+
//===========================================================================
116113

117114
};
118115

0 commit comments

Comments
 (0)