Skip to content

Commit cfdaefa

Browse files
authored
fix: prevent memory leak when component is destroyed
1 parent 60ba0d6 commit cfdaefa

File tree

1 file changed

+2
-26
lines changed

1 file changed

+2
-26
lines changed

src/virtual-scroll.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import 'rxjs/add/operator/switchMap';
2-
import 'rxjs/add/observable/of';
3-
41
import {
52
Component,
63
ElementRef,
@@ -9,7 +6,6 @@ import {
96
Input,
107
NgModule,
118
OnChanges,
12-
OnDestroy,
139
OnInit,
1410
Output,
1511
Renderer,
@@ -18,8 +14,6 @@ import {
1814
} from '@angular/core';
1915

2016
import { CommonModule } from '@angular/common';
21-
import { Observable } from 'rxjs/Observable';
22-
import { Subject } from 'rxjs/Subject';
2317

2418
export interface ChangeEvent {
2519
start?: number;
@@ -55,7 +49,7 @@ export interface ChangeEvent {
5549
}
5650
`]
5751
})
58-
export class VirtualScrollComponent implements OnInit, OnDestroy, OnChanges {
52+
export class VirtualScrollComponent implements OnInit, OnChanges {
5953

6054
@Input()
6155
items: any[] = [];
@@ -87,9 +81,6 @@ export class VirtualScrollComponent implements OnInit, OnDestroy, OnChanges {
8781
@ViewChild('content', { read: ElementRef })
8882
contentElementRef: ElementRef;
8983

90-
scroll$: Subject<Event> = new Subject<Event>();
91-
92-
onScrollListener: Function;
9384
topPadding: number;
9485
scrollHeight: number;
9586
previousStart: number;
@@ -100,15 +91,10 @@ export class VirtualScrollComponent implements OnInit, OnDestroy, OnChanges {
10091

10192
@HostListener('scroll')
10293
onScroll(e: Event) {
103-
this.scroll$.next();
94+
this.refresh();
10495
}
10596

10697
ngOnInit() {
107-
this.scroll$.switchMap(() => {
108-
this.refresh();
109-
return Observable.of();
110-
}).subscribe();
111-
11298
this.scrollbarWidth = 0; // this.element.nativeElement.offsetWidth - this.element.nativeElement.clientWidth;
11399
this.scrollbarHeight = 0; // this.element.nativeElement.offsetHeight - this.element.nativeElement.clientHeight;
114100
}
@@ -123,16 +109,6 @@ export class VirtualScrollComponent implements OnInit, OnDestroy, OnChanges {
123109
this.refresh();
124110
}
125111

126-
ngOnDestroy() {
127-
// Check that listener has been attached properly:
128-
// It may be undefined in some cases, e.g. if an exception is thrown, the component is
129-
// not initialized properly but destroy may be called anyways (e.g. in testing).
130-
if (this.onScrollListener !== undefined) {
131-
// this removes the listener
132-
this.onScrollListener();
133-
}
134-
}
135-
136112
refresh() {
137113
requestAnimationFrame(() => this.calculateItems());
138114
}

0 commit comments

Comments
 (0)