Skip to content

Commit

Permalink
refactor: dont track node depth
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Feb 10, 2025
1 parent c743c74 commit 3179609
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
32 changes: 11 additions & 21 deletions packages/core/injector/topology-tree/topology-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,15 @@ import { TreeNode } from './tree-node';

export class TopologyTree {
private root: TreeNode<Module>;
private links: Map<
Module,
{
node: TreeNode<Module>;
depth: number;
}
> = new Map();
private links: Map<Module, TreeNode<Module>> = new Map();

static from(root: Module) {
const tree = new TopologyTree();
tree.root = new TreeNode<Module>({
value: root,
constructor(moduleRef: Module) {
this.root = new TreeNode<Module>({
value: moduleRef,
parent: null,
});

tree.traverseAndCloneTree(tree.root);
return tree;
this.links.set(moduleRef, this.root);
this.traverseAndCloneTree(this.root);
}

public walk(callback: (value: Module, depth: number) => void) {
Expand All @@ -37,9 +29,9 @@ export class TopologyTree {
}
if (this.links.has(child)) {
const existingSubtree = this.links.get(child)!;
if (existingSubtree.depth < depth) {
existingSubtree.node.relink(node);
existingSubtree.depth = depth;
const existingDepth = existingSubtree.getDepth({ stopOn: node.value });
if (existingDepth < depth) {
existingSubtree.relink(node);
}
return;
}
Expand All @@ -49,10 +41,8 @@ export class TopologyTree {
parent: node,
});
node.addChild(childNode);
this.links.set(child, {
node: childNode,
depth,
});

this.links.set(child, childNode);

this.traverseAndCloneTree(childNode, depth + 1);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ export class DependenciesScanner {
const rootModule = modulesGenerator.next().value!;

// Convert modules to an acyclic connected graph
const tree = TopologyTree.from(rootModule);
const tree = new TopologyTree(rootModule);
tree.walk((moduleRef, depth) => {
if (moduleRef.isGlobal) {
return;
Expand Down

0 comments on commit 3179609

Please sign in to comment.