Skip to content

Commit 9e0de60

Browse files
authored
fix(tree): Fix flat mat tree data source (#9556)
1 parent b765c35 commit 9e0de60

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/cdk-experimental/tree/tree.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
2626
import {takeUntil} from 'rxjs/operators/takeUntil';
2727
import {Subject} from 'rxjs/Subject';
28+
import {Subscription} from 'rxjs/Subscription';
2829
import {CdkTreeNodeDef, CdkTreeNode, CdkTreeNodeOutletContext} from './node';
2930
import {CdkTreeNodeOutlet} from './outlet';
3031
import {TreeControl} from './control/tree-control';
@@ -65,6 +66,9 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {
6566
/** Stores the node definition that does not have a when predicate. */
6667
private _defaultNodeDef: CdkTreeNodeDef<T> | null;
6768

69+
/** Data subscription */
70+
private _dataSubscription : Subscription | null;
71+
6872
/**
6973
* Provides a stream containing the latest data array to render. Influenced by the tree's
7074
* stream of view window (what dataNodes are currently on screen).
@@ -118,14 +122,19 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {
118122
if (this.dataSource) {
119123
this.dataSource.disconnect(this);
120124
}
125+
126+
if (this._dataSubscription) {
127+
this._dataSubscription.unsubscribe();
128+
this._dataSubscription = null;
129+
}
121130
}
122131

123132
ngAfterContentChecked() {
124133
const defaultNodeDefs = this._nodeDefs.filter(def => !def.when);
125134
if (defaultNodeDefs.length > 1) { throw getTreeMultipleDefaultNodeDefsError(); }
126135
this._defaultNodeDef = defaultNodeDefs[0];
127136

128-
if (this.dataSource) {
137+
if (this.dataSource && this._nodeDefs && !this._dataSubscription) {
129138
this._observeRenderChanges();
130139
}
131140
}
@@ -145,6 +154,11 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {
145154
this.dataSource.disconnect(this);
146155
}
147156

157+
if (this._dataSubscription) {
158+
this._dataSubscription.unsubscribe();
159+
this._dataSubscription = null;
160+
}
161+
148162
// Remove the all dataNodes if there is now no data source
149163
if (!dataSource) {
150164
this._nodeOutlet.viewContainer.clear();
@@ -155,7 +169,7 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {
155169

156170
/** Set up a subscription for the data provided by the data source. */
157171
private _observeRenderChanges() {
158-
this.dataSource.connect(this).pipe(takeUntil(this._onDestroy))
172+
this._dataSubscription = this.dataSource.connect(this).pipe(takeUntil(this._onDestroy))
159173
.subscribe(data => {
160174
this._data = data;
161175
this._renderNodeChanges(data);

src/material-experimental/tree/data-source/flat-data-source.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,20 @@ export class MatTreeFlatDataSource<T, F> implements DataSource<F> {
136136
}
137137

138138
connect(collectionViewer: CollectionViewer): Observable<F[]> {
139-
return merge([
139+
const changes = [
140140
collectionViewer.viewChange,
141-
this.treeControl.expansionModel.onChange,
142-
this._flattenedData])
143-
.pipe(map(() => {
144-
this._expandedData.next(
145-
this.treeFlattener.expandFlattenedNodes(this._flattenedData.value, this.treeControl));
146-
return this._expandedData.value;
147-
}));
141+
this.treeControl.expansionModel.onChange!,
142+
this._flattenedData
143+
];
144+
return merge(...changes).pipe(map(() => {
145+
this._expandedData.next(
146+
this.treeFlattener.expandFlattenedNodes(this._flattenedData.value, this.treeControl));
147+
return this._expandedData.value;
148+
}));
148149
}
149150

150151
disconnect() {
152+
// no op
151153
}
152154
}
153155

0 commit comments

Comments
 (0)