Skip to content

Commit f43695e

Browse files
committed
WEB-427: Fix memory leak by unsubscribing from form valueChanges in Holiday components
- Added OnDestroy lifecycle hook to both Create and Edit Holiday components - Imported Subject and takeUntil from rxjs - Created destroy$ Subject to manage subscription cleanup - Applied takeUntil operator to form valueChanges subscriptions - Properly unsubscribe on component destruction to prevent memory leaks - Fixes potential performance issues from accumulated subscriptions
1 parent 6610a01 commit f43695e

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

src/app/organization/holidays/create-holiday/create-holiday.component.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** Angular Imports. */
22
import { SelectionModel } from '@angular/cdk/collections';
3-
import { Component, OnInit, ViewChild, Injectable } from '@angular/core';
3+
import { Component, OnInit, OnDestroy, ViewChild, Injectable } from '@angular/core';
44
import {
55
UntypedFormBuilder,
66
UntypedFormGroup,
@@ -9,6 +9,8 @@ import {
99
ReactiveFormsModule
1010
} from '@angular/forms';
1111
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
12+
import { Subject } from 'rxjs';
13+
import { takeUntil } from 'rxjs/operators';
1214
import { Dates } from 'app/core/utils/dates';
1315
import {
1416
MatTreeFlatDataSource,
@@ -52,7 +54,7 @@ import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
5254
MatIcon
5355
]
5456
})
55-
export class CreateHolidayComponent implements OnInit {
57+
export class CreateHolidayComponent implements OnInit, OnDestroy {
5658
/** Create Holiday form. */
5759
holidayForm: UntypedFormGroup;
5860
/** Repayment Scheduling data. */
@@ -63,6 +65,8 @@ export class CreateHolidayComponent implements OnInit {
6365
minDate = new Date(2000, 0, 1);
6466
/** Maximum Date allowed. */
6567
maxDate = new Date(2100, 0, 1);
68+
/** Subject for managing subscriptions. */
69+
private destroy$ = new Subject<void>();
6670
// Stores office data in a trie-like structure
6771
officesTrie: any;
6872
// Stores office data for access from DOM via Office ID
@@ -314,13 +318,16 @@ export class CreateHolidayComponent implements OnInit {
314318
* Sets the conditional controls.
315319
*/
316320
buildDependencies() {
317-
this.holidayForm.get('reschedulingType').valueChanges.subscribe((option: any) => {
318-
if (option === 2) {
319-
this.holidayForm.addControl('repaymentsRescheduledTo', new UntypedFormControl('', Validators.required));
320-
} else {
321-
this.holidayForm.removeControl('repaymentsRescheduledTo');
322-
}
323-
});
321+
this.holidayForm
322+
.get('reschedulingType')
323+
.valueChanges.pipe(takeUntil(this.destroy$))
324+
.subscribe((option: any) => {
325+
if (option === 2) {
326+
this.holidayForm.addControl('repaymentsRescheduledTo', new UntypedFormControl('', Validators.required));
327+
} else {
328+
this.holidayForm.removeControl('repaymentsRescheduledTo');
329+
}
330+
});
324331
}
325332

326333
/**
@@ -360,4 +367,12 @@ export class CreateHolidayComponent implements OnInit {
360367
);
361368
});
362369
}
370+
371+
/**
372+
* Component lifecycle hook to clean up subscriptions.
373+
*/
374+
ngOnDestroy() {
375+
this.destroy$.next();
376+
this.destroy$.complete();
377+
}
363378
}

src/app/organization/holidays/edit-holiday/edit-holiday.component.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** Angular Imports. */
2-
import { Component, OnInit } from '@angular/core';
2+
import { Component, OnInit, OnDestroy } from '@angular/core';
33
import {
44
UntypedFormBuilder,
55
UntypedFormGroup,
@@ -8,6 +8,8 @@ import {
88
ReactiveFormsModule
99
} from '@angular/forms';
1010
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
11+
import { Subject } from 'rxjs';
12+
import { takeUntil } from 'rxjs/operators';
1113
import { Dates } from 'app/core/utils/dates';
1214

1315
/** Custom Services. */
@@ -26,7 +28,7 @@ import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
2628
...STANDALONE_SHARED_IMPORTS
2729
]
2830
})
29-
export class EditHolidayComponent implements OnInit {
31+
export class EditHolidayComponent implements OnInit, OnDestroy {
3032
/** Edit Holiday form. */
3133
holidayForm: UntypedFormGroup;
3234
/** Holiday data. */
@@ -39,6 +41,8 @@ export class EditHolidayComponent implements OnInit {
3941
minDate = new Date(2000, 0, 1);
4042
/** Maximum Date allowed. */
4143
maxDate = new Date();
44+
/** Subject for managing subscriptions. */
45+
private destroy$ = new Subject<void>();
4246

4347
/**
4448
* Get holiday and holiday template from `Resolver`.
@@ -116,14 +120,20 @@ export class EditHolidayComponent implements OnInit {
116120
* Get Rescheduling Type.
117121
*/
118122
getReschedulingType() {
119-
this.holidayForm.get('reschedulingType').valueChanges.subscribe((option: any) => {
120-
this.reSchedulingType = option;
121-
if (option === 2) {
122-
this.holidayForm.addControl('repaymentsRescheduledTo', new UntypedFormControl(new Date(), Validators.required));
123-
} else {
124-
this.holidayForm.removeControl('repaymentsRescheduledTo');
125-
}
126-
});
123+
this.holidayForm
124+
.get('reschedulingType')
125+
.valueChanges.pipe(takeUntil(this.destroy$))
126+
.subscribe((option: any) => {
127+
this.reSchedulingType = option;
128+
if (option === 2) {
129+
this.holidayForm.addControl(
130+
'repaymentsRescheduledTo',
131+
new UntypedFormControl(new Date(), Validators.required)
132+
);
133+
} else {
134+
this.holidayForm.removeControl('repaymentsRescheduledTo');
135+
}
136+
});
127137
}
128138

129139
/**
@@ -160,4 +170,12 @@ export class EditHolidayComponent implements OnInit {
160170
this.router.navigate(['../'], { relativeTo: this.route });
161171
});
162172
}
173+
174+
/**
175+
* Component lifecycle hook to clean up subscriptions.
176+
*/
177+
ngOnDestroy() {
178+
this.destroy$.next();
179+
this.destroy$.complete();
180+
}
163181
}

0 commit comments

Comments
 (0)