Skip to content
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

fix(module:modal): resolve memory leaks #7123

Merged
merged 1 commit into from Jan 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}
}