Skip to content

Commit 0e5bb02

Browse files
crisbetoandrewseguin
authored andcommitted
fix(scrolling): scrollable elementScrolled stream not being completed on destroy (#11518)
Avoids potential memory leaks by completing the `CdkScrollable.elementScrolled` stream when the directive is destroyed.
1 parent 8e394a1 commit 0e5bb02

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/cdk/scrolling/scroll-dispatcher.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {NgModule, Component, ViewChild, ElementRef} from '@angular/core';
33
import {CdkScrollable, ScrollDispatcher, ScrollDispatchModule} from './public-api';
44
import {dispatchFakeEvent} from '@angular/cdk/testing';
55

6-
describe('Scroll Dispatcher', () => {
6+
describe('ScrollDispatcher', () => {
77

88
beforeEach(async(() => {
99
TestBed.configureTestingModule({
@@ -101,6 +101,17 @@ describe('Scroll Dispatcher', () => {
101101

102102
subscription.unsubscribe();
103103
});
104+
105+
it('should complete the scrollable stream when it is destroyed', () => {
106+
const scrollable = fixture.componentInstance.scrollable;
107+
const spy = jasmine.createSpy('complete spy');
108+
const subscription = scrollable.elementScrolled().subscribe(undefined, undefined, spy);
109+
110+
fixture.destroy();
111+
expect(spy).toHaveBeenCalled();
112+
subscription.unsubscribe();
113+
});
114+
104115
});
105116

106117
describe('Nested scrollables', () => {

src/cdk/scrolling/scrollable.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export class CdkScrollable implements OnInit, OnDestroy {
4141
if (this._scrollListener) {
4242
this.getElementRef().nativeElement.removeEventListener('scroll', this._scrollListener);
4343
}
44+
45+
this._elementScrolled.complete();
4446
}
4547

4648
/**

0 commit comments

Comments
 (0)