Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1d835d

Browse files
committed
refactor(state): use DestroyRef within EffectsService
1 parent 70173f6 commit c1d835d

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

libs/state/effects/src/lib/effects.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ describe('RxEffects', () => {
364364

365365
service.method1.mockClear();
366366

367-
(component as any).effects.ngOnDestroy();
367+
fixture.destroy();
368368

369369
store.state$.next('bar');
370370

libs/state/effects/src/lib/effects.service.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { ErrorHandler, Injectable, OnDestroy, Optional } from '@angular/core';
1+
import {
2+
DestroyRef,
3+
ErrorHandler,
4+
inject,
5+
Injectable,
6+
Optional,
7+
} from '@angular/core';
28
import {
39
EMPTY,
410
from,
@@ -65,11 +71,16 @@ import { toHook, untilDestroyed } from './utils';
6571
* NOTE: Avoid calling register/unregister/subscribe inside the side-effect function.
6672
*/
6773
@Injectable()
68-
export class RxEffects implements OnDestroy, OnDestroy$ {
74+
export class RxEffects implements OnDestroy$ {
6975
constructor(
7076
@Optional()
71-
private readonly errorHandler: ErrorHandler | null
72-
) {}
77+
private readonly errorHandler: ErrorHandler | null,
78+
) {
79+
inject(DestroyRef).onDestroy(() => {
80+
this._hooks$.next({ destroy: true });
81+
this.subscription.unsubscribe();
82+
});
83+
}
7384

7485
private static nextId = 0;
7586
readonly _hooks$ = new Subject<DestroyProp>();
@@ -95,7 +106,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
95106
*/
96107
register<T>(
97108
sourceObs: ObservableInput<T>,
98-
sideEffectFn: (value: T) => void
109+
sideEffectFn: (value: T) => void,
99110
): number;
100111

101112
/**
@@ -119,7 +130,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
119130
register<T>(
120131
sourceObs: ObservableInput<T>,
121132
// tslint:disable-next-line: unified-signatures
122-
observer: PartialObserver<T>
133+
observer: PartialObserver<T>,
123134
): number;
124135

125136
/**
@@ -152,7 +163,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
152163

153164
register<T>(
154165
obsOrSub: ObservableInput<T> | Subscription,
155-
fnOrObj?: ((value: T) => void) | PartialObserver<T>
166+
fnOrObj?: ((value: T) => void) | PartialObserver<T>,
156167
): number | void {
157168
if (obsOrSub instanceof Subscription) {
158169
this.subscription.add(obsOrSub);
@@ -170,8 +181,8 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
170181
this.errorHandler?.handleError(err);
171182
return EMPTY;
172183
}),
173-
applyBehavior
174-
)
184+
applyBehavior,
185+
),
175186
);
176187
} else {
177188
this.observables$.next(from(obsOrSub).pipe(applyBehavior));
@@ -223,15 +234,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
223234
return <V>(source: Observable<V>) =>
224235
source.pipe(
225236
untilDestroyed(this),
226-
takeUntil(this.effects$.pipe(filter((eId) => eId === effectId)))
237+
takeUntil(this.effects$.pipe(filter((eId) => eId === effectId))),
227238
);
228239
}
229-
230-
/**
231-
* @internal
232-
*/
233-
ngOnDestroy(): void {
234-
this._hooks$.next({ destroy: true });
235-
this.subscription.unsubscribe();
236-
}
237240
}

0 commit comments

Comments
 (0)