Skip to content

Commit fec20ad

Browse files
authored
perf(cdk/scrolling): don't re-measure viewport on resize (#23124)
We don't need to re-measure the viewport any time it is resized, we just need to clear the cache so that it is re-measured next time somebody requests it.
1 parent 7fae1a9 commit fec20ad

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/cdk/scrolling/viewport-ruler.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface ViewportScrollPosition {
2828
@Injectable({providedIn: 'root'})
2929
export class ViewportRuler implements OnDestroy {
3030
/** Cached viewport dimensions. */
31-
private _viewportSize: {width: number; height: number};
31+
private _viewportSize: {width: number; height: number} | null;
3232

3333
/** Stream of viewport change events. */
3434
private readonly _change = new Subject<Event>();
@@ -56,9 +56,9 @@ export class ViewportRuler implements OnDestroy {
5656
window.addEventListener('orientationchange', this._changeListener);
5757
}
5858

59-
// We don't need to keep track of the subscription,
60-
// because we complete the `change` stream on destroy.
61-
this.change().subscribe(() => this._updateViewportSize());
59+
// Clear the cached position so that the viewport is re-measured next time it is required.
60+
// We don't need to keep track of the subscription, because it is completed on destroy.
61+
this.change().subscribe(() => this._viewportSize = null);
6262
});
6363
}
6464

@@ -78,7 +78,7 @@ export class ViewportRuler implements OnDestroy {
7878
this._updateViewportSize();
7979
}
8080

81-
const output = {width: this._viewportSize.width, height: this._viewportSize.height};
81+
const output = {width: this._viewportSize!.width, height: this._viewportSize!.height};
8282

8383
// If we're not on a browser, don't cache the size since it'll be mocked out anyway.
8484
if (!this._platform.isBrowser) {

0 commit comments

Comments
 (0)