From 8c75b370c44c147d8f7603c421980c9d0a25c370 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Mon, 17 Jul 2017 13:28:41 +0300 Subject: [PATCH] perf(table): cell references not being cleaned up on destroy Fixes the table cell references not being cleaned up when the table is destroyed, causing them to stay in memory forever. --- src/cdk/table/table.spec.ts | 13 +++++++++++++ src/cdk/table/table.ts | 7 +++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/cdk/table/table.spec.ts b/src/cdk/table/table.spec.ts index b69724ec3c48..9850d8cd6f06 100644 --- a/src/cdk/table/table.spec.ts +++ b/src/cdk/table/table.spec.ts @@ -155,6 +155,19 @@ describe('CdkTable', () => { expect(changedRows[2].getAttribute('initialIndex')).toBe(null); }); + it('should clear the row view containers on destroy', () => { + const rowPlaceholder = fixture.componentInstance.table._rowPlaceholder.viewContainer; + const headerPlaceholder = fixture.componentInstance.table._headerRowPlaceholder.viewContainer; + + spyOn(rowPlaceholder, 'clear').and.callThrough(); + spyOn(headerPlaceholder, 'clear').and.callThrough(); + + fixture.destroy(); + + expect(rowPlaceholder.clear).toHaveBeenCalled(); + expect(headerPlaceholder.clear).toHaveBeenCalled(); + }); + describe('with trackBy', () => { let trackByComponent: TrackByCdkTableApp; diff --git a/src/cdk/table/table.ts b/src/cdk/table/table.ts index 592e56d63fa6..89b328857f83 100644 --- a/src/cdk/table/table.ts +++ b/src/cdk/table/table.ts @@ -27,7 +27,7 @@ import { TrackByFunction, ViewChild, ViewContainerRef, - ViewEncapsulation + ViewEncapsulation, } from '@angular/core'; import {CollectionViewer, DataSource} from './data-source'; import {CdkCellOutlet, CdkCellOutletRowContext, CdkHeaderRowDef, CdkRowDef} from './row'; @@ -175,8 +175,11 @@ export class CdkTable implements CollectionViewer { } ngOnDestroy() { + this._rowPlaceholder.viewContainer.clear(); + this._headerRowPlaceholder.viewContainer.clear(); this._onDestroy.next(); this._onDestroy.complete(); + if (this.dataSource) { this.dataSource.disconnect(this); } @@ -343,7 +346,7 @@ export class CdkTable implements CollectionViewer { viewRef.context.first = index === 0; viewRef.context.last = index === count - 1; viewRef.context.even = index % 2 === 0; - viewRef.context.odd = index % 2 !== 0; + viewRef.context.odd = !viewRef.context.even; } }