Skip to content

Avoid emitting position change event unnecessarily and emit inside the zone #28616

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 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion src/cdk/overlay/overlay-directives.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, ElementRef, ViewChild} from '@angular/core';
import {Component, ElementRef, NgZone, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {
ComponentFixture,
Expand Down Expand Up @@ -618,6 +618,18 @@ describe('Overlay directives', () => {
.toBe(true);
});

it('should emit the position change handler inside the zone', () => {
let callsInZone: boolean[] = [];

fixture.componentInstance.positionChangeHandler.and.callFake(() => {
callsInZone.push(NgZone.isInAngularZone());
});
fixture.componentInstance.isOpen = true;
fixture.detectChanges();

expect(callsInZone).toEqual([true]);
});

it('should emit when attached', () => {
expect(fixture.componentInstance.attachHandler).not.toHaveBeenCalled();
fixture.componentInstance.isOpen = true;
Expand Down
4 changes: 3 additions & 1 deletion src/cdk/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Inject,
InjectionToken,
Input,
NgZone,
OnChanges,
OnDestroy,
Optional,
Expand Down Expand Up @@ -116,6 +117,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
private _position: FlexibleConnectedPositionStrategy;
private _scrollStrategyFactory: () => ScrollStrategy;
private _disposeOnNavigation = false;
private _ngZone = inject(NgZone);

/** Origin for the connected overlay. */
@Input('cdkConnectedOverlayOrigin')
Expand Down Expand Up @@ -421,7 +423,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
this._positionSubscription = this._position.positionChanges
.pipe(takeWhile(() => this.positionChange.observers.length > 0))
.subscribe(position => {
this.positionChange.emit(position);
this._ngZone.run(() => this.positionChange.emit(position));

if (this.positionChange.observers.length === 0) {
this._positionSubscription.unsubscribe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
/** The last position to have been calculated as the best fit position. */
private _lastPosition: ConnectedPosition | null;

/** The last calculated scroll visibility. Only tracked */
private _lastScrollVisibility: ScrollingVisibility | null;

/** Subject that emits whenever the position changes. */
private readonly _positionChanges = new Subject<ConnectedOverlayPositionChange>();

Expand Down Expand Up @@ -710,18 +713,28 @@ export class FlexibleConnectedPositionStrategy implements PositionStrategy {
this._addPanelClasses(position.panelClass);
}

// Save the last connected position in case the position needs to be re-calculated.
this._lastPosition = position;

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

// We're recalculating on scroll, but we only want to emit if anything
// changed since downstream code might be hitting the `NgZone`.
if (
position !== this._lastPosition ||
!this._lastScrollVisibility ||
!compareScrollVisibility(this._lastScrollVisibility, scrollVisibility)
) {
const changeEvent = new ConnectedOverlayPositionChange(position, scrollVisibility);
this._positionChanges.next(changeEvent);
}

this._lastScrollVisibility = scrollVisibility;
}

// Save the last connected position in case the position needs to be re-calculated.
this._lastPosition = position;
this._isInitialRender = false;
}

Expand Down Expand Up @@ -1289,6 +1302,20 @@ function getRoundedBoundingClientRect(clientRect: Dimensions): Dimensions {
};
}

/** Returns whether two `ScrollingVisibility` objects are identical. */
function compareScrollVisibility(a: ScrollingVisibility, b: ScrollingVisibility): boolean {
if (a === b) {
return true;
}

return (
a.isOriginClipped === b.isOriginClipped &&
a.isOriginOutsideView === b.isOriginOutsideView &&
a.isOverlayClipped === b.isOverlayClipped &&
a.isOverlayOutsideView === b.isOverlayOutsideView
);
}

export const STANDARD_DROPDOWN_BELOW_POSITIONS: ConnectedPosition[] = [
{originX: 'start', originY: 'bottom', overlayX: 'start', overlayY: 'top'},
{originX: 'start', originY: 'top', overlayX: 'start', overlayY: 'bottom'},
Expand Down