Skip to content

Commit

Permalink
improve performance by using stack instead of recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Oct 4, 2024
1 parent 2fbb611 commit 8b5abbe
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { LinkNode } from ".";
import type { NavigationNode } from "./NavigationNode";
import { isApiLeaf, type NavigationNodeApiLeaf } from "./NavigationNodeApiLeaf";
import { isMarkdownLeaf, type NavigationNodeMarkdownLeaf } from "./NavigationNodePageLeaf";

/**
* A navigation node that represents a leaf in the navigation tree (i.e. a node that does not have children)
*/
export type NavigationNodeLeaf = NavigationNodeApiLeaf | NavigationNodeMarkdownLeaf;
export type NavigationNodeLeaf = NavigationNodeApiLeaf | NavigationNodeMarkdownLeaf | LinkNode;

export function isLeaf(node: NavigationNode): node is NavigationNodeLeaf {
return isApiLeaf(node) || isMarkdownLeaf(node);
return isApiLeaf(node) || isMarkdownLeaf(node) || node.type === "link";
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class NavigationConfigConverter {
};

// tag all children of hidden nodes as hidden
FernNavigation.V1.traverseNavigation(toRet, (node, _index, parents) => {
FernNavigation.V1.traverseDF(toRet, (node, parents) => {
if (
FernNavigation.V1.hasMetadata(node) &&
parents.some((p) => FernNavigation.V1.hasMetadata(p) && p.hidden === true)
Expand Down
37 changes: 37 additions & 0 deletions packages/fdr-sdk/src/navigation/versions/v1/getChildren.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { UnreachableCaseError } from "ts-essentials";
import { NavigationNode } from "./NavigationNode";
import { isLeaf } from "./NavigationNodeLeaf";

export function getChildren(node: NavigationNode): readonly NavigationNode[] {
if (isLeaf(node)) {
return [];
}

switch (node.type) {
case "apiPackage":
case "changelog":
case "changelogMonth":
case "changelogYear":
case "section":
case "sidebarGroup":
case "sidebarRoot":
case "tabbed":
case "versioned":
return node.children;
case "product":
case "root":
case "tab":
return [node.child];
case "apiReference":
return [...node.children, ...(node.changelog ? [node.changelog] : [])];
case "endpointPair":
return [node.nonStream, node.stream];
case "productgroup":
return [...(node.landingPage ? [node.landingPage] : []), ...node.children];
case "unversioned":
case "version":
return [...(node.landingPage ? [node.landingPage] : []), node.child];
default:
throw new UnreachableCaseError(node);
}
}
12 changes: 6 additions & 6 deletions packages/fdr-sdk/src/navigation/versions/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
export * from "../../../client/generated/api/resources/commons";
export * from "../../../client/generated/api/resources/navigation/resources/v1/types";
export * from "./convertAvailability";
export * from "./converters/ApiReferenceNavigationConverter";
export * from "./converters/toRootNode";
export * from "./followRedirect";
export * from "./getPageId";
export * from "./NavigationNode";
export * from "./NavigationNodeApiLeaf";
export * from "./NavigationNodeLeaf";
Expand All @@ -16,6 +11,11 @@ export * from "./NavigationNodeSection";
export * from "./NavigationNodeSectionOverview";
export * from "./NavigationNodeWithMetadata";
export * from "./NavigationNodeWithRedirect";
export * from "./convertAvailability";
export * from "./converters/ApiReferenceNavigationConverter";
export * from "./converters/toRootNode";
export * from "./followRedirect";
export * from "./getPageId";
export * from "./slugjoin";
export * from "./toDefaultSlug";
export * from "./traverseNavigation";
export * from "./traverseDF";
8 changes: 8 additions & 0 deletions packages/fdr-sdk/src/navigation/versions/v1/traverseDF.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { dfs } from "../../../utils/traversers/dfs";
import { TraverserVisit } from "../../../utils/traversers/types";
import { NavigationNode } from "./NavigationNode";
import { getChildren } from "./getChildren";

export function traverseDF(node: NavigationNode, visit: TraverserVisit<NavigationNode>): void {
return dfs(node, visit, getChildren);
}
107 changes: 0 additions & 107 deletions packages/fdr-sdk/src/navigation/versions/v1/traverseNavigation.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export class AlgoliaSearchRecordGenerator {
} satisfies Algolia.AlgoliaRecordVersionV3)
: undefined;

function toBreadcrumbs(parents: FernNavigation.V1.NavigationNode[]): string[] {
function toBreadcrumbs(parents: readonly FernNavigation.V1.NavigationNode[]): string[] {
return [
...breadcrumbs,
...parents
Expand All @@ -268,7 +268,7 @@ export class AlgoliaSearchRecordGenerator {
];
}

FernNavigation.V1.traverseNavigation(root, (node, _index, parents) => {
FernNavigation.V1.traverseDF(root, (node, parents) => {
if (!FernNavigation.V1.hasMetadata(node)) {
return;
}
Expand Down Expand Up @@ -698,7 +698,7 @@ export class AlgoliaSearchRecordGenerator {
}
: undefined;

function toBreadcrumbs(parents: FernNavigation.V1.NavigationNode[]): string[] {
function toBreadcrumbs(parents: readonly FernNavigation.V1.NavigationNode[]): string[] {
return [
...breadcrumbs,
...parents
Expand All @@ -714,7 +714,7 @@ export class AlgoliaSearchRecordGenerator {
];
}

FernNavigation.V1.traverseNavigation(root, (node, _index, parents) => {
FernNavigation.V1.traverseDF(root, (node, parents) => {
if (!FernNavigation.V1.hasMetadata(node)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ export class AlgoliaSearchRecordGeneratorV2 extends AlgoliaSearchRecordGenerator
} satisfies Algolia.AlgoliaRecordVersionV3)
: undefined;

FernNavigation.V1.traverseNavigation(root, (node, _index, parents) => {
FernNavigation.V1.traverseDF(root, (node, parents) => {
if (!FernNavigation.V1.hasMetadata(node)) {
return;
}
Expand Down Expand Up @@ -1359,7 +1359,7 @@ export class AlgoliaSearchRecordGeneratorV2 extends AlgoliaSearchRecordGenerator
slug: part.urlSlug,
}));

FernNavigation.V1.traverseNavigation(root, (node, _index, parents) => {
FernNavigation.V1.traverseDF(root, (node) => {
if (!FernNavigation.V1.hasMetadata(node)) {
return;
}
Expand Down Expand Up @@ -1449,7 +1449,7 @@ function toBreadcrumbs(
title: string;
slug: string;
}[],
parents: FernNavigation.V1.NavigationNode[],
parents: readonly FernNavigation.V1.NavigationNode[],
): BreadcrumbsInfo[] {
return [
...breadcrumbs,
Expand Down

0 comments on commit 8b5abbe

Please sign in to comment.