@@ -1066,6 +1066,9 @@ export class CdkTree<T, K = T>
1066
1066
// nodes are flat, or if the tree is using a levelAccessor and the nodes are
1067
1067
// nested.
1068
1068
if ( this . childrenAccessor && nodeType === 'flat' ) {
1069
+ // clear previously generated data so we don't keep end up retaining data overtime causing
1070
+ // memory leaks.
1071
+ this . _clearPreviousCache ( ) ;
1069
1072
// This flattens children into a single array.
1070
1073
this . _ariaSets . set ( null , [ ...nodes ] ) ;
1071
1074
return this . _flattenNestedNodesWithExpansion ( nodes ) . pipe (
@@ -1098,6 +1101,9 @@ export class CdkTree<T, K = T>
1098
1101
} ) ,
1099
1102
) ;
1100
1103
} else {
1104
+ // clear previously generated data so we don't keep end up retaining data overtime causing
1105
+ // memory leaks.
1106
+ this . _clearPreviousCache ( ) ;
1101
1107
// For nested nodes, we still need to perform the node flattening in order
1102
1108
// to maintain our caches for various tree operations.
1103
1109
this . _ariaSets . set ( null , [ ...nodes ] ) ;
@@ -1125,8 +1131,9 @@ export class CdkTree<T, K = T>
1125
1131
return ;
1126
1132
}
1127
1133
1128
- this . _parents . clear ( ) ;
1129
- this . _ariaSets . clear ( ) ;
1134
+ // clear previously generated data so we don't keep end up retaining data overtime causing
1135
+ // memory leaks.
1136
+ this . _clearPreviousCache ( ) ;
1130
1137
1131
1138
for ( let index = 0 ; index < flattenedNodes . length ; index ++ ) {
1132
1139
const dataNode = flattenedNodes [ index ] ;
@@ -1163,6 +1170,13 @@ export class CdkTree<T, K = T>
1163
1170
callback ( toToggle ) ;
1164
1171
}
1165
1172
}
1173
+
1174
+ /** Clears the maps we use to store parents, level & aria-sets in. */
1175
+ private _clearPreviousCache ( ) {
1176
+ this . _parents . clear ( ) ;
1177
+ this . _levels . clear ( ) ;
1178
+ this . _ariaSets . clear ( ) ;
1179
+ }
1166
1180
}
1167
1181
1168
1182
/**
0 commit comments