Skip to content

Commit

Permalink
fix(cdk): DirectiveStylesService fix ssr memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirpotekhin committed Dec 19, 2023
1 parent 8108863 commit 9ac6b1c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions projects/cdk/services/directive-styles.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
ComponentFactoryResolver,
ComponentRef,
Inject,
Injectable,
INJECTOR,
Injector,
OnDestroy,
Type,
} from '@angular/core';

Expand All @@ -13,8 +15,8 @@ import {
@Injectable({
providedIn: `root`,
})
export class TuiDirectiveStylesService {
private readonly map = new Map<Type<unknown>, unknown>();
export class TuiDirectiveStylesService implements OnDestroy {
private readonly map = new Map<string, ComponentRef<unknown>>();

constructor(
@Inject(ComponentFactoryResolver)
Expand All @@ -23,11 +25,15 @@ export class TuiDirectiveStylesService {
) {}

addComponent(component: Type<unknown>): void {
if (!this.map.has(component)) {
if (!this.map.has(component.name)) {
this.map.set(
component,
component.name,
this.resolver.resolveComponentFactory(component).create(this.injector),
);
}
}

ngOnDestroy(): void {
this.map.forEach(value => value.destroy());
}
}

0 comments on commit 9ac6b1c

Please sign in to comment.