Skip to content
Open
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1773629
fix(cdk/tree): add test that demonstrates issue
BobobUnicorn Sep 16, 2024
37f75e9
fix(cdk/tree): signalize render pipeline
BobobUnicorn Sep 16, 2024
c0406a8
fix(cdk/tree): fix lint errors
BobobUnicorn Sep 17, 2024
9dcfa35
fix(cdk/tree): update API goldens
BobobUnicorn Sep 17, 2024
7b14b85
fix(cdk/tree): formatting
BobobUnicorn Sep 17, 2024
a31f246
fix(cdk/tree): update goldens
BobobUnicorn Sep 17, 2024
dda9c79
refactor(cdk/tree): make tests compiled, gets rid of one `detectChanges`
BobobUnicorn Sep 19, 2024
78cc5c0
refactor(cdk/tree): fold more data computation into signals
BobobUnicorn Sep 20, 2024
a9c989e
fix(cdk/tree): remove unnecessary `async`
BobobUnicorn Sep 20, 2024
856fdd1
fix(cdk/tree): revert to ng_test_library; ng_module tests don't work …
BobobUnicorn Oct 3, 2024
b9dab16
fix(cdk/tree): revert signal changes for tests
BobobUnicorn Oct 3, 2024
8ca9455
refactor(cdk/tree): signalify `_flattenedNodes`
BobobUnicorn Oct 3, 2024
d58e9d9
refactor(cdk/tree): signalify the data rendering
BobobUnicorn Oct 3, 2024
312b578
refactor(cdk/tree): signalify the trackBy/expansionKey functions
BobobUnicorn Oct 3, 2024
327d6ad
fix(cdk/tree): remove duplicate `detectChanges`
BobobUnicorn Oct 3, 2024
7358786
fix(cdk/tree): fix lints
BobobUnicorn Oct 3, 2024
68b29d9
fix(cdk/tree): tests
BobobUnicorn Nov 6, 2024
ae9c2f5
fix(cdk/tree): change inputs back to regular @Input
BobobUnicorn Nov 6, 2024
93a959b
fix(cdk/tree): update goldens, fix lints
BobobUnicorn Nov 7, 2024
49330bb
fix(cdk/tree): formatting
BobobUnicorn Nov 7, 2024
cb4edf0
fix(cdk/tree): api goldens, again
BobobUnicorn Nov 7, 2024
be104c2
fix(cdk/tree): lint
BobobUnicorn Nov 18, 2024
ecc4070
fix(cdk/tree): format
BobobUnicorn Nov 18, 2024
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
Prev Previous commit
Next Next commit
refactor(cdk/tree): signalify _flattenedNodes
  • Loading branch information
