Prevent stack overflow in cyclical graphs

Summary: Logview reported "Error: Maximum call stack size exceeded" for "getElementLeaves". Apparently, there was a cycle in the graph that caused it. The new implementation should ignore cycles.

Reviewed By: lblasa

Differential Revision: D43976359

fbshipit-source-id: bb5218a3b29706146501241492ee079773d5abc3
This commit is contained in:
Andrey Goncharov
2023-03-10 08:33:19 -08:00
committed by Facebook GitHub Bot
parent 3d70020f40
commit ccba2fb73e

View File

@@ -341,20 +341,29 @@ export default class Inspector extends Component<Props, State> {
}
getElementLeaves(tree: ElementSelectorNode): Array<ElementID> {
return tree
? Object.entries(tree).reduce(
(
currLeafNode: Array<ElementID>,
[id, children]: [ElementID, ElementSelectorNode],
): Array<ElementID> =>
currLeafNode.concat(
Object.keys(children).length > 0
? this.getElementLeaves(children)
: [id],
),
[],
)
: [];
if (!tree) {
return [];
}
const leavesSet = new Set<ElementID>();
const treeIteratorStack: [ElementID, ElementSelectorNode][] = [
...Object.entries(tree),
];
while (treeIteratorStack.length) {
const [id, children] = treeIteratorStack.pop()!;
if (leavesSet.has(id)) {
continue;
}
if (Object.keys(children).length) {
treeIteratorStack.push(...Object.entries(children));
} else {
leavesSet.add(id);
}
}
return [...leavesSet];
}
/// Return path from given tree structure and id if id is not null; otherwise return any path