-
Notifications
You must be signed in to change notification settings - Fork 9
/
state_machine.js
310 lines (260 loc) · 7.69 KB
/
state_machine.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"use strict";
/**
* 有限状态机
*/
var StateMachine = function(states) {
if (this._isEmptyObj(states)) {
throw new Error('请正确设置状态!');
return;
}
this._events = {};
this._states = states;
this._initStates = this._cloneStates();
}
/**
* 将状态重置到初始化状态
*/
StateMachine.prototype.reset = function() {
for (var key in this._initStates) {
this._states[key] = this._initStates[key];
}
}
/**
* 设置状态
*/
StateMachine.prototype.set = function(stateKey, stateValue) {
var lasteStates = this._cloneStates();
var key;
if (typeof stateKey == 'object') {
for (key in stateKey) {
this._states[key] = stateKey[key];
}
}
this._states[stateKey] = stateValue;
// 是否触发事件
return this._check(lasteStates);
}
/**
* 获取当前状态
*/
StateMachine.prototype.get = function(stateKey) {
var curStates = this._cloneStates();
if (!arguments.length) return curStates;
return curStates[stateKey];
}
/**
* 绑定状态改变事件
*/
StateMachine.prototype.on = function(name, handler, conditions) {
// .on(fn, options)
if (typeof name == 'function') {
conditions = handler;
handler = name;
name = 'default';
}
if (!this._events[name]) this._events[name] = [];
this._events[name].push({
handler: handler,
conditions: conditions
});
}
/**
* 取消绑定事件
*/
StateMachine.prototype.off = function(name, handler) {
// .off(); off all
if (arguments.length == 0) {
this._events = {};
return;
}
// .off(handler);
if (typeof name == 'function') {
this._offByHandler(name);
return;
}
// no event for the name
if (!this._events[name]) return;
// .off(name)
if (typeof handler != 'function') {
delete this._events[name];
return;
}
// .off(name, handler)
this._events[name].filter(function(item) {
return item.handler !== handler;
});
}
StateMachine.prototype._offByHandler = function(handler) {
for (var name in this._events) {
this._events[name].filter(function(item) {
return item.handler !== handler;
});
}
}
// 触发事件
StateMachine.prototype.trigger = function(name, handler) {
// trigger all
if (arguments.length == 0) {
this._triggerByHandler('*');
return;
}
// .trigger(handler)
if (typeof name == 'function') {
this._triggerByHandler(name);
return;
}
if (!this._events[name]) return;
var curStates = this._cloneStates();
this._events[name].forEach(function(item) {
if (item.handler === handler) {
item.handler.call(that, {
name: name,
handler: item.handler,
from: curStates,
to: curStates
});
}
});
}
StateMachine.prototype._triggerByHandler = function(handler) {
var curStates = this._cloneStates();
var name;
var that = this;
for (name in this._events) {
this._events[name].forEach(function(item) {
if (handler == '*' || item.handler === handler) {
item.handler.call(that, {
name: name,
handler: item.handler,
from: curStates,
to: curStates
});
}
});
}
}
// 检查状态改变情况
StateMachine.prototype._check = function(lastStates) {
var changes = [];
var triggers = [];
var name, key;
var that = this;
for (key in this._states) {
if (lastStates[key] != this._states[key]) {
changes.push(key);
}
}
// 状态没有发生改变,不会自动触发事件
if (!changes.length) return false;
for (name in that._events) {
that._events[name].forEach(function(item) {
var flag = false;
if (typeof item.conditions == 'function') {
flag = item.conditions(lastStates, that._cloneStates(), changes);
} else if ( that._checkFrom(item.conditions, lastStates, changes)
&& that._checkTo(item.conditions, that._states, changes)
&& that._change(item.conditions, that._states, changes)
&& that._anyChange(item.conditions, that._states, changes) ) {
flag = true;
}
// save event to trigger
flag && triggers.push({
name: name,
handler: item.handler,
from: lastStates,
to: that._cloneStates()
});
});
}
// 统一触发事件
triggers.forEach(function(item) {
item.handler.call(that, item);
});
return true;
}
StateMachine.prototype._checkFrom = function(conditions, lastStates, changes) {
if (this._isEmptyObj(conditions)) return true;
return this._checkOneCondition(conditions['from'], lastStates, changes, lastStates);
}
StateMachine.prototype._checkTo = function(conditions, lastStates, changes) {
if (this._isEmptyObj(conditions)) return true;
return this._checkOneCondition(conditions['to'], this._states, changes, lastStates);
}
StateMachine.prototype._change = function(conditions, lastStates, changes) {
var flag = true;
if (this._isEmptyObj(conditions)) return true;
if (!Array.isArray(conditions.change)) return true;
conditions.change.forEach(function(v) {
if (!~changes.indexOf(v)) flag = false;
});
return flag;
}
StateMachine.prototype._anyChange = function(conditions, lastStates, changes) {
var flag = false;
if (this._isEmptyObj(conditions)) return true;
if (!Array.isArray(conditions.anyChange)) return true;
conditions.anyChange.forEach(function(v) {
if (~changes.indexOf(v)) flag = true;
});
return flag;
}
StateMachine.prototype._checkOneCondition = function(condition, compareStates, changes, lastStates) {
var curStates = this._cloneStates();
if (this._isEmptyObj(condition)) return true;
var flag = true;
var key, cond, type;
for (key in condition) {
cond = condition[key];
type = this._getConditionType(cond);
switch (type) {
case 'function':
flag = cond(lastStates[key], curStates[key]);
break;
case 'string':
case 'other':
flag = compareStates[key] === cond;
break;
case 'array':
flag = ~cond.indexOf(compareStates[key]);
break;
case 'regexp':
flag = cond.test(compareStates[key]);
break;
default:
flag = false;
}
if (!flag) break;
}
return flag;
}
// 判断条件的类型:function,string,array,regexp
StateMachine.prototype._getConditionType = function(obj) {
var type = 'other';
if (typeof obj == 'function') {
type = 'function';
} else if (typeof obj == 'string') {
type = 'string';
} else if (typeof obj == 'object') {
if (Array.isArray(obj)) {
type = 'array';
} else if (obj instanceof RegExp) {
type = 'regexp';
}
}
return type;
}
StateMachine.prototype._isEmptyObj = function(obj) {
if (typeof obj != 'object') return true;
for (var i in obj) return false;
return true;
}
// 复制当前状态
StateMachine.prototype._cloneStates = function(toCloneStates) {
var states = {};
var name;
toCloneStates = toCloneStates || this._states;
for (name in this._states) {
states[name] = this._states[name];
}
return states;
}