1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 |
|
8 | const utils = require('./utils');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | const StateMachine = module.exports = exports = function StateMachine() {
|
18 | };
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | StateMachine.ctor = function() {
|
35 | const states = [...arguments];
|
36 |
|
37 | const ctor = function() {
|
38 | StateMachine.apply(this, arguments);
|
39 | this.paths = {};
|
40 | this.states = {};
|
41 | };
|
42 |
|
43 | ctor.prototype = new StateMachine();
|
44 | ctor.prototype.constructor = ctor;
|
45 |
|
46 | ctor.prototype.stateNames = states;
|
47 |
|
48 | states.forEach(function(state) {
|
49 |
|
50 | ctor.prototype[state] = function(path) {
|
51 | this._changeState(path, state);
|
52 | };
|
53 | });
|
54 |
|
55 | return ctor;
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | StateMachine.prototype._changeState = function _changeState(path, nextState) {
|
69 | const prevState = this.paths[path];
|
70 | if (prevState === nextState) {
|
71 | return;
|
72 | }
|
73 | const prevBucket = this.states[prevState];
|
74 | if (prevBucket) delete prevBucket[path];
|
75 |
|
76 | this.paths[path] = nextState;
|
77 | this.states[nextState] = this.states[nextState] || {};
|
78 | this.states[nextState][path] = true;
|
79 | };
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | StateMachine.prototype.clear = function clear(state) {
|
86 | if (this.states[state] == null) {
|
87 | return;
|
88 | }
|
89 | const keys = Object.keys(this.states[state]);
|
90 | let i = keys.length;
|
91 | let path;
|
92 |
|
93 | while (i--) {
|
94 | path = keys[i];
|
95 | delete this.states[state][path];
|
96 | delete this.paths[path];
|
97 | }
|
98 | };
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | StateMachine.prototype.clearPath = function clearPath(path) {
|
105 | const state = this.paths[path];
|
106 | if (!state) {
|
107 | return;
|
108 | }
|
109 | delete this.paths[path];
|
110 | delete this.states[state][path];
|
111 | };
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | StateMachine.prototype.getStatePaths = function getStatePaths(state) {
|
119 | if (this.states[state] != null) {
|
120 | return this.states[state];
|
121 | }
|
122 | return {};
|
123 | };
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | StateMachine.prototype.some = function some() {
|
134 | const _this = this;
|
135 | const what = arguments.length ? arguments : this.stateNames;
|
136 | return Array.prototype.some.call(what, function(state) {
|
137 | if (_this.states[state] == null) {
|
138 | return false;
|
139 | }
|
140 | return Object.keys(_this.states[state]).length;
|
141 | });
|
142 | };
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | StateMachine.prototype._iter = function _iter(iterMethod) {
|
154 | return function() {
|
155 | let states = [...arguments];
|
156 | const callback = states.pop();
|
157 |
|
158 | if (!states.length) states = this.stateNames;
|
159 |
|
160 | const _this = this;
|
161 |
|
162 | const paths = states.reduce(function(paths, state) {
|
163 | if (_this.states[state] == null) {
|
164 | return paths;
|
165 | }
|
166 | return paths.concat(Object.keys(_this.states[state]));
|
167 | }, []);
|
168 |
|
169 | return paths[iterMethod](function(path, i, paths) {
|
170 | return callback(path, i, paths);
|
171 | });
|
172 | };
|
173 | };
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | StateMachine.prototype.forEach = function forEach() {
|
190 | this.forEach = this._iter('forEach');
|
191 | return this.forEach.apply(this, arguments);
|
192 | };
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | StateMachine.prototype.map = function map() {
|
210 | this.map = this._iter('map');
|
211 | return this.map.apply(this, arguments);
|
212 | };
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | StateMachine.prototype.clone = function clone() {
|
223 | const result = new this.constructor();
|
224 | result.paths = { ...this.paths };
|
225 | for (const state of this.stateNames) {
|
226 | if (!(state in this.states)) {
|
227 | continue;
|
228 | }
|
229 | result.states[state] = this.states[state] == null ? this.states[state] : { ...this.states[state] };
|
230 | }
|
231 | return result;
|
232 | };
|