Skip to content

Commit 3664efe

Browse files
authored
fix(module:modal): resolve memory leaks (#7123)
1 parent 8437111 commit 3664efe

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

components/modal/modal-ref.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ESCAPE, hasModifierKey } from '@angular/cdk/keycodes';
77
import { OverlayRef } from '@angular/cdk/overlay';
88
import { EventEmitter } from '@angular/core';
99
import { Subject } from 'rxjs';
10-
import { filter, take } from 'rxjs/operators';
10+
import { filter, take, takeUntil } from 'rxjs/operators';
1111

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

3737
private closeTimeout?: number;
3838

39+
private destroy$ = new Subject<void>();
40+
3941
constructor(
4042
private overlayRef: OverlayRef,
4143
private config: ModalOptions,
@@ -64,7 +66,7 @@ export class NzModalRef<T = NzSafeAny, R = NzSafeAny> implements NzModalLegacyAP
6466
this._finishDialogClose();
6567
});
6668

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

91-
containerInstance.cancelTriggered.subscribe(() => this.trigger(NzTriggerAction.CANCEL));
93+
containerInstance.cancelTriggered
94+
.pipe(takeUntil(this.destroy$))
95+
.subscribe(() => this.trigger(NzTriggerAction.CANCEL));
9296

93-
containerInstance.okTriggered.subscribe(() => this.trigger(NzTriggerAction.OK));
97+
containerInstance.okTriggered.pipe(takeUntil(this.destroy$)).subscribe(() => this.trigger(NzTriggerAction.OK));
9498

9599
overlayRef.detachments().subscribe(() => {
96100
this.afterClose.next(this.result);
@@ -197,5 +201,6 @@ export class NzModalRef<T = NzSafeAny, R = NzSafeAny> implements NzModalLegacyAP
197201
_finishDialogClose(): void {
198202
this.state = NzModalState.CLOSED;
199203
this.overlayRef.dispose();
204+
this.destroy$.next();
200205
}
201206
}

0 commit comments

Comments
 (0)