From c1d835dc50110fa38edd7b4e68c2dbd36a5b896e Mon Sep 17 00:00:00 2001 From: Michael Berger Date: Sun, 5 Jan 2025 15:34:30 +0100 Subject: [PATCH] refactor(state): use DestroyRef within EffectsService --- .../effects/src/lib/effects.service.spec.ts | 2 +- libs/state/effects/src/lib/effects.service.ts | 39 ++++++++++--------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/libs/state/effects/src/lib/effects.service.spec.ts b/libs/state/effects/src/lib/effects.service.spec.ts index 1a2c87cddd..ea2ed7e143 100644 --- a/libs/state/effects/src/lib/effects.service.spec.ts +++ b/libs/state/effects/src/lib/effects.service.spec.ts @@ -364,7 +364,7 @@ describe('RxEffects', () => { service.method1.mockClear(); - (component as any).effects.ngOnDestroy(); + fixture.destroy(); store.state$.next('bar'); diff --git a/libs/state/effects/src/lib/effects.service.ts b/libs/state/effects/src/lib/effects.service.ts index f477d7100f..daf002bc43 100644 --- a/libs/state/effects/src/lib/effects.service.ts +++ b/libs/state/effects/src/lib/effects.service.ts @@ -1,4 +1,10 @@ -import { ErrorHandler, Injectable, OnDestroy, Optional } from '@angular/core'; +import { + DestroyRef, + ErrorHandler, + inject, + Injectable, + Optional, +} from '@angular/core'; import { EMPTY, from, @@ -65,11 +71,16 @@ import { toHook, untilDestroyed } from './utils'; * NOTE: Avoid calling register/unregister/subscribe inside the side-effect function. */ @Injectable() -export class RxEffects implements OnDestroy, OnDestroy$ { +export class RxEffects implements OnDestroy$ { constructor( @Optional() - private readonly errorHandler: ErrorHandler | null - ) {} + private readonly errorHandler: ErrorHandler | null, + ) { + inject(DestroyRef).onDestroy(() => { + this._hooks$.next({ destroy: true }); + this.subscription.unsubscribe(); + }); + } private static nextId = 0; readonly _hooks$ = new Subject(); @@ -95,7 +106,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ { */ register( sourceObs: ObservableInput, - sideEffectFn: (value: T) => void + sideEffectFn: (value: T) => void, ): number; /** @@ -119,7 +130,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ { register( sourceObs: ObservableInput, // tslint:disable-next-line: unified-signatures - observer: PartialObserver + observer: PartialObserver, ): number; /** @@ -152,7 +163,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ { register( obsOrSub: ObservableInput | Subscription, - fnOrObj?: ((value: T) => void) | PartialObserver + fnOrObj?: ((value: T) => void) | PartialObserver, ): number | void { if (obsOrSub instanceof Subscription) { this.subscription.add(obsOrSub); @@ -170,8 +181,8 @@ export class RxEffects implements OnDestroy, OnDestroy$ { this.errorHandler?.handleError(err); return EMPTY; }), - applyBehavior - ) + applyBehavior, + ), ); } else { this.observables$.next(from(obsOrSub).pipe(applyBehavior)); @@ -223,15 +234,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ { return (source: Observable) => source.pipe( untilDestroyed(this), - takeUntil(this.effects$.pipe(filter((eId) => eId === effectId))) + takeUntil(this.effects$.pipe(filter((eId) => eId === effectId))), ); } - - /** - * @internal - */ - ngOnDestroy(): void { - this._hooks$.next({ destroy: true }); - this.subscription.unsubscribe(); - } }