Skip to content

Commit 876f20d

Browse files
fix(ComponentLoader): loader leaks memory in multiple places (#6625)
* fix(ComponentLoader): loader leaks memory in multiple places * Fix(component-loader): fixing lint --------- Co-authored-by: Alexey Umanskiy <[email protected]>
1 parent e6e374d commit 876f20d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/component-loader/component-loader.class.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ export class ComponentLoader<T extends object> {
226226
);
227227
}
228228
this._contentRef?.viewRef?.destroy();
229+
this._componentRef?.destroy();
229230

230231
this._contentRef = void 0;
231232
this._componentRef = void 0;
@@ -312,12 +313,16 @@ export class ComponentLoader<T extends object> {
312313
if (!this._componentRef || !this._componentRef.location) {
313314
return;
314315
}
316+
317+
let unsubscribeOutsideClick = Function.prototype;
318+
let unsubscribeEscClick = Function.prototype;
319+
315320
// why: should run after first event bubble
316321
if (this._listenOpts.outsideClick) {
317322
const target = this._componentRef.location.nativeElement;
318323
setTimeout(() => {
319324
if (this._renderer && this._elementRef) {
320-
this._globalListener = registerOutsideClick(this._renderer, {
325+
unsubscribeOutsideClick = registerOutsideClick(this._renderer, {
321326
targets: [target, this._elementRef.nativeElement],
322327
outsideClick: this._listenOpts.outsideClick,
323328
hide: () => this._listenOpts.hide && this._listenOpts.hide()
@@ -327,12 +332,17 @@ export class ComponentLoader<T extends object> {
327332
}
328333
if (this._listenOpts.outsideEsc && this._renderer && this._elementRef) {
329334
const target = this._componentRef.location.nativeElement;
330-
this._globalListener = registerEscClick(this._renderer, {
335+
unsubscribeEscClick = registerEscClick(this._renderer, {
331336
targets: [target, this._elementRef.nativeElement],
332337
outsideEsc: this._listenOpts.outsideEsc,
333338
hide: () => this._listenOpts.hide && this._listenOpts.hide()
334339
});
335340
}
341+
342+
this._globalListener = () => {
343+
unsubscribeOutsideClick();
344+
unsubscribeEscClick();
345+
};
336346
}
337347

338348
getInnerComponent(): ComponentRef<T> | undefined {

0 commit comments

Comments
 (0)