Skip to content

Commit

Permalink
fix(overlay): onPositionChange stream not being completed
Browse files Browse the repository at this point in the history
Completes the `onPositionChange` stream when the position strategy is disposed. Also cleans up a few cases that were unsubscribing from it explicitly.
  • Loading branch information
crisbeto committed Dec 14, 2017
1 parent f95f832 commit 3c7526a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
5 changes: 1 addition & 4 deletions src/cdk/overlay/overlay-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
private _templatePortal: TemplatePortal<any>;
private _hasBackdrop = false;
private _backdropSubscription = Subscription.EMPTY;
private _positionSubscription = Subscription.EMPTY;
private _offsetX: number = 0;
private _offsetY: number = 0;
private _position: ConnectedPositionStrategy;
Expand Down Expand Up @@ -328,8 +327,7 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
);
}

this._positionSubscription =
strategy.onPositionChange.subscribe(pos => this.positionChange.emit(pos));
strategy.onPositionChange.subscribe(pos => this.positionChange.emit(pos));
}

/** Attaches the overlay and subscribes to backdrop clicks if backdrop exists */
Expand Down Expand Up @@ -372,7 +370,6 @@ export class CdkConnectedOverlay implements OnDestroy, OnChanges {
}

this._backdropSubscription.unsubscribe();
this._positionSubscription.unsubscribe();
this._document.removeEventListener('keydown', this._escapeListener);
}

Expand Down
18 changes: 18 additions & 0 deletions src/cdk/overlay/position/connected-position-strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,24 @@ describe('ConnectedPositionStrategy', () => {
subscription.unsubscribe();
});

it('should complete the onPositionChange stream on dispose', () => {
positionBuilder = new OverlayPositionBuilder(viewportRuler, document);

strategy = positionBuilder.connectedTo(
fakeElementRef,
{originX: 'end', originY: 'bottom'},
{overlayX: 'start', overlayY: 'top'});

const completeHandler = jasmine.createSpy('complete handler');

strategy.onPositionChange.subscribe(undefined, undefined, completeHandler);
strategy.attach(fakeOverlayRef(overlayElement));
strategy.apply();
strategy.dispose();

expect(completeHandler).toHaveBeenCalled();
});

it('should pick the fallback position that shows the largest area of the element', () => {
originElement.style.top = '200px';
originElement.style.right = '25px';
Expand Down
1 change: 1 addition & 0 deletions src/cdk/overlay/position/connected-position-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export class ConnectedPositionStrategy implements PositionStrategy {
dispose() {
this._applied = false;
this._resizeSubscription.unsubscribe();
this._onPositionChange.complete();
}

/** @docs-private */
Expand Down
4 changes: 1 addition & 3 deletions src/lib/menu/menu-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
private _overlayRef: OverlayRef | null = null;
private _menuOpen: boolean = false;
private _closeSubscription = Subscription.EMPTY;
private _positionSubscription = Subscription.EMPTY;
private _hoverSubscription = Subscription.EMPTY;

// Tracking input type is necessary so it's possible to only auto-focus
Expand Down Expand Up @@ -336,7 +335,7 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
* correct, even if a fallback position is used for the overlay.
*/
private _subscribeToPositions(position: ConnectedPositionStrategy): void {
this._positionSubscription = position.onPositionChange.subscribe(change => {
position.onPositionChange.subscribe(change => {
const posX: MenuPositionX = change.connectionPair.overlayX === 'start' ? 'after' : 'before';
const posY: MenuPositionY = change.connectionPair.overlayY === 'top' ? 'below' : 'above';

Expand Down Expand Up @@ -391,7 +390,6 @@ export class MatMenuTrigger implements AfterContentInit, OnDestroy {
/** Cleans up the active subscriptions. */
private _cleanUpSubscriptions(): void {
this._closeSubscription.unsubscribe();
this._positionSubscription.unsubscribe();
this._hoverSubscription.unsubscribe();
}

Expand Down

0 comments on commit 3c7526a

Please sign in to comment.