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