Skip to content

Commit 1a2c195

Browse files
committed
fix: add ngOnDestroy to DependencyGraphComponent to prevent memory leak
1 parent 47283a7 commit 1a2c195

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

src/app/component/dependency-graph/dependency-graph.component.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { Component, OnInit, Input, ElementRef, SimpleChanges, OnChanges } from '@angular/core';
1+
import { Component, OnInit, OnDestroy, Input, ElementRef, SimpleChanges, OnChanges } from '@angular/core';
22
import * as d3 from 'd3';
3+
import { Subject } from 'rxjs';
4+
import { takeUntil } from 'rxjs/operators';
35
import { LoaderService } from '../../service/loader/data-loader.service';
46
import { Activity } from 'src/app/model/activity-store';
57
import { DataStore } from 'src/app/model/data-store';
@@ -38,7 +40,8 @@ interface ThemeColors {
3840
templateUrl: './dependency-graph.component.html',
3941
styleUrls: ['./dependency-graph.component.css'],
4042
})
41-
export class DependencyGraphComponent implements OnInit, OnChanges {
43+
export class DependencyGraphComponent implements OnInit, OnChanges, OnDestroy {
44+
private destroy$ = new Subject<void>();
4245
css: CSSStyleDeclaration = getComputedStyle(document.body);
4346
themeColors: Partial<ThemeColors> = {};
4447
theme: string;
@@ -66,11 +69,19 @@ export class DependencyGraphComponent implements OnInit, OnChanges {
6669
});
6770

6871
// Reactively handle theme changes (if user toggles later)
69-
this.themeService.theme$.subscribe((theme: string) => {
72+
this.themeService.theme$.pipe(takeUntil(this.destroy$)).subscribe((theme: string) => {
7073
this.setThemeColors(theme);
7174
});
7275
}
7376

77+
ngOnDestroy(): void {
78+
this.destroy$.next();
79+
this.destroy$.complete();
80+
if (this.simulation) {
81+
this.simulation.stop();
82+
}
83+
}
84+
7485
ngOnChanges(changes: SimpleChanges): void {
7586
if (this.dataStore?.activityStore) {
7687
if (changes?.hasOwnProperty('activityName')) {

0 commit comments

Comments
 (0)