statemachine.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*!
  2. * Module dependencies.
  3. */
  4. 'use strict';
  5. const utils = require('./utils'); // eslint-disable-line no-unused-vars
  6. /*!
  7. * StateMachine represents a minimal `interface` for the
  8. * constructors it builds via StateMachine.ctor(...).
  9. *
  10. * @api private
  11. */
  12. const StateMachine = module.exports = exports = function StateMachine() {
  13. };
  14. /*!
  15. * StateMachine.ctor('state1', 'state2', ...)
  16. * A factory method for subclassing StateMachine.
  17. * The arguments are a list of states. For each state,
  18. * the constructor's prototype gets state transition
  19. * methods named after each state. These transition methods
  20. * place their path argument into the given state.
  21. *
  22. * @param {String} state
  23. * @param {String} [state]
  24. * @return {Function} subclass constructor
  25. * @private
  26. */
  27. StateMachine.ctor = function() {
  28. const states = [...arguments];
  29. const ctor = function() {
  30. StateMachine.apply(this, arguments);
  31. this.paths = {};
  32. this.states = {};
  33. this.stateNames = states;
  34. let i = states.length,
  35. state;
  36. while (i--) {
  37. state = states[i];
  38. this.states[state] = {};
  39. }
  40. };
  41. ctor.prototype = new StateMachine();
  42. states.forEach(function(state) {
  43. // Changes the `path`'s state to `state`.
  44. ctor.prototype[state] = function(path) {
  45. this._changeState(path, state);
  46. };
  47. });
  48. return ctor;
  49. };
  50. /*!
  51. * This function is wrapped by the state change functions:
  52. *
  53. * - `require(path)`
  54. * - `modify(path)`
  55. * - `init(path)`
  56. *
  57. * @api private
  58. */
  59. StateMachine.prototype._changeState = function _changeState(path, nextState) {
  60. const prevBucket = this.states[this.paths[path]];
  61. if (prevBucket) delete prevBucket[path];
  62. this.paths[path] = nextState;
  63. this.states[nextState][path] = true;
  64. };
  65. /*!
  66. * ignore
  67. */
  68. StateMachine.prototype.clear = function clear(state) {
  69. const keys = Object.keys(this.states[state]);
  70. let i = keys.length;
  71. let path;
  72. while (i--) {
  73. path = keys[i];
  74. delete this.states[state][path];
  75. delete this.paths[path];
  76. }
  77. };
  78. /*!
  79. * Checks to see if at least one path is in the states passed in via `arguments`
  80. * e.g., this.some('required', 'inited')
  81. *
  82. * @param {String} state that we want to check for.
  83. * @private
  84. */
  85. StateMachine.prototype.some = function some() {
  86. const _this = this;
  87. const what = arguments.length ? arguments : this.stateNames;
  88. return Array.prototype.some.call(what, function(state) {
  89. return Object.keys(_this.states[state]).length;
  90. });
  91. };
  92. /*!
  93. * This function builds the functions that get assigned to `forEach` and `map`,
  94. * since both of those methods share a lot of the same logic.
  95. *
  96. * @param {String} iterMethod is either 'forEach' or 'map'
  97. * @return {Function}
  98. * @api private
  99. */
  100. StateMachine.prototype._iter = function _iter(iterMethod) {
  101. return function() {
  102. let states = [...arguments];
  103. const callback = states.pop();
  104. if (!states.length) states = this.stateNames;
  105. const _this = this;
  106. const paths = states.reduce(function(paths, state) {
  107. return paths.concat(Object.keys(_this.states[state]));
  108. }, []);
  109. return paths[iterMethod](function(path, i, paths) {
  110. return callback(path, i, paths);
  111. });
  112. };
  113. };
  114. /*!
  115. * Iterates over the paths that belong to one of the parameter states.
  116. *
  117. * The function profile can look like:
  118. * this.forEach(state1, fn); // iterates over all paths in state1
  119. * this.forEach(state1, state2, fn); // iterates over all paths in state1 or state2
  120. * this.forEach(fn); // iterates over all paths in all states
  121. *
  122. * @param {String} [state]
  123. * @param {String} [state]
  124. * @param {Function} callback
  125. * @private
  126. */
  127. StateMachine.prototype.forEach = function forEach() {
  128. this.forEach = this._iter('forEach');
  129. return this.forEach.apply(this, arguments);
  130. };
  131. /*!
  132. * Maps over the paths that belong to one of the parameter states.
  133. *
  134. * The function profile can look like:
  135. * this.forEach(state1, fn); // iterates over all paths in state1
  136. * this.forEach(state1, state2, fn); // iterates over all paths in state1 or state2
  137. * this.forEach(fn); // iterates over all paths in all states
  138. *
  139. * @param {String} [state]
  140. * @param {String} [state]
  141. * @param {Function} callback
  142. * @return {Array}
  143. * @private
  144. */
  145. StateMachine.prototype.map = function map() {
  146. this.map = this._iter('map');
  147. return this.map.apply(this, arguments);
  148. };