8000 refactor(state): drop untilDestroyed operator by michaelbe812 · Pull Request #1838 · rx-angular/rx-angular · GitHub
[go: up one dir, main page]

Skip to content

refactor(state): drop untilDestroyed operator #1838

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
merged 3 commits into from
Jan 28, 2025
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
39 changes: 18 additions & 21 deletions libs/state/effects/src/lib/effects.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {
DestroyRef,
ErrorHandler,
inject,
Injectable,
Optional,
} from '@angular/core';
import { DestroyRef, ErrorHandler, inject, Injectable } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import {
EMPTY,
from,
Expand All @@ -18,14 +13,14 @@ import {
import {
catchError,
filter,
mapTo,
map,
mergeAll,
share,
takeUntil,
tap,
} from 'rxjs/operators';
import { DestroyProp, OnDestroy$ } from './model';
import { toHook, untilDestroyed } from './utils';
import { toHook } from './utils';

/**
* @deprecated - use rxEffects instead
Expand Down Expand Up @@ -72,17 +67,9 @@ import { toHook, untilDestroyed } from './utils';
*/
@Injectable()
export class RxEffects implements OnDestroy$ {
constructor(
@Optional()
private readonly errorHandler: ErrorHandler | null,
) {
inject(DestroyRef).onDestroy(() => {
this._hooks$.next({ destroy: true });
this.subscription.unsubscribe();
});
}

private static nextId = 0;
private readonly destroyRef = inject(DestroyRef);
private readonly errorHandler = inject(ErrorHandler, { optional: true });
readonly _hooks$ = new Subject<DestroyProp>();
private readonly observables$ = new Subject<Observable<unknown>>();
// we have to use publish here to make it hot (composition happens without subscriber)
Expand All @@ -91,6 +78,13 @@ export class RxEffects implements OnDestroy$ {
onDestroy$: Observable<boolean> = this._hooks$.pipe(toHook('destroy'));
private readonly destroyers: Record<number, Subject<void>> = {};

constructor() {
this.destroyRef.onDestroy(() => {
this._hooks$.next({ destroy: true });
this.subscription.unsubscribe();
});
}

/**
* Performs a side-effect whenever a source observable emits, and handles its subscription.
*
Expand Down Expand Up @@ -171,7 +165,10 @@ export class RxEffects implements OnDestroy$ {
}
const effectId = RxEffects.nextId++;
const destroy$ = (this.destroyers[effectId] = new Subject<void>());
const applyBehavior = pipe(mapTo(effectId), takeUntil(destroy$));
const applyBehavior = pipe(
map(() => effectId),
takeUntil(destroy$),
);
if (fnOrObj != null) {
this.observables$.next(
from(obsOrSub).pipe(
Expand Down Expand Up @@ -233,7 +230,7 @@ export class RxEffects implements OnDestroy$ {
untilEffect(effectId: number) {
return <V>(source: Observable<V>) =>
source.pipe(
untilDestroyed(this),
takeUntilDestroyed(this.destroyRef),
takeUntil(this.effects$.pipe(filter((eId) => eId === effectId))),
);
}
Expand Down
25 changes: 6 additions & 19 deletions libs/state/effects/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { MonoTypeOperatorFunction, Observable } from 'rxjs';
import { filter, map, shareReplay, take, takeUntil } from 'rxjs/operators';
import { HookProps, OnDestroy$, SingleShotProps } from './model';
import { Observable } from 'rxjs';
import { filter, map, shareReplay, take } from 'rxjs/operators';
import { HookProps, SingleShotProps } from './model';

export function isSingleShotHookNameGuard<T>(
name: unknown
name: unknown,
): name is keyof SingleShotProps {
return !!name && typeof name === 'string' && name !== '';
}
Expand All @@ -16,7 +16,7 @@ const singleShotOperators = (o$: Observable<true>): Observable<true> =>
o$.pipe(
filter((v) => v === true),
take(1),
shareReplay()
shareReplay(),
);

/**
Expand All @@ -32,19 +32,6 @@ export function toHook<H extends keyof HookProps>(name: H) {
return (o$: Observable<HookProps>): Observable<HookProps[H]> =>
o$.pipe(
map((p) => p[name]),
operators
operators,
);
}

/**
* This operator can be used to take instances that implements `OnDestroy$` and unsubscribes from the given Observable when the instances
* `onDestroy$` Observable emits.
*
* @param instanceWithLifecycle
*/
export function untilDestroyed<V>(
instanceWithLifecycle: OnDestroy$
): MonoTypeOperatorFunction<V> {
return (source) =>
source.pipe(takeUntil<V>(instanceWithLifecycle.onDestroy$));
}
Loading
0