Skip to content

Commit

Permalink
Merge pull request #7 from FoxComm/fix/local-async-namespace
Browse files Browse the repository at this point in the history
don't apply async reducer by default
  • Loading branch information
eugene-sy authored Dec 27, 2016
2 parents be8f1ae + d30ef93 commit 9bcdb55
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// export common things

export createAsyncActions from './redux/async-utils';
export createAsyncActions from './redux/async-utils';
export makeLocalStore, { addAsyncReducer } from './redux/make-local-store';
19 changes: 15 additions & 4 deletions src/redux/make-local-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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),
};
}
}

0 comments on commit 9bcdb55

Please sign in to comment.