Skip to content

Commit

Permalink
Merge pull request mirrorjs#47 from madisvain/master
Browse files Browse the repository at this point in the history
extra reducers possibilty
  • Loading branch information
llh911001 authored Sep 11, 2017
2 parents 496cbf0 + 852e1cb commit e849f3c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ 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),

}

const historyModes = ['browser', 'hash', 'memory']
const isObject = target => Object.prototype.toString.call(target) === '[object Object]'

export default function defaults(opts = {}) {

const {
historyMode,
middlewares,
reducers,
addEffect,
} = opts

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

if (reducers && !isObject(reducers)) {
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
13 changes: 7 additions & 6 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {getHistory} from './router'

export let store

export function createStore(models, initialState, middlewares = []) {
export function createStore(models, reducers, initialState, middlewares = []) {

const middleware = applyMiddleware(
routerMiddleware(getHistory()),
Expand All @@ -32,28 +32,29 @@ export function createStore(models, initialState, middlewares = []) {
}
}

const reducer = createReducer(models)
const reducer = createReducer(models, reducers)
const enhancer = composeEnhancers(...enhancers)

store = _createStore(reducer, initialState, enhancer)

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
14 changes: 14 additions & 0 deletions test/defaults.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ describe('mirror.defaults', () => {
}).not.toThrow()
})

it('throws if reducers is not object', () => {
expect(() => {
defaults({
reducers: () => []
})
}).toThrow(/invalid/)

expect(() => {
defaults({
reducers: {}
})
}).not.toThrow()
})

it('throws if an addEffect is not a function that returns a function', () => {
expect(() => {
defaults({
Expand Down

0 comments on commit e849f3c

Please sign in to comment.