Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Nov 4, 2024
1 parent 39dd2b5 commit 3103a8a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion arches_lingo/src/arches_lingo/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface Scheme {

export interface NodeAndParentInstruction {
node: TreeNode;
parentShouldHideSiblings: boolean;
shouldHideSiblings: boolean;
}

export interface IconLabels {
Expand Down
36 changes: 20 additions & 16 deletions arches_lingo/src/arches_lingo/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function treeFromSchemes(
processItem(child, child.narrower),
);
const parentOfFocusedNode = nodesAndInstructions.find(
(obj) => obj.parentShouldHideSiblings,
(obj) => obj.shouldHideSiblings,
);
if (parentOfFocusedNode) {
childrenAsNodes = [parentOfFocusedNode.node];
Expand All @@ -64,34 +64,38 @@ export function treeFromSchemes(
}

const node: TreeNode = buildNode(item, childrenAsNodes);
let parentShouldHideSiblings = !!parentOfFocusedNode;
if (!parentShouldHideSiblings) {
let shouldHideSiblings = !!parentOfFocusedNode;
if (!shouldHideSiblings) {
const focalNode = node.children!.find(
(child: TreeNode) => child.data.id === focusedNode?.data?.id,
);
if (focalNode) {
node.children = [focalNode];
parentShouldHideSiblings = true;
shouldHideSiblings = true;
}
}
return { node, parentShouldHideSiblings };
return { node, shouldHideSiblings };
}

// Immediately process & return only this scheme if it's focused.
// If this scheme is focused, immediately process and return it.
const focalScheme = schemes.find((sch) => sch.id === focusedNode?.data?.id);
if (focalScheme) {
return [processItem(focalScheme, focalScheme.top_concepts).node];
}

// Otherwise, process all schemes.
const nodesAndInstructions = schemes.map((scheme: Scheme) =>
processItem(scheme, scheme.top_concepts),
);
const focusedChild = nodesAndInstructions.find(
(o) => o.parentShouldHideSiblings,
);
if (focusedChild) {
return [focusedChild.node];
// Otherwise, process schemes until a focused node is found.
const reshapedSchemes = [];
for (const scheme of schemes) {
const { node, shouldHideSiblings } = processItem(
scheme,
scheme.top_concepts,
);
if (shouldHideSiblings) {
return [node];
} else {
reshapedSchemes.push(node);
}
}
return nodesAndInstructions.map((obj) => obj.node);

return reshapedSchemes;
}

0 comments on commit 3103a8a

Please sign in to comment.