File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -120,11 +120,13 @@ export class Module {
120120 return this . _distance ;
121121 }
122122
123- set distance ( distance : number ) {
123+ public updateDistance ( distance : number , stack : Module [ ] ) {
124124 this . _distance = distance ;
125125 Array . from ( this . _imports )
126- . filter ( module => module && ! module . imports . has ( this ) )
127- . forEach ( module => ( module . distance = distance + 1 ) ) ;
126+ . filter ( module => module && ! stack . includes ( this ) )
127+ . forEach ( module =>
128+ module . updateDistance ( distance + 1 , stack . concat ( this ) ) ,
129+ ) ;
128130 }
129131
130132 public addCoreProviders ( container : NestContainer ) {
@@ -413,10 +415,9 @@ export class Module {
413415
414416 public addRelatedModule ( module : Module ) {
415417 this . _imports . add ( module ) ;
416- module . distance =
417- this . _distance + 1 > module . _distance
418- ? this . _distance + 1
419- : module . _distance ;
418+ if ( this . _distance + 1 > module . _distance ) {
419+ module . updateDistance ( this . _distance + 1 , [ this ] ) ;
420+ }
420421 }
421422
422423 public replace ( toReplace : string | symbol | Type < any > , options : any ) {
Original file line number Diff line number Diff line change @@ -499,7 +499,7 @@ describe('Module', () => {
499499 moduleC = new Module ( ModuleC as any , [ ] , container ) ;
500500 } ) ;
501501
502- it ( 'should get "distance" correct ' , ( ) => {
502+ it ( 'should calculate "distance" properly ' , ( ) => {
503503 moduleA . addRelatedModule ( moduleB ) ;
504504 moduleC . addRelatedModule ( moduleB ) ;
505505 moduleA . addRelatedModule ( moduleC ) ;
@@ -509,11 +509,16 @@ describe('Module', () => {
509509 expect ( moduleC . distance ) . to . be . equal ( 1 ) ;
510510 } ) ;
511511
512- it ( 'should don`t throw exception when circular dependency' , ( ) => {
512+ it ( 'should not throw an exception when circular dependency occurs ' , ( ) => {
513513 expect ( ( ) => {
514514 moduleA . addRelatedModule ( moduleB ) ;
515515 moduleB . addRelatedModule ( moduleA ) ;
516516 } ) . to . not . throw ;
517+ expect ( ( ) => {
518+ moduleA . addRelatedModule ( moduleB ) ;
519+ moduleB . addRelatedModule ( moduleC ) ;
520+ moduleC . addRelatedModule ( moduleA ) ;
521+ } ) . to . not . throw ;
517522 } ) ;
518523 } ) ;
519524} ) ;
You can’t perform that action at this time.
0 commit comments