Open OliverJAsh opened 4 years ago
Something like this?
import { pipeWith } from 'pipe-ts';
import { useObservable } from 'rxjs-hooks';
import { RestrictArray } from 'rxjs-hooks/dist/cjs/type';
import * as Rx from 'shared/facades/rx';
/** `useObservable` is designed for creating state and using that in the component, but most of the
* time we just want to run side effects. This is a specialised version of `useObservable` that
* handles those use cases. https://github.com/LeetCode-OpenSource/rxjs-hooks/issues/98 */
export const useObservableSideEffect = <Inputs>(
runSideEffects: (inputs$: Rx.Observable<RestrictArray<Inputs>>) => Rx.Observable<unknown>,
inputs: RestrictArray<Inputs>,
): void => {
useObservable(
(_state$, inputs$) => pipeWith(inputs$, runSideEffects, Rx.ignoreElements()),
undefined,
inputs,
);
};
In Redux-Observable, for side-effects, I always use ignoreElements
.
This is a matter of auto-returning the value that goes through the stream. I wrote an article about a possible alternative, but I dunno if that's the intention of the library. It's add more complexity. https://itnext.io/the-best-practice-anti-pattern-5e8bd873aadf?source=rss----5b301f10ddcd---4
Having a hook for side-effects makes more sense because that's the same as useEffect
today.
Sometimes we want to use an observable not to create state but rather just to perform side effects.
This is possible using
useObservable
:… but it's a bit awkward for a few reasons:
Observable
of a certain type (State
), when the type doesn't matter to us, since the result is not being used outside of the hook. We need to domergeMapTo(EMPTY)
at the end of the chain, to satisfy the return type.State
here.What do you think about another hook which is specifically designed for this use case, to avoid the problems above?
I'm sure we can find a better name… 😄