10000 Merge pull request #1838 from michaelbe812/f/rx-effects-refactoring · rx-angular/rx-angular@d3defff · GitHub
[go: up one dir, main page]

Skip to content

Commit d3defff

Browse files
authored
Merge pull request #1838 from michaelbe812/f/rx-effects-refactoring
refactor(state): drop untilDestroyed operator
2 parents fe74e09 + eeee73b commit d3defff

File tree

2 files changed

+24
-40
lines changed

2 files changed

+24
-40
lines changed

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
DestroyRef,
3-
ErrorHandler,
4-
inject,
5-
Injectable,
6-
Optional,
7-
} from '@angular/core';
1+
import { DestroyRef, ErrorHandler, inject, Injectable } from '@angular/core';
2+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
83
import {
94
EMPTY,
105
from,
@@ -18,14 +13,14 @@ import {
1813
import {
1914
catchError,
2015
filter,
21-
mapTo,
16+
map,
2217
mergeAll,
2318
share,
2419
takeUntil,
2520
tap,
2621
} from 'rxjs/operators';
2722
import { DestroyProp, OnDestroy$ } from './model';
28-
import { toHook, untilDestroyed } from './utils';
23+
import { toHook } from './utils';
2924

3025
/**
3126
* @deprecated - use rxEffects instead
@@ -72,17 +67,9 @@ import { toHook, untilDestroyed } from './utils';
7267
*/
7368
@Injectable()
7469
export class RxEffects implements OnDestroy$ {
75-
constructor(
76-
@Optional()
77-
private readonly errorHandler: ErrorHandler | null,
78-
) {
79-
inject(DestroyRef).onDestroy(() => {
80-
this._hooks$.next({ destroy: true });
81-
this.subscription.unsubscribe();
82-
});
83-
}
84-
8570
private static nextId = 0;
71+
private readonly destroyRef = inject(DestroyRef);
72+
private readonly errorHandler = inject(ErrorHandler, { optional: true });
8673
readonly _hooks$ = new Subject<DestroyProp>();
8774
private readonly observables$ = new Subject<Observable<unknown>>();
8875
// we have to use publish here to make it hot (composition happens without subscriber)
@@ -91,6 +78,13 @@ export class RxEffects implements OnDestroy$ {
9178
onDestroy$: Observable<boolean> = this._hooks$.pipe(toHook('destroy'));
9279
private readonly destroyers: Record<number, Subject<void>> = {};
9380

81+
constructor() {
82+
this.destroyRef.onDestroy(() => {
83+
this._hooks$.next({ destroy: true });
84+
this.subscription.unsubscribe();
85+
});
86+
}
87+
9488
/**
9589
* Performs a side-effect whenever a source observable emits, and handles its subscription.
9690
*
@@ -171,7 +165,10 @@ export class RxEffects implements OnDestroy$ {
171165
}
172166
const effectId = RxEffects.nextId++;
173167
const destroy$ = (this.destroyers[effectId] = new Subject<void>());
174-
const applyBehavior = pipe(mapTo(effectId), takeUntil(destroy$));
168+
const applyBehavior = pipe(
169+
map(() => effectId),
170+
takeUntil(destroy$),
171+
);
175172
if (fnOrObj != null) {
176173
this.observables$.next(
177174
from(obsOrSub).pipe(
@@ -233,7 +230,7 @@ export class RxEffects implements OnDestroy$ {
233230
untilEffect(effectId: number) {
234231
return <V>(source: Observable<V>) =>
235232
source.pipe(
236-
untilDestroyed(this),
233+
takeUntilDestroyed(this.destroyRef),
237234
takeUntil(this.effects$.pipe(filter((eId) => eId === effectId))),
238235
);
239236
}

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

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { MonoTypeOperatorFunction, Observable } from 'rxjs';
2-
import { filter, map, shareReplay, take, takeUntil } from 'rxjs/operators';
3-
import { HookProps, OnDestroy$, SingleShotProps } from './model';
1+
import { Observable } from 'rxjs';
2+
import { filter, map, shareReplay, take } from 'rxjs/operators';
3+
import { HookProps, SingleShotProps } from './model';
44

55
export function isSingleShotHookNameGuard<T>(
6-
name: unknown
6+
name: unknown,
77
): name is keyof SingleShotProps {
88
return !!name && typeof name === 'string' && name !== '';
99
}
@@ -16,7 +16,7 @@ const singleShotOperators = (o$: Observable<true>): Observable<true> =>
1616
o$.pipe(
1717
filter((v) => v === true),
1818
take(1),
19-
shareReplay()
19+
shareReplay(),
2020
);
2121

2222
/**
@@ -32,19 +32,6 @@ export function toHook<H extends keyof HookProps>(name: H) {
3232
return (o$: Observable<HookProps>): Observable<HookProps[H]> =>
3333
o$.pipe(
3434
map((p) => p[name]),
35-
operators
35+
operators,
3636
);
3737
}
38-
39-
/**
40-
* This operator can be used to take instances that implements `OnDestroy$` and unsubscribes from the given Observable when the instances
41-
* `onDestroy$` Observable emits.
42-
*
43-
* @param instanceWithLifecycle
44-
*/
45-
export function untilDestroyed<V>(
46-
instanceWithLifecycle: OnDestroy$
47-
): MonoTypeOperatorFunction<V> {
48-
return (source) =>
49-
source.pipe(takeUntil<V>(instanceWithLifecycle.onDestroy$));
50-
}

0 commit comments

Comments
 (0)
0