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

Skip to content

Commit 17d4823

Browse files
committed
refactor(state): rename onCleanup to onDestroy
1 parent 9c65994 commit 17d4823

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

libs/state/effects/src/lib/rx-effects.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ describe(rxEffects, () => {
4343
expect(spyError).toHaveBeenCalledTimes(1);
4444
});
4545

46-
it('should call onCleanup', () => {
46+
it('should call onDestroy', () => {
4747
const spySideEffect = jest.fn();
4848
const spyInternalOnCleanup = jest.fn();
4949

50-
const { fixture, component } = setupComponent(({ register, onCleanup }) => {
50+
const { fixture, component } = setupComponent(({ register, onDestroy }) => {
5151
register(of('src').pipe(tap(spySideEffect)));
52-
onCleanup(spyInternalOnCleanup);
52+
onDestroy(spyInternalOnCleanup);
5353
});
5454
const spyOnCleanup = jest.fn();
55-
component.effects.onCleanup(spyOnCleanup);
55+
component.effects.onDestroy(spyOnCleanup);
5656

5757
expect(spySideEffect).toHaveBeenCalled();
5858
expect(spyInternalOnCleanup).not.toHaveBeenCalled();

libs/state/effects/src/lib/rx-effects.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ import { SideEffectFnOrObserver, SideEffectObservable } from './types';
99

1010
type RxEffects<T> = {
1111
register: RegisterFn<T>;
12-
onCleanup: OnCleanupFn<T>;
12+
onDestroy: (fn: Fn) => Fn;
1313
};
1414
type RegisterFn<T> = (
1515
observable: SideEffectObservable<T>,
1616
sideEffectOrObserver?: SideEffectFnOrObserver<T>
1717
) => () => void;
18-
type OnCleanupFn<T> = (sideEffect: () => void) => void;
18+
type Fn = () => void;
1919

20-
export type RxEffectsSetupFn<T> = (cfg: {
21-
register: RegisterFn<T>;
22-
onCleanup: OnCleanupFn<T>;
23-
}) => void;
20+
export type RxEffectsSetupFn<T> = (
21+
cfg: Pick<RxEffects<T>, 'register' | 'onDestroy'>
22+
) => void;
2423

2524
/**
2625
* @description
@@ -61,9 +60,7 @@ export function rxEffects<T>(setupFn?: RxEffectsSetupFn<T>): RxEffects<T> {
6160
const errorHandler = inject(ErrorHandler, { optional: true });
6261
const destroyRef = inject(DestroyRef);
6362
const runningEffects: Subscription[] = [];
64-
const destroyEf = destroyRef.onDestroy(() =>
65-
runningEffects.forEach((ef) => ef.unsubscribe())
66-
);
63+
destroyRef.onDestroy(() => runningEffects.forEach((ef) => ef.unsubscribe()));
6764

6865
/**
6966
* Subscribe to observables and trigger side effect.
@@ -79,8 +76,10 @@ export function rxEffects<T>(setupFn?: RxEffectsSetupFn<T>): RxEffects<T> {
7976
* }
8077
* }
8178
*
82-
* @param obs$ Source observable input
83-
* @param observer Observer object
79+
* @param {SideEffectObservable} obs$ Source observable input
80+
* @param {SideEffectFnOrObserver} sideEffect Observer object
81+
*
82+
* @return {Function} - unregisterFn
8483
*/
8584
function register(
8685
obs$: SideEffectObservable<T>,
@@ -91,12 +90,15 @@ export function rxEffects<T>(setupFn?: RxEffectsSetupFn<T>): RxEffects<T> {
9190
? {
9291
...sideEffect,
9392
// preserve original logic
94-
error: (e) => {
93+
error: (e: unknown) => {
9594
sideEffect?.error(e);
9695
errorHandler.handleError(e);
9796
},
9897
}
99-
: { next: sideEffect, error: (e) => errorHandler.handleError(e) };
98+
: {
99+
next: sideEffect,
100+
error: (e: unknown) => errorHandler.handleError(e),
101+
};
100102
const sub = from(obs$).subscribe(observer);
101103
runningEffects.push(sub);
102104
return () => sub.unsubscribe();
@@ -111,21 +113,22 @@ export function rxEffects<T>(setupFn?: RxEffectsSetupFn<T>): RxEffects<T> {
111113
* template: `<button name="save" (click)="save()">Save</button>`
112114
* })
113115
* class ListComponent {
114-
* private ef = rxEffects(({onCleanup}) => {
115-
* onCleanup(() => console.log('done'));
116+
* private ef = rxEffects(({onDestroy}) => {
117+
* onDestroy(() => console.log('done'));
116118
* }
117119
* }
118120
*
119-
* @param obs$ Source observable input
120-
* @param observer Observer object
121+
* @param {Fn} callback onDestroy callback
122+
*
123+
* @return {Fn} unregisterFn
121124
*/
122-
function onCleanup(fn) {
123-
destroyRef.onDestroy(fn);
125+
function onDestroy(callback: Fn): Fn {
126+
return destroyRef.onDestroy(callback);
124127
}
125128

126129
const effects = {
127130
register,
128-
onCleanup,
131+
onDestroy,
129132
};
130133

131134
setupFn?.(effects);

0 commit comments

Comments
 (0)