Created
March 19, 2019 13:42
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A size-optimized refactor of Redux's combineReducers. | |
// All safeguards removed. Use at your own risk. | |
// https://github.com/reduxjs/redux/blob/master/src/combineReducers.js | |
const combineReducers = reducers => (state, action) => { | |
let hasChanged | |
const nextState = Object.keys(reducers).reduce((result, key) => { | |
result[key] = reducers[key](state[key], action) | |
hasChanged = hasChanged || result[key] !== state[key] | |
return result | |
}, {}) | |
return hasChanged ? nextState : state | |
} | |
const combineStores = stores => { | |
const storesArray = Object.entries(stores) | |
const rootInitialState = {} | |
const reducers = {} | |
for (const [key, store] of storesArray) { | |
rootInitialState[key] = store.initialState | |
reducers[key] = store.reducer | |
} | |
const rootReducer = combineReducers(reducers) | |
return { rootReducer, rootInitialState } | |
} | |
export default combineStores |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment