Skip to content

Commit

Permalink
fix(module:modal): resolve memory leaks (#7123)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturovt committed Jan 13, 2022
1 parent 8437111 commit 3664efe
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions components/modal/modal-ref.ts
Expand Up @@ -7,7 +7,7 @@ import { ESCAPE, hasModifierKey } from '@angular/cdk/keycodes';
import { OverlayRef } from '@angular/cdk/overlay';
import { EventEmitter } from '@angular/core';
import { Subject } from 'rxjs';
import { filter, take } from 'rxjs/operators';
import { filter, take, takeUntil } from 'rxjs/operators';

import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { isPromise } from 'ng-zorro-antd/core/util';
Expand Down Expand Up @@ -36,6 +36,8 @@ export class NzModalRef<T = NzSafeAny, R = NzSafeAny> implements NzModalLegacyAP

private closeTimeout?: number;

private destroy$ = new Subject<void>();

constructor(
private overlayRef: OverlayRef,
private config: ModalOptions,
Expand Down Expand Up @@ -64,7 +66,7 @@ export class NzModalRef<T = NzSafeAny, R = NzSafeAny> implements NzModalLegacyAP
this._finishDialogClose();
});

containerInstance.containerClick.pipe(take(1)).subscribe(() => {
containerInstance.containerClick.pipe(take(1), takeUntil(this.destroy$)).subscribe(() => {
const cancelable = !this.config.nzCancelLoading && !this.config.nzOkLoading;
if (cancelable) {
this.trigger(NzTriggerAction.CANCEL);
Expand All @@ -88,9 +90,11 @@ export class NzModalRef<T = NzSafeAny, R = NzSafeAny> implements NzModalLegacyAP
this.trigger(NzTriggerAction.CANCEL);
});

containerInstance.cancelTriggered.subscribe(() => this.trigger(NzTriggerAction.CANCEL));
containerInstance.cancelTriggered
.pipe(takeUntil(this.destroy$))
.subscribe(() => this.trigger(NzTriggerAction.CANCEL));

containerInstance.okTriggered.subscribe(() => this.trigger(NzTriggerAction.OK));
containerInstance.okTriggered.pipe(takeUntil(this.destroy$)).subscribe(() => this.trigger(NzTriggerAction.OK));

overlayRef.detachments().subscribe(() => {
this.afterClose.next(this.result);
Expand Down Expand Up @@ -197,5 +201,6 @@ export class NzModalRef<T = NzSafeAny, R = NzSafeAny> implements NzModalLegacyAP
_finishDialogClose(): void {
this.state = NzModalState.CLOSED;
this.overlayRef.dispose();
this.destroy$.next();
}
}

0 comments on commit 3664efe

Please sign in to comment.