@@ -3,23 +3,15 @@ import { TreeNode } from './tree-node';
3
3
4
4
export class TopologyTree {
5
5
private root : TreeNode < Module > ;
6
- private links : Map <
7
- Module ,
8
- {
9
- node : TreeNode < Module > ;
10
- depth : number ;
11
- }
12
- > = new Map ( ) ;
6
+ private links : Map < Module , TreeNode < Module > > = new Map ( ) ;
13
7
14
- static from ( root : Module ) {
15
- const tree = new TopologyTree ( ) ;
16
- tree . root = new TreeNode < Module > ( {
17
- value : root ,
8
+ constructor ( moduleRef : Module ) {
9
+ this . root = new TreeNode < Module > ( {
10
+ value : moduleRef ,
18
11
parent : null ,
19
12
} ) ;
20
-
21
- tree . traverseAndCloneTree ( tree . root ) ;
22
- return tree ;
13
+ this . links . set ( moduleRef , this . root ) ;
14
+ this . traverseAndCloneTree ( this . root ) ;
23
15
}
24
16
25
17
public walk ( callback : ( value : Module , depth : number ) => void ) {
@@ -37,9 +29,9 @@ export class TopologyTree {
37
29
}
38
30
if ( this . links . has ( child ) ) {
39
31
const existingSubtree = this . links . get ( child ) ! ;
40
- if ( existingSubtree . depth < depth ) {
41
- existingSubtree . node . relink ( node ) ;
42
- existingSubtree . depth = depth ;
32
+ const existingDepth = existingSubtree . getDepth ( { stopOn : node . value } ) ;
33
+ if ( existingDepth < depth ) {
34
+ existingSubtree . relink ( node ) ;
43
35
}
44
36
return ;
45
37
}
@@ -49,10 +41,8 @@ export class TopologyTree {
49
41
parent : node ,
50
42
} ) ;
51
43
node . addChild ( childNode ) ;
52
- this . links . set ( child , {
53
- node : childNode ,
54
- depth,
55
- } ) ;
44
+
45
+ this . links . set ( child , childNode ) ;
56
46
57
47
this . traverseAndCloneTree ( childNode , depth + 1 ) ;
58
48
} ) ;
0 commit comments