Skip to content

perf(overlay): only compute and emit position changes if there are subscribers #11431

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
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
25 changes: 20 additions & 5 deletions src/cdk/overlay/position/flexible-connected-position-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,19 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
/** Selector to be used when finding the elements on which to set the transform origin. */
private _transformOriginSelector: string;

/** Amount of subscribers to the `positionChanges` stream. */
private _positionChangeSubscriptions = 0;

/** Observable sequence of position changes. */
positionChanges: Observable<ConnectedOverlayPositionChange> =
this._positionChanges.asObservable();
positionChanges: Observable<ConnectedOverlayPositionChange> = Observable.create(observer => {
const subscription = this._positionChanges.subscribe(observer);
this._positionChangeSubscriptions++;

return () => {
subscription.unsubscribe();
this._positionChangeSubscriptions--;
};
});

/** Ordered list of preferred positions, from most to least desirable. */
get positions() {
Expand Down Expand Up @@ -581,9 +591,14 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
this._lastPosition = position;

// Notify that the position has been changed along with its change properties.
const scrollableViewProperties = this._getScrollVisibility();
const changeEvent = new ConnectedOverlayPositionChange(position, scrollableViewProperties);
this._positionChanges.next(changeEvent);
// We only emit if we've got any subscriptions, because the scroll visibility
// calculcations can be somewhat expensive.
if (this._positionChangeSubscriptions > 0) {
const scrollableViewProperties = this._getScrollVisibility();
const changeEvent = new ConnectedOverlayPositionChange(position, scrollableViewProperties);
this._positionChanges.next(changeEvent);
}

this._isInitialRender = false;
}

Expand Down