Spaces:
Paused
Paused
File size: 5,592 Bytes
d54c8f8 | 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 |
/*!
* Module dependencies.
*/
'use strict';
const utils = require('./utils'); // eslint-disable-line no-unused-vars
/**
* StateMachine represents a minimal `interface` for the
* constructors it builds via StateMachine.ctor(...).
*
* @api private
*/
const StateMachine = module.exports = exports = function StateMachine() {
};
/**
* StateMachine.ctor('state1', 'state2', ...)
* A factory method for subclassing StateMachine.
* The arguments are a list of states. For each state,
* the constructor's prototype gets state transition
* methods named after each state. These transition methods
* place their path argument into the given state.
*
* @param {String} state
* @param {String} [state]
* @return {Function} subclass constructor
* @api private
*/
StateMachine.ctor = function() {
const states = [...arguments];
const ctor = function() {
StateMachine.apply(this, arguments);
this.paths = {};
this.states = {};
};
ctor.prototype = new StateMachine();
ctor.prototype.constructor = ctor;
ctor.prototype.stateNames = states;
states.forEach(function(state) {
// Changes the `path`'s state to `state`.
ctor.prototype[state] = function(path) {
this._changeState(path, state);
};
});
return ctor;
};
/**
* This function is wrapped by the state change functions:
*
* - `require(path)`
* - `modify(path)`
* - `init(path)`
*
* @api private
*/
StateMachine.prototype._changeState = function _changeState(path, nextState) {
const prevState = this.paths[path];
if (prevState === nextState) {
return;
}
const prevBucket = this.states[prevState];
if (prevBucket) delete prevBucket[path];
this.paths[path] = nextState;
this.states[nextState] = this.states[nextState] || {};
this.states[nextState][path] = true;
};
/*!
* ignore
*/
StateMachine.prototype.clear = function clear(state) {
if (this.states[state] == null) {
return;
}
const keys = Object.keys(this.states[state]);
let i = keys.length;
let path;
while (i--) {
path = keys[i];
delete this.states[state][path];
delete this.paths[path];
}
};
/*!
* ignore
*/
StateMachine.prototype.clearPath = function clearPath(path) {
const state = this.paths[path];
if (!state) {
return;
}
delete this.paths[path];
delete this.states[state][path];
};
/**
* Gets the paths for the given state, or empty object `{}` if none.
* @api private
*/
StateMachine.prototype.getStatePaths = function getStatePaths(state) {
if (this.states[state] != null) {
return this.states[state];
}
return {};
};
/**
* Checks to see if at least one path is in the states passed in via `arguments`
* e.g., this.some('required', 'inited')
*
* @param {String} state that we want to check for.
* @api private
*/
StateMachine.prototype.some = function some() {
const _this = this;
const what = arguments.length ? arguments : this.stateNames;
return Array.prototype.some.call(what, function(state) {
if (_this.states[state] == null) {
return false;
}
return Object.keys(_this.states[state]).length;
});
};
/**
* This function builds the functions that get assigned to `forEach` and `map`,
* since both of those methods share a lot of the same logic.
*
* @param {String} iterMethod is either 'forEach' or 'map'
* @return {Function}
* @api private
*/
StateMachine.prototype._iter = function _iter(iterMethod) {
return function() {
let states = [...arguments];
const callback = states.pop();
if (!states.length) states = this.stateNames;
const _this = this;
const paths = states.reduce(function(paths, state) {
if (_this.states[state] == null) {
return paths;
}
return paths.concat(Object.keys(_this.states[state]));
}, []);
return paths[iterMethod](function(path, i, paths) {
return callback(path, i, paths);
});
};
};
/**
* Iterates over the paths that belong to one of the parameter states.
*
* The function profile can look like:
* this.forEach(state1, fn); // iterates over all paths in state1
* this.forEach(state1, state2, fn); // iterates over all paths in state1 or state2
* this.forEach(fn); // iterates over all paths in all states
*
* @param {String} [state]
* @param {String} [state]
* @param {Function} callback
* @api private
*/
StateMachine.prototype.forEach = function forEach() {
this.forEach = this._iter('forEach');
return this.forEach.apply(this, arguments);
};
/**
* Maps over the paths that belong to one of the parameter states.
*
* The function profile can look like:
* this.forEach(state1, fn); // iterates over all paths in state1
* this.forEach(state1, state2, fn); // iterates over all paths in state1 or state2
* this.forEach(fn); // iterates over all paths in all states
*
* @param {String} [state]
* @param {String} [state]
* @param {Function} callback
* @return {Array}
* @api private
*/
StateMachine.prototype.map = function map() {
this.map = this._iter('map');
return this.map.apply(this, arguments);
};
/**
* Returns a copy of this state machine
*
* @param {Function} callback
* @return {StateMachine}
* @api private
*/
StateMachine.prototype.clone = function clone() {
const result = new this.constructor();
result.paths = { ...this.paths };
for (const state of this.stateNames) {
if (!(state in this.states)) {
continue;
}
result.states[state] = this.states[state] == null ? this.states[state] : { ...this.states[state] };
}
return result;
};
|