@@ -25,6 +25,7 @@ import {
25
25
import { BehaviorSubject } from 'rxjs/BehaviorSubject' ;
26
26
import { takeUntil } from 'rxjs/operators/takeUntil' ;
27
27
import { Subject } from 'rxjs/Subject' ;
28
+ import { Subscription } from 'rxjs/Subscription' ;
28
29
import { CdkTreeNodeDef , CdkTreeNode , CdkTreeNodeOutletContext } from './node' ;
29
30
import { CdkTreeNodeOutlet } from './outlet' ;
30
31
import { TreeControl } from './control/tree-control' ;
@@ -65,6 +66,9 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {
65
66
/** Stores the node definition that does not have a when predicate. */
66
67
private _defaultNodeDef : CdkTreeNodeDef < T > | null ;
67
68
69
+ /** Data subscription */
70
+ private _dataSubscription : Subscription | null ;
71
+
68
72
/**
69
73
* Provides a stream containing the latest data array to render. Influenced by the tree's
70
74
* stream of view window (what dataNodes are currently on screen).
@@ -118,14 +122,19 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {
118
122
if ( this . dataSource ) {
119
123
this . dataSource . disconnect ( this ) ;
120
124
}
125
+
126
+ if ( this . _dataSubscription ) {
127
+ this . _dataSubscription . unsubscribe ( ) ;
128
+ this . _dataSubscription = null ;
129
+ }
121
130
}
122
131
123
132
ngAfterContentChecked ( ) {
124
133
const defaultNodeDefs = this . _nodeDefs . filter ( def => ! def . when ) ;
125
134
if ( defaultNodeDefs . length > 1 ) { throw getTreeMultipleDefaultNodeDefsError ( ) ; }
126
135
this . _defaultNodeDef = defaultNodeDefs [ 0 ] ;
127
136
128
- if ( this . dataSource ) {
137
+ if ( this . dataSource && this . _nodeDefs && ! this . _dataSubscription ) {
129
138
this . _observeRenderChanges ( ) ;
130
139
}
131
140
}
@@ -145,6 +154,11 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {
145
154
this . dataSource . disconnect ( this ) ;
146
155
}
147
156
157
+ if ( this . _dataSubscription ) {
158
+ this . _dataSubscription . unsubscribe ( ) ;
159
+ this . _dataSubscription = null ;
160
+ }
161
+
148
162
// Remove the all dataNodes if there is now no data source
149
163
if ( ! dataSource ) {
150
164
this . _nodeOutlet . viewContainer . clear ( ) ;
@@ -155,7 +169,7 @@ export class CdkTree<T> implements CollectionViewer, OnInit, OnDestroy {
155
169
156
170
/** Set up a subscription for the data provided by the data source. */
157
171
private _observeRenderChanges ( ) {
158
- this . dataSource . connect ( this ) . pipe ( takeUntil ( this . _onDestroy ) )
172
+ this . _dataSubscription = this . dataSource . connect ( this ) . pipe ( takeUntil ( this . _onDestroy ) )
159
173
. subscribe ( data => {
160
174
this . _data = data ;
161
175
this . _renderNodeChanges ( data ) ;
0 commit comments