From 6d895e78c0538d142535b4ebedda88e1cc2aa91e Mon Sep 17 00:00:00 2001 From: PavlovVasil Date: Fri, 23 Nov 2018 12:09:17 +0200 Subject: [PATCH] fix(igxGrid): preventing a potential memory leak #3033 --- .../src/lib/grids/grid-base.component.ts | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/projects/igniteui-angular/src/lib/grids/grid-base.component.ts b/projects/igniteui-angular/src/lib/grids/grid-base.component.ts index ac37956d5bc..7e120591540 100644 --- a/projects/igniteui-angular/src/lib/grids/grid-base.component.ts +++ b/projects/igniteui-angular/src/lib/grids/grid-base.component.ts @@ -1988,6 +1988,22 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements * @hidden */ protected _columnPinning = false; + /** + * @hidden + */ + protected __keydownListener = null; + /** + * @hidden + */ + protected __vScrollListener = null; + /** + * @hidden + */ + protected __hScrollListener = null; + /** + * @hidden + */ + protected _wheelListener = null; protected _allowFiltering = false; private _filteredData = null; private resizeHandler; @@ -2163,7 +2179,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements public ngAfterViewInit() { this.zone.runOutsideAngular(() => { this.document.defaultView.addEventListener('resize', this.resizeHandler); - this.nativeElement.addEventListener('keydown', this.keydownHandler.bind(this)); + this.__keydownListener = this.nativeElement.addEventListener('keydown', this.keydownHandler.bind(this)); }); this.calculateGridWidth(); this.initPinning(); @@ -2209,11 +2225,15 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements ); this.zone.runOutsideAngular(() => - this.verticalScrollContainer.getVerticalScroll().addEventListener('scroll', this.verticalScrollHandler.bind(this)) + this.__vScrollListener = this.verticalScrollContainer + .getVerticalScroll() + .addEventListener('scroll', this.verticalScrollHandler.bind(this)) ); this.zone.runOutsideAngular(() => - this.parentVirtDir.getHorizontalScroll().addEventListener('scroll', this.horizontalScrollHandler.bind(this)) + this.__hScrollListener = this.parentVirtDir + .getHorizontalScroll() + .addEventListener('scroll', this.horizontalScrollHandler.bind(this)) ); this._horizontalForOfs = this._dataRowList.map(row => row.virtDirRow); const vertScrDC = this.verticalScrollContainer.dc.instance._viewContainer.element.nativeElement; @@ -2226,9 +2246,9 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements public ngOnDestroy() { this.zone.runOutsideAngular(() => { this.document.defaultView.removeEventListener('resize', this.resizeHandler); - this.nativeElement.removeEventListener('keydown', this.keydownHandler); - this.verticalScrollContainer.getVerticalScroll().removeEventListener('scroll', this.verticalScrollHandler); - this.parentVirtDir.getHorizontalScroll().removeEventListener('scroll', this.horizontalScrollHandler); + this.nativeElement.removeEventListener('keydown', this.__keydownListener); + this.verticalScrollContainer.getVerticalScroll().removeEventListener('scroll', this.__vScrollListener); + this.parentVirtDir.getHorizontalScroll().removeEventListener('scroll', this.__hScrollListener); const vertScrDC = this.verticalScrollContainer.dc.instance._viewContainer.element.nativeElement; vertScrDC.removeEventListener('scroll', (evt) => { this.scrollHandler(evt); }); }); @@ -4341,7 +4361,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements this.configureRowEditingOverlay(cell.rowID); this.rowEditingOverlay.open(this.rowEditSettings); this.rowEditPositioningStrategy.isTopInitialPosition = this.rowEditPositioningStrategy.isTop; - this.rowEditingOverlay.element.addEventListener('wheel', this.rowEditingWheelHandler.bind(this)); + this._wheelListener = this.rowEditingOverlay.element.addEventListener('wheel', this.rowEditingWheelHandler.bind(this)); } /** @@ -4349,7 +4369,7 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements */ public closeRowEditingOverlay() { this.gridAPI.set_edit_row_state(this.id, null); - this.rowEditingOverlay.element.removeEventListener('wheel', this.rowEditingWheelHandler); + this.rowEditingOverlay.element.removeEventListener('wheel', this._wheelListener); this.rowEditPositioningStrategy.isTopInitialPosition = null; this.rowEditingOverlay.close(); this.rowEditingOverlay.element.parentElement.style.display = '';