Skip to content

fix(cdk/tree): retainining previous objects #30431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/cdk/tree/tree.ts
Original file line number Diff line number Diff line change
@@ -1066,6 +1066,9 @@ export class CdkTree<T, K = T>
// nodes are flat, or if the tree is using a levelAccessor and the nodes are
// nested.
if (this.childrenAccessor && nodeType === 'flat') {
// clear previously generated data so we don't keep end up retaining data overtime causing
// memory leaks.
this._clearPreviousCache();
// This flattens children into a single array.
this._ariaSets.set(null, [...nodes]);
return this._flattenNestedNodesWithExpansion(nodes).pipe(
@@ -1098,6 +1101,9 @@ export class CdkTree<T, K = T>
}),
);
} else {
// clear previously generated data so we don't keep end up retaining data overtime causing
// memory leaks.
this._clearPreviousCache();
// For nested nodes, we still need to perform the node flattening in order
// to maintain our caches for various tree operations.
this._ariaSets.set(null, [...nodes]);
@@ -1125,8 +1131,9 @@ export class CdkTree<T, K = T>
return;
}

this._parents.clear();
this._ariaSets.clear();
// clear previously generated data so we don't keep end up retaining data overtime causing
// memory leaks.
this._clearPreviousCache();

for (let index = 0; index < flattenedNodes.length; index++) {
const dataNode = flattenedNodes[index];
@@ -1163,6 +1170,13 @@ export class CdkTree<T, K = T>
callback(toToggle);
}
}

/** Clears the maps we use to store parents, level & aria-sets in. */
private _clearPreviousCache() {
this._parents.clear();
this._levels.clear();
this._ariaSets.clear();
}
}

/**
Loading
Oops, something went wrong.