Skip to content

feat(table): add disconnect to data source #5382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion src/demo-app/data-table/person-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class PersonDataSource extends DataSource<any> {

connect(): Observable<UserData[]> {
const displayDataChanges = [
this._paginator.page,
this._paginator.page,
this._sort.mdSortChange,
this._peopleDatabase.dataChange
];
Expand All @@ -26,6 +26,10 @@ export class PersonDataSource extends DataSource<any> {
});
}

disconnect() {
// No-op
}

/** Returns a sorted copy of the database data. */
getSortedData(): UserData[] {
const data = this._peopleDatabase.data.slice();
Expand Down
1 change: 1 addition & 0 deletions src/lib/core/data-table/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export interface CollectionViewer {

export abstract class DataSource<T> {
abstract connect(collectionViewer: CollectionViewer): Observable<T[]>;
abstract disconnect(collectionViewer: CollectionViewer): void;
}
18 changes: 17 additions & 1 deletion src/lib/core/data-table/data-table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ describe('CdkTable', () => {
});
});

it('should disconnect the data source when table is destroyed', () => {
expect(dataSource.isConnected).toBe(true);

fixture.destroy();
expect(dataSource.isConnected).toBe(false);
});

it('should not clobber an existing table role', () => {
fixture = TestBed.createComponent(CustomRoleCdkTableApp);
fixture.detectChanges();
Expand Down Expand Up @@ -301,8 +308,10 @@ describe('CdkTable', () => {
]);

// Add a data source that has initialized data. Expect that the table shows this data.
component.dataSource = new FakeDataSource();
const dynamicDataSource = new FakeDataSource();
component.dataSource = dynamicDataSource;
fixture.detectChanges();
expect(dynamicDataSource.isConnected).toBe(true);

let data = component.dataSource.data;
expect(tableElement).toMatchTableContent([
Expand All @@ -315,6 +324,9 @@ describe('CdkTable', () => {
// Remove the data source and check to make sure the table is empty again.
component.dataSource = null;
fixture.detectChanges();

// Expect that the old data source has been disconnected.
expect(dynamicDataSource.isConnected).toBe(false);
expect(tableElement).toMatchTableContent([
['Column A']
]);
Expand Down Expand Up @@ -454,6 +466,10 @@ class FakeDataSource extends DataSource<TestData> {
return map.call(combineLatest(streams), ([data]) => data);
}

disconnect() {
this.isConnected = false;
}

addData() {
const nextIndex = this.data.length + 1;

Expand Down
7 changes: 7 additions & 0 deletions src/lib/core/data-table/data-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ export class CdkTable<T> implements CollectionViewer {
ngOnDestroy() {
this._onDestroy.next();
this._onDestroy.complete();
if (this.dataSource) {
this.dataSource.disconnect(this);
}
}

ngOnInit() {
Expand Down Expand Up @@ -237,6 +240,10 @@ export class CdkTable<T> implements CollectionViewer {
*/
private _switchDataSource(dataSource: DataSource<T>) {
this._data = [];

if (this._dataSource) {
this.dataSource.disconnect(this);
}
this._dataSource = dataSource;

if (this._isViewInitialized) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/sort/sort.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ class FakeDataSource extends DataSource<any> {
connect(collectionViewer: CollectionViewer): Observable<any[]> {
return map.call(collectionViewer.viewChange, () => []);
}
disconnect() {}
}

@Component({
Expand Down
2 changes: 2 additions & 0 deletions src/lib/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class FakeDataSource extends DataSource<TestData> {
return this._dataChange;
}

disconnect() {}

addData() {
const nextIndex = this.data.length + 1;

Expand Down