File tree 2 files changed +22
-2
lines changed
2 files changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ import {TreeControl} from './control/tree-control';
17
17
import { FlatTreeControl } from './control/flat-tree-control' ;
18
18
import { NestedTreeControl } from './control/nested-tree-control' ;
19
19
import { CdkTreeModule } from './index' ;
20
- import { CdkTree } from './tree' ;
20
+ import { CdkTree , CdkTreeNode } from './tree' ;
21
21
import { getTreeControlFunctionsMissingError } from './tree-errors' ;
22
22
23
23
@@ -35,6 +35,20 @@ describe('CdkTree', () => {
35
35
} ) . compileComponents ( ) ;
36
36
}
37
37
38
+ it ( 'should clear out the `mostRecentTreeNode` on destroy' , ( ) => {
39
+ configureCdkTreeTestingModule ( [ SimpleCdkTreeApp ] ) ;
40
+ const fixture = TestBed . createComponent ( SimpleCdkTreeApp ) ;
41
+ fixture . detectChanges ( ) ;
42
+
43
+ // Cast the assertions to a boolean to avoid Jasmine going into an
44
+ // infinite loop when stringifying the object, if the test starts failing.
45
+ expect ( ! ! CdkTreeNode . mostRecentTreeNode ) . toBe ( true ) ;
46
+
47
+ fixture . destroy ( ) ;
48
+
49
+ expect ( ! ! CdkTreeNode . mostRecentTreeNode ) . toBe ( false ) ;
50
+ } ) ;
51
+
38
52
describe ( 'flat tree' , ( ) => {
39
53
describe ( 'should initialize' , ( ) => {
40
54
let fixture : ComponentFixture < SimpleCdkTreeApp > ;
Original file line number Diff line number Diff line change @@ -290,7 +290,7 @@ export class CdkTree<T>
290
290
'class' : 'cdk-tree-node' ,
291
291
} ,
292
292
} )
293
- export class CdkTreeNode < T > implements FocusableOption , OnDestroy {
293
+ export class CdkTreeNode < T > implements FocusableOption , OnDestroy {
294
294
/**
295
295
* The most recently created `CdkTreeNode`. We save it in static variable so we can retrieve it
296
296
* in `CdkTree` and set the data to it.
@@ -328,6 +328,12 @@ export class CdkTreeNode<T> implements FocusableOption, OnDestroy {
328
328
}
329
329
330
330
ngOnDestroy ( ) {
331
+ // If this is the last tree node being destroyed,
332
+ // clear out the reference to avoid leaking memory.
333
+ if ( CdkTreeNode . mostRecentTreeNode === this ) {
334
+ CdkTreeNode . mostRecentTreeNode = null ;
335
+ }
336
+
331
337
this . _destroyed . next ( ) ;
332
338
this . _destroyed . complete ( ) ;
333
339
}
You can’t perform that action at this time.
0 commit comments