I joined a project which is written in React.js with redux and saga. Reducers in the store are written with the use of pipe function:
export const HighestOrderReducer = (...higherOrderReducers) => (baseReducer) =>
higherOrderReducers.reduce(
(reducer, nextHigherOrderReducer) => nextHigherOrderReducer(reducer),
baseReducer
);
so, I have, for example a dispatch of an action:
dispatch(createSocketConnection(token));
that creates an action with type SOCKET_ACTION_TYPES.GET.START and payload token.
this is how the reducer for this slice looks like:
export default HighestOrderReducer (
withResetState(SOCKET_ACTION_TYPES.RESET_STATE, initialState),
withLoadable({
isLoadingActionType: [SOCKET_ACTION_TYPES.GET.START],
successActionType: [SOCKET_ACTION_TYPES.GET.SUCCESS],
errorActionType: [SOCKET_ACTION_TYPES.GET.ERROR]
})
)(reducer);
which is a pipe function, and can be re-written as
withLoadable({
isLoadingActionType: [SOCKET_ACTION_TYPES.GET.START],
successActionType: [SOCKET_ACTION_TYPES.GET.SUCCESS],
errorActionType: [SOCKET_ACTION_TYPES.GET.ERROR]
})
(withResetState(resetStateActionType, initialState)(reducer)(state, action))
(state, action)
Here are the withResetState, withLoadable and reducer functions:
// the first function of the pipeline
export const withResetState = (resetStateActionType, initialState) => (baseReducer) => (state, action) => {
const newState = action.type === resetStateActionType ? { ...initialState } : state;
return baseReducer(newState, action);
};
now, if you look at the withResetState function, with the given action type and reducer it is returning an object, initialState. My problem is that in order for the second function of the pipe, withLoadable, to work, the first one is supposed to return some sort of a function, which, it doesn't to the best of my knowledge.
So, could you please let me know weather I missed something and the first function does return a function or I found a bug in the existing project?