Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function getSimplePathsAsArray<
TContext = DefaultContext,
TEvent extends EventObject = EventObject
>(
machine: StateNode,
options?: ValueAdjMapOptions
): Array> {
const result = getSimplePaths(machine, options);
return keys(result).map(key => result[key]);
}
state: stateMap.get(fromStateSerial)!,
event: deserializeEventString(subEvent)
});
util(nextSegment.state, toStateSerial);
}
}
}
path.pop();
visited.delete(fromStateSerial);
}
const initialStateSerial = stateSerializer(machine.initialState);
stateMap.set(initialStateSerial, machine.initialState);
for (const nextStateSerial of keys(adjacency)) {
util(machine.initialState, nextStateSerial);
}
return paths;
}
visited.add(fromStateSerial);
if (fromStateSerial === toStateSerial) {
if (!paths[toStateSerial]) {
paths[toStateSerial] = {
state: stateMap.get(toStateSerial)!,
paths: []
};
}
paths[toStateSerial].paths.push({
state: fromState,
weight: path.length,
segments: [...path]
});
} else {
for (const subEvent of keys(adjacency[fromStateSerial])) {
const nextSegment = adjacency[fromStateSerial][subEvent];
if (!nextSegment) {
continue;
}
const nextStateSerial = stateSerializer(nextSegment.state);
stateMap.set(nextStateSerial, nextSegment.state);
if (!visited.has(nextStateSerial)) {
path.push({
state: stateMap.get(fromStateSerial)!,
event: deserializeEventString(subEvent)
});
util(nextSegment.state, toStateSerial);
}
export function getStateNodes(
stateNode: StateNode | StateMachine
): StateNode[] {
const { states } = stateNode;
const nodes = keys(states).reduce((accNodes: StateNode[], stateKey) => {
const childStateNode = states[stateKey];
const childStateNodes = getStateNodes(childStateNode);
accNodes.push(childStateNode, ...childStateNodes);
return accNodes;
}, []);
return nodes;
}
>();
const stateMap = new Map>();
const initialVertex = optionsWithDefaults.stateSerializer(
machine.initialState
);
stateMap.set(initialVertex, machine.initialState);
weightMap.set(initialVertex, [0, undefined, undefined]);
const unvisited = new Set();
const visited = new Set();
unvisited.add(initialVertex);
while (unvisited.size > 0) {
for (const vertex of unvisited) {
const [weight] = weightMap.get(vertex)!;
for (const event of keys(adjacency[vertex])) {
const nextSegment = adjacency[vertex][event];
const nextVertex = optionsWithDefaults.stateSerializer(
nextSegment.state
);
stateMap.set(nextVertex, nextSegment.state);
if (!weightMap.has(nextVertex)) {
weightMap.set(nextVertex, [weight + 1, vertex, event]);
} else {
const [nextWeight] = weightMap.get(nextVertex)!;
if (nextWeight > weight + 1) {
weightMap.set(nextVertex, [weight + 1, vertex, event]);
}
}
if (!visited.has(nextVertex)) {
unvisited.add(nextVertex);
}