xstate
Version:
Finite State Machines and Statecharts for the Modern Web.
278 lines (242 loc) • 7.55 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
var _tslib = require('./_virtual/_tslib.js');
var utils = require('./utils.js');
var isLeafNode = function (stateNode) {
return stateNode.type === 'atomic' || stateNode.type === 'final';
};
function getAllChildren(stateNode) {
return Object.keys(stateNode.states).map(function (key) {
return stateNode.states[key];
});
}
function getChildren(stateNode) {
return getAllChildren(stateNode).filter(function (sn) {
return sn.type !== 'history';
});
}
function getAllStateNodes(stateNode) {
var stateNodes = [stateNode];
if (isLeafNode(stateNode)) {
return stateNodes;
}
return stateNodes.concat(utils.flatten(getChildren(stateNode).map(getAllStateNodes)));
}
function getConfiguration(prevStateNodes, stateNodes) {
var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
var prevConfiguration = new Set(prevStateNodes);
var prevAdjList = getAdjList(prevConfiguration);
var configuration = new Set(stateNodes);
try {
// add all ancestors
for (var configuration_1 = _tslib.__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 {
// add descendants
for (var configuration_2 = _tslib.__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 previously active, add existing child nodes
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 _e = (e_3 = void 0, _tslib.__values(getChildren(s))), _f = _e.next(); !_f.done; _f = _e.next()) {
var child = _f.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 (_f && !_f.done && (_c = _e.return)) _c.call(_e);
} 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;
}
}
try {
// add all ancestors
for (var configuration_3 = _tslib.__values(configuration), configuration_3_1 = configuration_3.next(); !configuration_3_1.done; configuration_3_1 = configuration_3.next()) {
var s = configuration_3_1.value;
var m = s.parent;
while (m && !configuration.has(m)) {
configuration.add(m);
m = m.parent;
}
}
} catch (e_4_1) {
e_4 = {
error: e_4_1
};
} finally {
try {
if (configuration_3_1 && !configuration_3_1.done && (_d = configuration_3.return)) _d.call(configuration_3);
} finally {
if (e_4) throw e_4.error;
}
}
return configuration;
}
function getValueFromAdj(baseNode, adjList) {
var childStateNodes = adjList.get(baseNode);
if (!childStateNodes) {
return {}; // todo: fix?
}
if (baseNode.type === 'compound') {
var childStateNode = childStateNodes[0];
if (childStateNode) {
if (isLeafNode(childStateNode)) {
return childStateNode.key;
}
} else {
return {};
}
}
var stateValue = {};
childStateNodes.forEach(function (csn) {
stateValue[csn.key] = getValueFromAdj(csn, adjList);
});
return stateValue;
}
function getAdjList(configuration) {
var e_5, _a;
var adjList = new Map();
try {
for (var configuration_4 = _tslib.__values(configuration), configuration_4_1 = configuration_4.next(); !configuration_4_1.done; configuration_4_1 = configuration_4.next()) {
var s = configuration_4_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_5_1) {
e_5 = {
error: e_5_1
};
} finally {
try {
if (configuration_4_1 && !configuration_4_1.done && (_a = configuration_4.return)) _a.call(configuration_4);
} finally {
if (e_5) throw e_5.error;
}
}
return adjList;
}
function getValue(rootNode, configuration) {
var config = getConfiguration([rootNode], configuration);
return getValueFromAdj(rootNode, getAdjList(config));
}
function has(iterable, item) {
if (Array.isArray(iterable)) {
return iterable.some(function (member) {
return member === item;
});
}
if (iterable instanceof Set) {
return iterable.has(item);
}
return false; // TODO: fix
}
function nextEvents(configuration) {
return _tslib.__spreadArray([], _tslib.__read(new Set(utils.flatten(_tslib.__spreadArray([], _tslib.__read(configuration.map(function (sn) {
return sn.ownEvents;
})), false)))), false);
}
function isInFinalState(configuration, stateNode) {
if (stateNode.type === 'compound') {
return getChildren(stateNode).some(function (s) {
return s.type === 'final' && has(configuration, s);
});
}
if (stateNode.type === 'parallel') {
return getChildren(stateNode).every(function (sn) {
return isInFinalState(configuration, sn);
});
}
return false;
}
function getMeta(configuration) {
if (configuration === void 0) {
configuration = [];
}
return configuration.reduce(function (acc, stateNode) {
if (stateNode.meta !== undefined) {
acc[stateNode.id] = stateNode.meta;
}
return acc;
}, {});
}
function getTagsFromConfiguration(configuration) {
return new Set(utils.flatten(configuration.map(function (sn) {
return sn.tags;
})));
}
exports.getAdjList = getAdjList;
exports.getAllChildren = getAllChildren;
exports.getAllStateNodes = getAllStateNodes;
exports.getChildren = getChildren;
exports.getConfiguration = getConfiguration;
exports.getMeta = getMeta;
exports.getTagsFromConfiguration = getTagsFromConfiguration;
exports.getValue = getValue;
exports.has = has;
exports.isInFinalState = isInFinalState;
exports.isLeafNode = isLeafNode;
exports.nextEvents = nextEvents;
;