8000 refactor(state): use DestroyRef within EffectsService by michaelbe812 · Pull Request #1831 · rx-angular/rx-angular · GitHub
[go: up one dir, main page]

Skip to content

refactor 8000 (state): use DestroyRef within EffectsService #1831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/state/effects/src/lib/effects.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ describe('RxEffects', () => {

service.method1.mockClear();

(component as any).effects.ngOnDestroy();
fixture.destroy();

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

Expand Down
39 changes: 21 additions & 18 deletions libs/state/effects/src/lib/effects.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ErrorHandler, Injectable, OnDestroy, Optional } from '@angular/core';
import {
DestroyRef,
ErrorHandler,
inject,
Injectable,
Optional,
} from '@angular/core';
import {
EMPTY,
from,
Expand Down Expand Up @@ -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<DestroyProp>();
Expand All @@ -95,7 +106,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
*/
register<T>(
sourceObs: ObservableInput<T>,
sideEffectFn: (value: T) => void
sideEffectFn: (value: T) => void,
): number;

/**
Expand All @@ -119,7 +130,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
register<T>(
sourceObs: ObservableInput<T>,
// tslint:disable-next-line: unified-signatures
observer: PartialObserver<T>
observer: PartialObserver<T>,
): number;

/**
Expand Down Expand Up @@ -152,7 +163,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ {

register<T>(
obsOrSub: ObservableInput<T> | Subscription,
fnOrObj?: ((value: T) => void) | PartialObserver<T>
fnOrObj?: ((value: T) => void) | PartialObserver<T>,
): number | void {
if (obsOrSub instanceof Subscription) {
this.subscription.add(obsOrSub);
Expand All @@ -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));
Expand Down Expand Up @@ -223,15 +234,7 @@ export class RxEffects implements OnDestroy, OnDestroy$ {
return <V>(source: Observable<V>) =>
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();
}
}
Loading
0