Browse Source

feat(lib): implement takeUntilDestroyed

pull/6385/head
Kenrick Tandrian 1 month ago
parent
commit
bf9e71c78a
  1. 19
      libs/ui/src/lib/fire-calculator/fire-calculator.component.ts

19
libs/ui/src/lib/fire-calculator/fire-calculator.component.ts

@ -19,6 +19,7 @@ import {
ViewChild, ViewChild,
output output
} from '@angular/core'; } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { import {
FormBuilder, FormBuilder,
FormControl, FormControl,
@ -56,7 +57,7 @@ import {
} from 'date-fns'; } from 'date-fns';
import { isNil, isNumber } from 'lodash'; import { isNil, isNumber } from 'lodash';
import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader'; import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader';
import { Subject, debounceTime, takeUntil } from 'rxjs'; import { debounceTime } from 'rxjs';
import { FireCalculatorService } from './fire-calculator.service'; import { FireCalculatorService } from './fire-calculator.service';
@ -114,7 +115,6 @@ export class GfFireCalculatorComponent implements OnChanges, OnDestroy {
private readonly DEFAULT_RETIREMENT_DATE = startOfMonth( private readonly DEFAULT_RETIREMENT_DATE = startOfMonth(
addYears(new Date(), 10) addYears(new Date(), 10)
); );
private readonly unsubscribeSubject = new Subject<void>();
public constructor( public constructor(
private changeDetectorRef: ChangeDetectorRef, private changeDetectorRef: ChangeDetectorRef,
@ -130,13 +130,13 @@ export class GfFireCalculatorComponent implements OnChanges, OnDestroy {
); );
this.calculatorForm.valueChanges this.calculatorForm.valueChanges
.pipe(takeUntil(this.unsubscribeSubject)) .pipe(takeUntilDestroyed())
.subscribe(() => { .subscribe(() => {
this.initialize(); this.initialize();
}); });
this.calculatorForm.valueChanges this.calculatorForm.valueChanges
.pipe(debounceTime(500), takeUntil(this.unsubscribeSubject)) .pipe(debounceTime(500), takeUntilDestroyed())
.subscribe(() => { .subscribe(() => {
const { projectedTotalAmount, retirementDate } = const { projectedTotalAmount, retirementDate } =
this.calculatorForm.getRawValue(); this.calculatorForm.getRawValue();
@ -151,7 +151,7 @@ export class GfFireCalculatorComponent implements OnChanges, OnDestroy {
this.calculatorForm this.calculatorForm
.get('annualInterestRate') .get('annualInterestRate')
?.valueChanges.pipe(debounceTime(500), takeUntil(this.unsubscribeSubject)) ?.valueChanges.pipe(debounceTime(500), takeUntilDestroyed())
.subscribe((annualInterestRate) => { .subscribe((annualInterestRate) => {
if (annualInterestRate !== null) { if (annualInterestRate !== null) {
this.annualInterestRateChanged.emit(annualInterestRate); this.annualInterestRateChanged.emit(annualInterestRate);
@ -159,7 +159,7 @@ export class GfFireCalculatorComponent implements OnChanges, OnDestroy {
}); });
this.calculatorForm this.calculatorForm
.get('paymentPerPeriod') .get('paymentPerPeriod')
?.valueChanges.pipe(debounceTime(500), takeUntil(this.unsubscribeSubject)) ?.valueChanges.pipe(debounceTime(500), takeUntilDestroyed())
.subscribe((savingsRate) => { .subscribe((savingsRate) => {
if (savingsRate !== null) { if (savingsRate !== null) {
this.savingsRateChanged.emit(savingsRate); this.savingsRateChanged.emit(savingsRate);
@ -167,7 +167,7 @@ export class GfFireCalculatorComponent implements OnChanges, OnDestroy {
}); });
this.calculatorForm this.calculatorForm
.get('projectedTotalAmount') .get('projectedTotalAmount')
?.valueChanges.pipe(debounceTime(500), takeUntil(this.unsubscribeSubject)) ?.valueChanges.pipe(debounceTime(500), takeUntilDestroyed())
.subscribe((projectedTotalAmount) => { .subscribe((projectedTotalAmount) => {
if (projectedTotalAmount !== null) { if (projectedTotalAmount !== null) {
this.projectedTotalAmountChanged.emit(projectedTotalAmount); this.projectedTotalAmountChanged.emit(projectedTotalAmount);
@ -175,7 +175,7 @@ export class GfFireCalculatorComponent implements OnChanges, OnDestroy {
}); });
this.calculatorForm this.calculatorForm
.get('retirementDate') .get('retirementDate')
?.valueChanges.pipe(debounceTime(500), takeUntil(this.unsubscribeSubject)) ?.valueChanges.pipe(debounceTime(500), takeUntilDestroyed())
.subscribe((retirementDate) => { .subscribe((retirementDate) => {
if (retirementDate !== null) { if (retirementDate !== null) {
this.retirementDateChanged.emit(retirementDate); this.retirementDateChanged.emit(retirementDate);
@ -265,9 +265,6 @@ export class GfFireCalculatorComponent implements OnChanges, OnDestroy {
public ngOnDestroy() { public ngOnDestroy() {
this.chart?.destroy(); this.chart?.destroy();
this.unsubscribeSubject.next();
this.unsubscribeSubject.complete();
} }
private initialize() { private initialize() {

Loading…
Cancel
Save