Skip to content

Commit

Permalink
fix(datepicker): leaking backdropClick subscriptions (#8919)
Browse files Browse the repository at this point in the history
Currently the datepicker re-subscribes to the same `backdropClicked` stream whenever the popup is attached, which causes it to stack up multiple subscriptions for every time the calendar is opened. These changes move the subscription so it only happens once.
crisbeto authored and andrewseguin committed Dec 13, 2017
1 parent 4d8c712 commit 3728555
Showing 2 changed files with 24 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/lib/datepicker/datepicker.spec.ts
Original file line number Diff line number Diff line change
@@ -340,6 +340,29 @@ describe('MatDatepicker', () => {

expect(() => fixture.detectChanges()).not.toThrow();
});

it('should clear out the backdrop subscriptions on close', () => {
for (let i = 0; i < 3; i++) {
testComponent.datepicker.open();
fixture.detectChanges();

testComponent.datepicker.close();
fixture.detectChanges();
}

testComponent.datepicker.open();
fixture.detectChanges();

spyOn(testComponent.datepicker, 'close').and.callThrough();

const backdrop = document.querySelector('.cdk-overlay-backdrop')! as HTMLElement;

backdrop.click();
fixture.detectChanges();

expect(testComponent.datepicker.close).toHaveBeenCalledTimes(1);
expect(testComponent.datepicker.opened).toBe(false);
});
});

describe('datepicker with too many inputs', () => {
3 changes: 1 addition & 2 deletions src/lib/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
@@ -352,8 +352,6 @@ export class MatDatepicker<D> implements OnDestroy {
this._popupRef.updatePosition();
});
}

this._popupRef.backdropClick().subscribe(() => this.close());
}

/** Create the popup. */
@@ -368,6 +366,7 @@ export class MatDatepicker<D> implements OnDestroy {
});

this._popupRef = this._overlay.create(overlayConfig);
this._popupRef.backdropClick().subscribe(() => this.close());
}

/** Create the popup PositionStrategy. */

0 comments on commit 3728555

Please sign in to comment.