forked from jakesgordon/javascript-state-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
161 lines (130 loc) · 4.78 KB
/
config.js
File metadata and controls
161 lines (130 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict'
//-------------------------------------------------------------------------------------------------
var mixin = require('./util/mixin'),
camelize = require('./util/camelize');
//-------------------------------------------------------------------------------------------------
function Config(options, StateMachine) {
options = options || {};
this.options = options; // preserving original options can be useful (e.g visualize plugin)
this.defaults = StateMachine.defaults;
this.states = [];
this.transitions = [];
this.map = {};
this.lifecycle = this.configureLifecycle();
this.init = this.configureInitTransition(options.init);
this.data = this.configureData(options.data);
this.methods = this.configureMethods(options.methods);
this.map[this.defaults.wildcard] = {};
this.configureTransitions(options.transitions || []);
this.plugins = this.configurePlugins(options.plugins, StateMachine.plugin);
}
//-------------------------------------------------------------------------------------------------
mixin(Config.prototype, {
addState: function(name) {
if (!this.map[name]) {
this.states.push(name);
this.addStateLifecycleNames(name);
this.map[name] = {};
}
},
addStateLifecycleNames: function(name) {
this.lifecycle.onEnter[name] = camelize.prepended('onEnter', name);
this.lifecycle.onLeave[name] = camelize.prepended('onLeave', name);
this.lifecycle.on[name] = camelize.prepended('on', name);
},
addTransition: function(name) {
if (this.transitions.indexOf(name) < 0) {
this.transitions.push(name);
this.addTransitionLifecycleNames(name);
}
},
addTransitionLifecycleNames: function(name) {
this.lifecycle.onBefore[name] = camelize.prepended('onBefore', name);
this.lifecycle.onAfter[name] = camelize.prepended('onAfter', name);
this.lifecycle.on[name] = camelize.prepended('on', name);
},
mapTransition: function(transition) {
var name = transition.name,
from = transition.from,
to = transition.to;
this.addState(from);
if (typeof to !== 'function')
this.addState(to);
this.addTransition(name);
this.map[from][name] = transition;
return transition;
},
configureLifecycle: function() {
return {
onBefore: { transition: 'onBeforeTransition' },
onAfter: { transition: 'onAfterTransition' },
onEnter: { state: 'onEnterState' },
onLeave: { state: 'onLeaveState' },
on: { transition: 'onTransition' }
};
},
configureInitTransition: function(init) {
if (typeof init === 'string') {
return this.mapTransition(mixin({}, this.defaults.init, { to: init, active: true }));
}
else if (typeof init === 'object') {
return this.mapTransition(mixin({}, this.defaults.init, init, { active: true }));
}
else {
this.addState(this.defaults.init.from);
return this.defaults.init;
}
},
configureData: function(data) {
if (typeof data === 'function')
return data;
else if (typeof data === 'object')
return function() { return data; }
else
return function() { return {}; }
},
configureMethods: function(methods) {
return methods || {};
},
configurePlugins: function(plugins, builtin) {
plugins = plugins || [];
var n, max, plugin;
for(n = 0, max = plugins.length ; n < max ; n++) {
plugin = plugins[n];
if (typeof plugin === 'function')
plugins[n] = plugin = plugin()
if (plugin.configure)
plugin.configure(this);
}
return plugins
},
configureTransitions: function(transitions) {
var i, n, transition, from, to, wildcard = this.defaults.wildcard;
for(n = 0 ; n < transitions.length ; n++) {
transition = transitions[n];
from = Array.isArray(transition.from) ? transition.from : [transition.from || wildcard]
to = transition.to || wildcard;
for(i = 0 ; i < from.length ; i++) {
this.mapTransition({ name: transition.name, from: from[i], to: to });
}
}
},
transitionFor: function(state, transition) {
var wildcard = this.defaults.wildcard;
return this.map[state][transition] ||
this.map[wildcard][transition];
},
transitionsFor: function(state) {
var wildcard = this.defaults.wildcard;
return Object.keys(this.map[state]).concat(Object.keys(this.map[wildcard]));
},
allStates: function() {
return this.states;
},
allTransitions: function() {
return this.transitions;
}
});
//-------------------------------------------------------------------------------------------------
module.exports = Config;
//-------------------------------------------------------------------------------------------------