Skip to content

fix(overlay): only dispatch position change event if requested #19785

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 28, 2020
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
23 changes: 20 additions & 3 deletions src/cdk/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
ViewContainerRef,
} from '@angular/core';
import {Subscription} from 'rxjs';
import {takeWhile} from 'rxjs/operators';
import {Overlay} from './overlay';
import {OverlayConfig} from './overlay-config';
import {OverlayRef} from './overlay-ref';
Expand Down Expand Up @@ -113,6 +114,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
private _backdropSubscription = Subscription.EMPTY;
private _attachSubscription = Subscription.EMPTY;
private _detachSubscription = Subscription.EMPTY;
private _positionSubscription = Subscription.EMPTY;
private _offsetX: number;
private _offsetY: number;
private _position: FlexibleConnectedPositionStrategy;
Expand Down Expand Up @@ -254,6 +256,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
this._attachSubscription.unsubscribe();
this._detachSubscription.unsubscribe();
this._backdropSubscription.unsubscribe();
this._positionSubscription.unsubscribe();

if (this._overlayRef) {
this._overlayRef.dispose();
Expand Down Expand Up @@ -367,10 +370,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
/** Returns the position strategy of the overlay to be set on the overlay config */
private _createPositionStrategy(): FlexibleConnectedPositionStrategy {
const strategy = this._overlay.position().flexibleConnectedTo(this.origin.elementRef);

this._updatePositionStrategy(strategy);
strategy.positionChanges.subscribe(p => this.positionChange.emit(p));

return strategy;
}

Expand All @@ -394,6 +394,22 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
} else {
this._backdropSubscription.unsubscribe();
}

this._positionSubscription.unsubscribe();

// Only subscribe to `positionChanges` if requested, because putting
// together all the information for it can be expensive.
if (this.positionChange.observers.length > 0) {
this._positionSubscription = this._position.positionChanges
.pipe(takeWhile(() => this.positionChange.observers.length > 0))
.subscribe(position => {
this.positionChange.emit(position);

if (this.positionChange.observers.length === 0) {
this._positionSubscription.unsubscribe();
}
});
}
}

/** Detaches the overlay and unsubscribes to backdrop clicks if backdrop exists */
Expand All @@ -403,6 +419,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
}

this._backdropSubscription.unsubscribe();
this._positionSubscription.unsubscribe();
}

static ngAcceptInputType_hasBackdrop: BooleanInput;
Expand Down