Skip to content

Commit

Permalink
fix(cdk-tree): fix crash in test cases with max call stack
Browse files Browse the repository at this point in the history
Fix crash in test cases " An error was thrown in afterAll. RangeError: Maximum call stack size exceeded."

example: https://app.circleci.com/pipelines/github/angular/components/58166/workflows/cc9e4f5d-2182-44a5-89a6-56f93e196843/jobs/506030

Fix crash when happened in expandAll and collapseAll methods. Use direct
value of `_flattenedNodes` rather than subscribing to avoid unnecessary
subscription. Using the value directly is safe because `_flattenedNodes`
is a synchronous cache.
  • Loading branch information
zarend committed Sep 27, 2023
1 parent 38e48c7 commit 06ce6a8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
16 changes: 6 additions & 10 deletions src/cdk/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,9 @@ export class CdkTree<T, K = T>
this.treeControl.expandAll();
} else if (this._expansionModel) {
const expansionModel = this._expansionModel;
this._getAllNodes()
.pipe(takeUntil(this._onDestroy))
.subscribe(children => {
expansionModel.select(...children.map(child => this._getExpansionKey(child)));
});
expansionModel.select(
...this._flattenedNodes.value.map(child => this._getExpansionKey(child)),
);
}
}

Expand All @@ -657,11 +655,9 @@ export class CdkTree<T, K = T>
this.treeControl.collapseAll();
} else if (this._expansionModel) {
const expansionModel = this._expansionModel;
this._getAllNodes()
.pipe(takeUntil(this._onDestroy))
.subscribe(children => {
expansionModel.deselect(...children.map(child => this._getExpansionKey(child)));
});
expansionModel.deselect(
...this._flattenedNodes.value.map(child => this._getExpansionKey(child)),
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/material/tree/testing/tree-harness.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ interface ExampleFlatNode {
{{node.name}}
</mat-tree-node>
<!-- This is the tree node template for expandable nodes -->
<mat-tree-node *matTreeNodeDef="let node;when: flatTreeHasChild" matTreeNodePadding>
<mat-tree-node *matTreeNodeDef="let node;when: flatTreeHasChild" matTreeNodePadding isExpandable>
<button matTreeNodeToggle>
Toggle
</button>
Expand All @@ -240,7 +240,7 @@ interface ExampleFlatNode {
{{node.name}}
</mat-tree-node>
<!-- This is the tree node template for expandable nodes -->
<mat-nested-tree-node *matTreeNodeDef="let node; when: nestedTreeHasChild">
<mat-nested-tree-node *matTreeNodeDef="let node; when: nestedTreeHasChild" isExpandable>
<button matTreeNodeToggle>
Toggle
</button>
Expand Down

0 comments on commit 06ce6a8

Please sign in to comment.