stateUtils.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var _tslib = require('./_virtual/_tslib.js');
  4. var utils = require('./utils.js');
  5. var isLeafNode = function (stateNode) {
  6. return stateNode.type === 'atomic' || stateNode.type === 'final';
  7. };
  8. function getAllChildren(stateNode) {
  9. return Object.keys(stateNode.states).map(function (key) {
  10. return stateNode.states[key];
  11. });
  12. }
  13. function getChildren(stateNode) {
  14. return getAllChildren(stateNode).filter(function (sn) {
  15. return sn.type !== 'history';
  16. });
  17. }
  18. function getAllStateNodes(stateNode) {
  19. var stateNodes = [stateNode];
  20. if (isLeafNode(stateNode)) {
  21. return stateNodes;
  22. }
  23. return stateNodes.concat(utils.flatten(getChildren(stateNode).map(getAllStateNodes)));
  24. }
  25. function getConfiguration(prevStateNodes, stateNodes) {
  26. var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
  27. var prevConfiguration = new Set(prevStateNodes);
  28. var prevAdjList = getAdjList(prevConfiguration);
  29. var configuration = new Set(stateNodes);
  30. try {
  31. // add all ancestors
  32. for (var configuration_1 = _tslib.__values(configuration), configuration_1_1 = configuration_1.next(); !configuration_1_1.done; configuration_1_1 = configuration_1.next()) {
  33. var s = configuration_1_1.value;
  34. var m = s.parent;
  35. while (m && !configuration.has(m)) {
  36. configuration.add(m);
  37. m = m.parent;
  38. }
  39. }
  40. } catch (e_1_1) {
  41. e_1 = {
  42. error: e_1_1
  43. };
  44. } finally {
  45. try {
  46. if (configuration_1_1 && !configuration_1_1.done && (_a = configuration_1.return)) _a.call(configuration_1);
  47. } finally {
  48. if (e_1) throw e_1.error;
  49. }
  50. }
  51. var adjList = getAdjList(configuration);
  52. try {
  53. // add descendants
  54. for (var configuration_2 = _tslib.__values(configuration), configuration_2_1 = configuration_2.next(); !configuration_2_1.done; configuration_2_1 = configuration_2.next()) {
  55. var s = configuration_2_1.value; // if previously active, add existing child nodes
  56. if (s.type === 'compound' && (!adjList.get(s) || !adjList.get(s).length)) {
  57. if (prevAdjList.get(s)) {
  58. prevAdjList.get(s).forEach(function (sn) {
  59. return configuration.add(sn);
  60. });
  61. } else {
  62. s.initialStateNodes.forEach(function (sn) {
  63. return configuration.add(sn);
  64. });
  65. }
  66. } else {
  67. if (s.type === 'parallel') {
  68. try {
  69. for (var _e = (e_3 = void 0, _tslib.__values(getChildren(s))), _f = _e.next(); !_f.done; _f = _e.next()) {
  70. var child = _f.value;
  71. if (!configuration.has(child)) {
  72. configuration.add(child);
  73. if (prevAdjList.get(child)) {
  74. prevAdjList.get(child).forEach(function (sn) {
  75. return configuration.add(sn);
  76. });
  77. } else {
  78. child.initialStateNodes.forEach(function (sn) {
  79. return configuration.add(sn);
  80. });
  81. }
  82. }
  83. }
  84. } catch (e_3_1) {
  85. e_3 = {
  86. error: e_3_1
  87. };
  88. } finally {
  89. try {
  90. if (_f && !_f.done && (_c = _e.return)) _c.call(_e);
  91. } finally {
  92. if (e_3) throw e_3.error;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. } catch (e_2_1) {
  99. e_2 = {
  100. error: e_2_1
  101. };
  102. } finally {
  103. try {
  104. if (configuration_2_1 && !configuration_2_1.done && (_b = configuration_2.return)) _b.call(configuration_2);
  105. } finally {
  106. if (e_2) throw e_2.error;
  107. }
  108. }
  109. try {
  110. // add all ancestors
  111. for (var configuration_3 = _tslib.__values(configuration), configuration_3_1 = configuration_3.next(); !configuration_3_1.done; configuration_3_1 = configuration_3.next()) {
  112. var s = configuration_3_1.value;
  113. var m = s.parent;
  114. while (m && !configuration.has(m)) {
  115. configuration.add(m);
  116. m = m.parent;
  117. }
  118. }
  119. } catch (e_4_1) {
  120. e_4 = {
  121. error: e_4_1
  122. };
  123. } finally {
  124. try {
  125. if (configuration_3_1 && !configuration_3_1.done && (_d = configuration_3.return)) _d.call(configuration_3);
  126. } finally {
  127. if (e_4) throw e_4.error;
  128. }
  129. }
  130. return configuration;
  131. }
  132. function getValueFromAdj(baseNode, adjList) {
  133. var childStateNodes = adjList.get(baseNode);
  134. if (!childStateNodes) {
  135. return {}; // todo: fix?
  136. }
  137. if (baseNode.type === 'compound') {
  138. var childStateNode = childStateNodes[0];
  139. if (childStateNode) {
  140. if (isLeafNode(childStateNode)) {
  141. return childStateNode.key;
  142. }
  143. } else {
  144. return {};
  145. }
  146. }
  147. var stateValue = {};
  148. childStateNodes.forEach(function (csn) {
  149. stateValue[csn.key] = getValueFromAdj(csn, adjList);
  150. });
  151. return stateValue;
  152. }
  153. function getAdjList(configuration) {
  154. var e_5, _a;
  155. var adjList = new Map();
  156. try {
  157. for (var configuration_4 = _tslib.__values(configuration), configuration_4_1 = configuration_4.next(); !configuration_4_1.done; configuration_4_1 = configuration_4.next()) {
  158. var s = configuration_4_1.value;
  159. if (!adjList.has(s)) {
  160. adjList.set(s, []);
  161. }
  162. if (s.parent) {
  163. if (!adjList.has(s.parent)) {
  164. adjList.set(s.parent, []);
  165. }
  166. adjList.get(s.parent).push(s);
  167. }
  168. }
  169. } catch (e_5_1) {
  170. e_5 = {
  171. error: e_5_1
  172. };
  173. } finally {
  174. try {
  175. if (configuration_4_1 && !configuration_4_1.done && (_a = configuration_4.return)) _a.call(configuration_4);
  176. } finally {
  177. if (e_5) throw e_5.error;
  178. }
  179. }
  180. return adjList;
  181. }
  182. function getValue(rootNode, configuration) {
  183. var config = getConfiguration([rootNode], configuration);
  184. return getValueFromAdj(rootNode, getAdjList(config));
  185. }
  186. function has(iterable, item) {
  187. if (Array.isArray(iterable)) {
  188. return iterable.some(function (member) {
  189. return member === item;
  190. });
  191. }
  192. if (iterable instanceof Set) {
  193. return iterable.has(item);
  194. }
  195. return false; // TODO: fix
  196. }
  197. function nextEvents(configuration) {
  198. return _tslib.__spreadArray([], _tslib.__read(new Set(utils.flatten(_tslib.__spreadArray([], _tslib.__read(configuration.map(function (sn) {
  199. return sn.ownEvents;
  200. })), false)))), false);
  201. }
  202. function isInFinalState(configuration, stateNode) {
  203. if (stateNode.type === 'compound') {
  204. return getChildren(stateNode).some(function (s) {
  205. return s.type === 'final' && has(configuration, s);
  206. });
  207. }
  208. if (stateNode.type === 'parallel') {
  209. return getChildren(stateNode).every(function (sn) {
  210. return isInFinalState(configuration, sn);
  211. });
  212. }
  213. return false;
  214. }
  215. function getMeta(configuration) {
  216. if (configuration === void 0) {
  217. configuration = [];
  218. }
  219. return configuration.reduce(function (acc, stateNode) {
  220. if (stateNode.meta !== undefined) {
  221. acc[stateNode.id] = stateNode.meta;
  222. }
  223. return acc;
  224. }, {});
  225. }
  226. function getTagsFromConfiguration(configuration) {
  227. return new Set(utils.flatten(configuration.map(function (sn) {
  228. return sn.tags;
  229. })));
  230. }
  231. exports.getAdjList = getAdjList;
  232. exports.getAllChildren = getAllChildren;
  233. exports.getAllStateNodes = getAllStateNodes;
  234. exports.getChildren = getChildren;
  235. exports.getConfiguration = getConfiguration;
  236. exports.getMeta = getMeta;
  237. exports.getTagsFromConfiguration = getTagsFromConfiguration;
  238. exports.getValue = getValue;
  239. exports.has = has;
  240. exports.isInFinalState = isInFinalState;
  241. exports.isLeafNode = isLeafNode;
  242. exports.nextEvents = nextEvents;