From 8eb08667608723b796fbe8edcf7167570c0b7939 Mon Sep 17 00:00:00 2001 From: Luke De Feo Date: Mon, 28 Nov 2022 05:09:20 -0800 Subject: [PATCH] Add parent reference to desktop node Summary: There are some situations where we are doing a traversal of the entire node hierachy looking for something. In some situtations this linear search can be replaced with a walk up the anchestory path making the code easier to understand and more efficient Reviewed By: lblasa Differential Revision: D41548253 fbshipit-source-id: 4fb2141282f1f9663835c3b7812d30dcc59b707e --- desktop/plugins/public/ui-debugger/index.tsx | 18 +++++++++++++++++- desktop/plugins/public/ui-debugger/types.tsx | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/desktop/plugins/public/ui-debugger/index.tsx b/desktop/plugins/public/ui-debugger/index.tsx index 0df383cbc..b7a905504 100644 --- a/desktop/plugins/public/ui-debugger/index.tsx +++ b/desktop/plugins/public/ui-debugger/index.tsx @@ -128,8 +128,9 @@ export function plugin(client: PluginClient) { } event.nodes.forEach((node) => { - draft.nodes.set(node.id, node); + draft.nodes.set(node.id, {...node}); }); + setParentPointers(rootId.get()!!, undefined, draft.nodes); }); uiState.treeState.update((draft) => { @@ -164,6 +165,21 @@ export function plugin(client: PluginClient) { }; } +function setParentPointers( + cur: Id, + parent: Id | undefined, + nodes: Map, +) { + const node = nodes.get(cur); + if (node == null) { + return; + } + node.parent = parent; + node.children.forEach((child) => { + setParentPointers(child, cur, nodes); + }); +} + function collapseinActiveChildren(node: UINode, draft: TreeState) { if (node.activeChild) { const inactiveChildren = node.children.filter( diff --git a/desktop/plugins/public/ui-debugger/types.tsx b/desktop/plugins/public/ui-debugger/types.tsx index d1df5635b..6af2e113d 100644 --- a/desktop/plugins/public/ui-debugger/types.tsx +++ b/desktop/plugins/public/ui-debugger/types.tsx @@ -63,6 +63,7 @@ export type NestedNode = { export type UINode = { id: Id; + parent?: Id; //this attribute doesn't come from the client and is set by the desktop qualifiedName: string; name: string; attributes: Record;