Skip to content

Instantly share code, notes, and snippets.

@trumbitta
Last active June 29, 2020 22:56
Show Gist options
  • Save trumbitta/1d1e962aa72bc785c2cbaee21cc19879 to your computer and use it in GitHub Desktop.
Save trumbitta/1d1e962aa72bc785c2cbaee21cc19879 to your computer and use it in GitHub Desktop.
Custom operator for using portions of state$ in redux-observable epics
function withLatestFromSelector<A, R, S, I>(
state$: StateObservable<A>,
selector: OutputSelector<A, R, S>,
): OperatorFunction<I, Array<I | R>> {
return (input$) =>
input$.pipe(
withLatestFrom(state$),
map(([input, state]) => [input, selector(state)]),
);
}
export const myEpic: Epic = (action$: ActionsObservable<Action>, state$: StateObservable<AppState>) =>
action$.pipe(
ofType(myActions.fooAction.type),
withLatestFromSelector(state$, mySelector),
tap(([action, fromSelector]) => console.log(action, fromSelector)),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment