From d30ef93c12dff2ec8d22d9cd8a9cad31f9a1431d Mon Sep 17 00:00:00 2001 From: Andrey Rublev Date: Tue, 27 Dec 2016 19:00:43 +0700 Subject: [PATCH] don't apply async reducer by default --- src/index.js | 3 ++- src/redux/make-local-store.js | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index e18cb1e..c7edd2f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ // export common things -export createAsyncActions from './redux/async-utils'; \ No newline at end of file +export createAsyncActions from './redux/async-utils'; +export makeLocalStore, { addAsyncReducer } from './redux/make-local-store'; \ No newline at end of file diff --git a/src/redux/make-local-store.js b/src/redux/make-local-store.js index 7255591..b954b48 100644 --- a/src/redux/make-local-store.js +++ b/src/redux/make-local-store.js @@ -18,13 +18,10 @@ function getDisplayName(WrappedComponent) { // )(Component); export default function makeLocalStore(reducer, initialState = {}, middlewares = [thunk]) { return WrappedComponent => { - // async reducer is often needed - // so we include him by default - const finalReducer = reduceReducers(reducer, asyncReducer); class LocalStore extends Component { constructor(...args) { super(...args); - this.store = createStore(finalReducer, initialState, applyMiddleware(...middlewares)); + this.store = createStore(reducer, initialState, applyMiddleware(...middlewares)); } render() { @@ -43,3 +40,17 @@ export default function makeLocalStore(reducer, initialState = {}, middlewares = }; } +type ShouldReturnObject = (state: any, action: any) => Object; + +export function addAsyncReducer(reducer: ShouldReturnObject, namespace = 'asyncActions') { + if (!namespace) return reduceReducers(reducer, asyncReducer); + + return (state, action) => { + const newState = reducer(state, action); + return { + ...newState, + [namespace]: asyncReducer(state, action), + }; + } +} +