Skip to content

perf(cdk/scrolling): don't re-measure viewport on resize #23124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/cdk/scrolling/viewport-ruler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface ViewportScrollPosition {
@Injectable({providedIn: 'root'})
export class ViewportRuler implements OnDestroy {
/** Cached viewport dimensions. */
private _viewportSize: {width: number; height: number};
private _viewportSize: {width: number; height: number} | null;

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

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

Expand All @@ -78,7 +78,7 @@ export class ViewportRuler implements OnDestroy {
this._updateViewportSize();
}

const output = {width: this._viewportSize.width, height: this._viewportSize.height};
const output = {width: this._viewportSize!.width, height: this._viewportSize!.height};

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