Files
30-seconds-of-code/node_modules/xstate/es/stateUtils.js
2019-08-20 15:52:05 +02:00

153 lines
5.6 KiB
JavaScript

var __values = this && this.__values || function (o) {
var m = typeof Symbol === "function" && o[Symbol.iterator],
i = 0;
if (m) return m.call(o);
return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
};
import { keys } from './utils';
function getChildren(stateNode) {
return keys(stateNode.states).map(function (key) {
return stateNode.states[key];
});
}
export function getConfiguration(prevStateNodes, stateNodes) {
var e_1, _a, e_2, _b, e_3, _c;
var prevConfiguration = new Set(prevStateNodes);
var prevAdjList = getAdjList(prevConfiguration);
var configuration = new Set(stateNodes);
try {
// add all ancestors
for (var configuration_1 = __values(configuration), configuration_1_1 = configuration_1.next(); !configuration_1_1.done; configuration_1_1 = configuration_1.next()) {
var s = configuration_1_1.value;
var m = s.parent;
while (m && !configuration.has(m)) {
configuration.add(m);
m = m.parent;
}
}
} catch (e_1_1) {
e_1 = { error: e_1_1 };
} finally {
try {
if (configuration_1_1 && !configuration_1_1.done && (_a = configuration_1.return)) _a.call(configuration_1);
} finally {
if (e_1) throw e_1.error;
}
}
var adjList = getAdjList(configuration);
try {
// console.log('KEYS:', [...adjList.keys()].map(k => k.id));
// add descendants
for (var configuration_2 = __values(configuration), configuration_2_1 = configuration_2.next(); !configuration_2_1.done; configuration_2_1 = configuration_2.next()) {
var s = configuration_2_1.value;
if (s.type === 'compound' && (!adjList.get(s) || !adjList.get(s).length)) {
if (prevAdjList.get(s)) {
prevAdjList.get(s).forEach(function (sn) {
return configuration.add(sn);
});
} else {
s.initialStateNodes.forEach(function (sn) {
return configuration.add(sn);
});
}
} else {
if (s.type === 'parallel') {
try {
for (var _d = __values(getChildren(s)), _e = _d.next(); !_e.done; _e = _d.next()) {
var child = _e.value;
if (!configuration.has(child)) {
configuration.add(child);
if (prevAdjList.get(child)) {
prevAdjList.get(child).forEach(function (sn) {
return configuration.add(sn);
});
} else {
child.initialStateNodes.forEach(function (sn) {
return configuration.add(sn);
});
}
}
}
} catch (e_3_1) {
e_3 = { error: e_3_1 };
} finally {
try {
if (_e && !_e.done && (_c = _d.return)) _c.call(_d);
} finally {
if (e_3) throw e_3.error;
}
}
}
}
}
} catch (e_2_1) {
e_2 = { error: e_2_1 };
} finally {
try {
if (configuration_2_1 && !configuration_2_1.done && (_b = configuration_2.return)) _b.call(configuration_2);
} finally {
if (e_2) throw e_2.error;
}
}
return configuration;
}
function getValueFromAdj(baseNode, adjList) {
var stateValue = {};
var childStateNodes = adjList.get(baseNode);
if (!childStateNodes) {
return {}; // todo: fix?
}
if (baseNode.type === 'compound') {
if (childStateNodes[0]) {
if (childStateNodes[0].type === 'atomic') {
return childStateNodes[0].key;
}
} else {
return {};
}
}
childStateNodes.forEach(function (csn) {
stateValue[csn.key] = getValueFromAdj(csn, adjList);
});
return stateValue;
}
export function getAdjList(configuration) {
var e_4, _a;
var adjList = new Map();
try {
for (var configuration_3 = __values(configuration), configuration_3_1 = configuration_3.next(); !configuration_3_1.done; configuration_3_1 = configuration_3.next()) {
var s = configuration_3_1.value;
if (!adjList.has(s)) {
adjList.set(s, []);
}
if (s.parent) {
if (!adjList.has(s.parent)) {
adjList.set(s.parent, []);
}
adjList.get(s.parent).push(s);
}
}
} catch (e_4_1) {
e_4 = { error: e_4_1 };
} finally {
try {
if (configuration_3_1 && !configuration_3_1.done && (_a = configuration_3.return)) _a.call(configuration_3);
} finally {
if (e_4) throw e_4.error;
}
}
// console.log(
// [...adjList.keys()].map(key => [key.id, adjList.get(key)!.map(sn => sn.id)])
// );
return adjList;
}
export function getValue(rootNode, configuration) {
var config = getConfiguration([rootNode], configuration);
return getValueFromAdj(rootNode, getAdjList(config));
}
//# sourceMappingURL=stateUtils.js.map