UNPKG

@equinor/fusion-observable

Version:
38 lines 1.35 kB
import { useLayoutEffect } from 'react'; /** * React hook that attaches a flow (epic-style function) to a {@link FlowSubject}. * * The flow receives the action stream and state observable, and must return an * observable of new actions. The subscription is managed by the hook lifecycle. * * **Important:** The observable returned from the flow must emit new actions. * Returning the same action will cause an infinite loop. * * @template S - The state type. * @template A - The action type. * @param subject - The `FlowSubject` to attach the flow to. * @param epic - A flow function `(action$, state$) => Observable<Action>`. * * @example * ```tsx * useObservableFlow(subject, (action$) => * action$.pipe( * filterAction('search'), * debounceTime(300), * switchMap((a) => fetchResults(a.payload).pipe( * map((res) => ({ type: 'searchResult', payload: res })), * )), * ), * ); * ``` */ export const useObservableFlow = (subject, epic) => { useLayoutEffect(() => { const subscription = subject.addFlow(epic); return () => subscription.unsubscribe(); }, [subject, epic]); }; /** @deprecated Renamed to {@link useObservableFlow} since 8.0.3. */ export const useObservableEpic = useObservableFlow; export default useObservableFlow; //# sourceMappingURL=useObservableFlow.js.map