Skip to content

Commit

Permalink
initial extra reducers possibility
Browse files Browse the repository at this point in the history
  • Loading branch information
madisvain committed Sep 9, 2017
1 parent 8ed54a9 commit 23eb3fa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export const options = {
// A list of the standard Redux middleware
middlewares: [],

// A list of additional reducers
reducers: {},

// An overwrite of the existing effect handler
addEffect: addEffect(effects),

Expand All @@ -23,6 +26,7 @@ export default function defaults(opts = {}) {
const {
historyMode,
middlewares,
reducers,
addEffect,
} = opts

Expand All @@ -34,6 +38,10 @@ export default function defaults(opts = {}) {
throw new Error(`middlewares "${middlewares}" is invalid, must be an Array!`)
}

if (reducers && !Array.isArray(reducers) && (typeof obj === 'object')) {
throw new Error(`middlewares "${reducers}" is invalid, must be an Array!`)
}

if (addEffect) {
if (typeof addEffect !== 'function' || typeof addEffect({}) !== 'function') {
throw new Error(`addEffect "${addEffect}" is invalid, must be a function that returns a function`)
Expand Down
6 changes: 3 additions & 3 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ let Root

export default function render(component, container, callback) {

const {initialState, middlewares} = options
const {initialState, middlewares, reducers} = options

if (started) {

// If app has rendered, do `store.replaceReducer` to update store.
replaceReducer(store, models)
replaceReducer(store, models, reducers)

// Call `render` without arguments means *re-render*. Since store has updated,
// `component` will automatically be updated, so no need to `ReactDOM.render` again.
Expand All @@ -25,7 +25,7 @@ export default function render(component, container, callback) {
}

} else {
createStore(models, initialState, middlewares)
createStore(models, reducers, initialState, middlewares)
}

// Use named function get a proper displayName
Expand Down
9 changes: 5 additions & 4 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,21 @@ export function createStore(models, initialState, middlewares = []) {
return store
}

export function replaceReducer(store, models) {
const reducer = createReducer(models)
export function replaceReducer(store, models, reducers) {
const reducer = createReducer(models, reducers)
store.replaceReducer(reducer)
}

function createReducer(models) {
function createReducer(models, reducers) {

const reducers = models.reduce((acc, cur) => {
const modelReducers = models.reduce((acc, cur) => {
acc[cur.name] = cur.reducer
return acc
}, {})

return combineReducers({
...reducers,
...modelReducers,
routing: routerReducer
})

Expand Down

0 comments on commit 23eb3fa

Please sign in to comment.