BobobUnicorn committed Nov 18, 2024
commit 8ca94550771b0b9bc134087a6ce54774d209d45c
51 changes: 31 additions & 20 deletions src/cdk/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
Output,
QueryList,
TrackByFunction,
computed,
signal,
effect,
ViewChild,
Expand All @@ -50,7 +51,7 @@ import {
inject,
booleanAttribute,
} from '@angular/core';
import {toSignal} from '@angular/core/rxjs-interop';
import {toObservable, toSignal} from '@angular/core/rxjs-interop';
import {coerceObservable} from '@angular/cdk/coercion/private';
import {
BehaviorSubject,
Expand Down Expand Up @@ -85,6 +86,7 @@ import {
getTreeMultipleDefaultNodeDefsError,
getTreeNoValidDataSourceError,
} from './tree-errors';
import { NgTemplateOutlet } from '@angular/common';

type RenderingData<T> =
| {
Expand All @@ -105,7 +107,16 @@ type RenderingData<T> =
@Component({
selector: 'cdk-tree',
exportAs: 'cdkTree',
template: `<ng-container cdkTreeNodeOutlet></ng-container>`,
template: `
<ng-template let-nodes>
@for (node of nodes(); track node.key) {
<ng-container
[ngTemplateOutlet]="node.template"
[ngTemplateOutletContext]="node.context" />
}
</ng-template>
<ng-container cdkTreeNodeOutlet></ng-container>
`,
host: {
'class': 'cdk-tree',
'role': 'tree',
Expand All @@ -117,7 +128,8 @@ type RenderingData<T> =
// declared elsewhere, they are checked when their declaration points are checked.
// tslint:disable-next-line:validate-decorators
changeDetection: ChangeDetectionStrategy.Default,
imports: [CdkTreeNodeOutlet],
standalone: true,
imports: [CdkTreeNodeOutlet, NgTemplateOutlet],
})
export class CdkTree<T, K = T>
implements
Expand Down Expand Up @@ -261,13 +273,6 @@ export class CdkTree<T, K = T>
/** Keep track of which nodes are expanded. */
private _expansionModel?: SelectionModel<K>;

/**
* Maintain a synchronous cache of flattened data nodes. This will only be
* populated after initial render, and in certain cases, will be delayed due to
* relying on Observable `getChildren` calls.
*/
private _flattenedNodes: BehaviorSubject<readonly T[]> = new BehaviorSubject<readonly T[]>([]);

/** The automatically determined node type for the tree. */
private _nodeType = new BehaviorSubject<'flat' | 'nested' | null>(null);

Expand Down Expand Up @@ -297,6 +302,18 @@ export class CdkTree<T, K = T>
),
{initialValue: null},
);
/**
* Maintain a synchronous cache of flattened data nodes. This will only be
* populated after initial render, and in certain cases, will be delayed due to
* relying on Observable `getChildren` calls.
*/
private readonly _flattenedNodes = computed(() => {
return this._renderData()?.flattenedNodes ?? [];
});
private readonly _flatNodesObs = toObservable(this._flattenedNodes);
private readonly _renderNodes = computed(() => {
return this._renderData()?.renderNodes ?? [];
});

constructor(...args: unknown[]);
constructor() {
Expand Down Expand Up @@ -412,7 +429,6 @@ export class CdkTree<T, K = T>

// If we're here, then we know what our node type is, and therefore can
// perform our usual rendering pipeline.
this._updateCachedData(data.flattenedNodes);
this.renderNodeChanges(data.renderNodes);
this._updateKeyManagerItems(data.flattenedNodes);
// Explicitly detect the initial set of changes to this component subtree
Expand Down Expand Up @@ -694,7 +710,7 @@ export class CdkTree<T, K = T>
} else if (this._expansionModel) {
const expansionModel = this._expansionModel;
expansionModel.select(
...this._flattenedNodes.value.map(child => this._getExpansionKey(child)),
...this._flattenedNodes().map(child => this._getExpansionKey(child)),
);
}
}
Expand All @@ -706,7 +722,7 @@ export class CdkTree<T, K = T>
} else if (this._expansionModel) {
const expansionModel = this._expansionModel;
expansionModel.deselect(
...this._flattenedNodes.value.map(child => this._getExpansionKey(child)),
...this._flattenedNodes().map(child => this._getExpansionKey(child)),
);
}
}
Expand Down Expand Up @@ -747,15 +763,14 @@ export class CdkTree<T, K = T>
);

if (levelAccessor) {
return combineLatest([isExpanded, this._flattenedNodes]).pipe(
return combineLatest([isExpanded, this._flatNodesObs]).pipe(
map(([expanded, flattenedNodes]) => {
if (!expanded) {
return [];
}
return this._findChildrenByLevel(
levelAccessor,
flattenedNodes,

dataNode,
1,
);
Expand Down Expand Up @@ -898,7 +913,7 @@ export class CdkTree<T, K = T>
if (this.levelAccessor) {
const results = this._findChildrenByLevel(
this.levelAccessor,
this._flattenedNodes.value,
this._flattenedNodes(),
dataNode,
Infinity,
);
Expand Down Expand Up @@ -1103,10 +1118,6 @@ export class CdkTree<T, K = T>
}
}

private _updateCachedData(flattenedNodes: readonly T[]) {
this._flattenedNodes.next(flattenedNodes);
}

private _updateKeyManagerItems(flattenedNodes: readonly T[]) {
this._keyManagerNodes.next(flattenedNodes);
}
Expand Down