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.
  • Loading branch information
crisbeto authored and andrewseguin committed Dec 19, 2017
1 parent 534c797 commit baf22ec
Show file tree
Hide file tree
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
Expand Up @@ -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', () => {
Expand Down
3 changes: 1 addition & 2 deletions src/lib/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ export class MatDatepicker<D> implements OnDestroy {
this._popupRef.updatePosition();
});
}

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

/** Create the popup. */
Expand All @@ -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. */
Expand Down

0 comments on commit baf22ec

Please sign in to comment.