File tree 2 files changed +45
-1
lines changed
packages/core/injector/topology-tree
2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,11 @@ export class TopologyTree {
29
29
}
30
30
if ( this . links . has ( child ) ) {
31
31
const existingSubtree = this . links . get ( child ) ! ;
32
- const existingDepth = existingSubtree . getDepth ( { stopOn : node . value } ) ;
32
+
33
+ if ( node . hasCycleWith ( child ) ) {
34
+ return ;
35
+ }
36
+ const existingDepth = existingSubtree . getDepth ( ) ;
33
37
if ( existingDepth < depth ) {
34
38
existingSubtree . relink ( node ) ;
35
39
}
Original file line number Diff line number Diff line change @@ -17,7 +17,47 @@ export class TreeNode<T> {
17
17
}
18
18
19
19
relink ( parent : TreeNode < T > ) {
20
+ this . parent ?. removeChild ( this ) ;
21
+
20
22
this . parent = parent ;
21
23
this . parent . addChild ( this ) ;
22
24
}
25
+
26
+ getDepth ( ) {
27
+ const visited = new Set < TreeNode < T > > ( ) ;
28
+
29
+ let depth = 0 ;
30
+ let current : TreeNode < T > | null = this ;
31
+
32
+ while ( current ) {
33
+ depth ++ ;
34
+ current = current . parent ;
35
+
36
+ // Stop on cycle
37
+ if ( visited . has ( current ! ) ) {
38
+ return - 1 ;
39
+ }
40
+ visited . add ( current ! ) ;
41
+ }
42
+ return depth ;
43
+ }
44
+
45
+ hasCycleWith ( target : T ) {
46
+ const visited = new Set < TreeNode < T > > ( ) ;
47
+
48
+ let current : TreeNode < T > | null = this ;
49
+
50
+ while ( current ) {
51
+ if ( current . value === target ) {
52
+ return true ;
53
+ }
54
+ current = current . parent ;
55
+
56
+ if ( visited . has ( current ! ) ) {
57
+ return false ;
58
+ }
59
+ visited . add ( current ! ) ;
60
+ }
61
+ return false ;
62
+ }
23
63
}
You can’t perform that action at this time.
0 commit comments