Skip to content

Commit 9fe62f9

Browse files
andrewseguintinayuangao
authored andcommitted
feat(table): add disconnect to data source (#5382)
* feat(data-table): add disconnect to data source * testing * add collectionviewer to disconnect
1 parent 4b98823 commit 9fe62f9

File tree

6 files changed

+33
-2
lines changed

6 files changed

+33
-2
lines changed

src/demo-app/data-table/person-data-source.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class PersonDataSource extends DataSource<any> {
1313

1414
connect(): Observable<UserData[]> {
1515
const displayDataChanges = [
16-
this._paginator.page,
16+
this._paginator.page,
1717
this._sort.mdSortChange,
1818
this._peopleDatabase.dataChange
1919
];
@@ -26,6 +26,10 @@ export class PersonDataSource extends DataSource<any> {
2626
});
2727
}
2828

29+
disconnect() {
30+
// No-op
31+
}
32+
2933
/** Returns a sorted copy of the database data. */
3034
getSortedData(): UserData[] {
3135
const data = this._peopleDatabase.data.slice();

src/lib/core/data-table/data-source.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export interface CollectionViewer {
1414

1515
export abstract class DataSource<T> {
1616
abstract connect(collectionViewer: CollectionViewer): Observable<T[]>;
17+
abstract disconnect(collectionViewer: CollectionViewer): void;
1718
}

src/lib/core/data-table/data-table.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ describe('CdkTable', () => {
9494
});
9595
});
9696

97+
it('should disconnect the data source when table is destroyed', () => {
98+
expect(dataSource.isConnected).toBe(true);
99+
100+
fixture.destroy();
101+
expect(dataSource.isConnected).toBe(false);
102+
});
103+
97104
it('should not clobber an existing table role', () => {
98105
fixture = TestBed.createComponent(CustomRoleCdkTableApp);
99106
fixture.detectChanges();
@@ -301,8 +308,10 @@ describe('CdkTable', () => {
301308
]);
302309

303310
// Add a data source that has initialized data. Expect that the table shows this data.
304-
component.dataSource = new FakeDataSource();
311+
const dynamicDataSource = new FakeDataSource();
312+
component.dataSource = dynamicDataSource;
305313
fixture.detectChanges();
314+
expect(dynamicDataSource.isConnected).toBe(true);
306315

307316
let data = component.dataSource.data;
308317
expect(tableElement).toMatchTableContent([
@@ -315,6 +324,9 @@ describe('CdkTable', () => {
315324
// Remove the data source and check to make sure the table is empty again.
316325
component.dataSource = null;
317326
fixture.detectChanges();
327+
328+
// Expect that the old data source has been disconnected.
329+
expect(dynamicDataSource.isConnected).toBe(false);
318330
expect(tableElement).toMatchTableContent([
319331
['Column A']
320332
]);
@@ -454,6 +466,10 @@ class FakeDataSource extends DataSource<TestData> {
454466
return map.call(combineLatest(streams), ([data]) => data);
455467
}
456468

469+
disconnect() {
470+
this.isConnected = false;
471+
}
472+
457473
addData() {
458474
const nextIndex = this.data.length + 1;
459475

src/lib/core/data-table/data-table.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ export class CdkTable<T> implements CollectionViewer {
184184
ngOnDestroy() {
185185
this._onDestroy.next();
186186
this._onDestroy.complete();
187+
if (this.dataSource) {
188+
this.dataSource.disconnect(this);
189+
}
187190
}
188191

189192
ngOnInit() {
@@ -237,6 +240,10 @@ export class CdkTable<T> implements CollectionViewer {
237240
*/
238241
private _switchDataSource(dataSource: DataSource<T>) {
239242
this._data = [];
243+
244+
if (this._dataSource) {
245+
this.dataSource.disconnect(this);
246+
}
240247
this._dataSource = dataSource;
241248

242249
if (this._isViewInitialized) {

src/lib/sort/sort.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ class FakeDataSource extends DataSource<any> {
203203
connect(collectionViewer: CollectionViewer): Observable<any[]> {
204204
return map.call(collectionViewer.viewChange, () => []);
205205
}
206+
disconnect() {}
206207
}
207208

208209
@Component({

src/lib/table/table.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class FakeDataSource extends DataSource<TestData> {
6868
return this._dataChange;
6969
}
7070

71+
disconnect() {}
72+
7173
addData() {
7274
const nextIndex = this.data.length + 1;
7375

0 commit comments

Comments
 (0)