statemachine.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*!
  2. * Module dependencies.
  3. */
  4. 'use strict';
  5. const utils = require('./utils');
  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 = utils.args(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. const numArgs = arguments.length;
  103. let states = utils.args(arguments, 0, numArgs - 1);
  104. const callback = arguments[numArgs - 1];
  105. if (!states.length) states = this.stateNames;
  106. const _this = this;
  107. const paths = states.reduce(function(paths, state) {
  108. return paths.concat(Object.keys(_this.states[state]));
  109. }, []);
  110. return paths[iterMethod](function(path, i, paths) {
  111. return callback(path, i, paths);
  112. });
  113. };
  114. };
  115. /*!
  116. * Iterates over the paths that belong to one of the parameter states.
  117. *
  118. * The function profile can look like:
  119. * this.forEach(state1, fn); // iterates over all paths in state1
  120. * this.forEach(state1, state2, fn); // iterates over all paths in state1 or state2
  121. * this.forEach(fn); // iterates over all paths in all states
  122. *
  123. * @param {String} [state]
  124. * @param {String} [state]
  125. * @param {Function} callback
  126. * @private
  127. */
  128. StateMachine.prototype.forEach = function forEach() {
  129. this.forEach = this._iter('forEach');
  130. return this.forEach.apply(this, arguments);
  131. };
  132. /*!
  133. * Maps over the paths that belong to one of the parameter states.
  134. *
  135. * The function profile can look like:
  136. * this.forEach(state1, fn); // iterates over all paths in state1
  137. * this.forEach(state1, state2, fn); // iterates over all paths in state1 or state2
  138. * this.forEach(fn); // iterates over all paths in all states
  139. *
  140. * @param {String} [state]
  141. * @param {String} [state]
  142. * @param {Function} callback
  143. * @return {Array}
  144. * @private
  145. */
  146. StateMachine.prototype.map = function map() {
  147. this.map = this._iter('map');
  148. return this.map.apply(this, arguments);
  149. };