diff --git a/src/processModules.js b/src/processModules.js index f213284..5ee5b39 100644 --- a/src/processModules.js +++ b/src/processModules.js @@ -5,38 +5,38 @@ export default function processModules(modules) { .filter(module => ( typeof module.reducer == 'function' || typeof module.reducer == 'object' )) - .map(module => ( - typeof module.reducer == 'function' ? module.reducer : handleActions(module.reducer) - )) + .map(module => { + const reducer = typeof module.reducer == 'function' ? module.reducer : handleActions(module.reducer) + const path = module.selector ? module.selector.split('.') : [] + + return !path.length ? reducer : (state, action) => { + const selection = path.reduce((state, key) => state && state[key] || {}, state) + const result = reducer(selection, action) + const changes = path.concat(null).reverse().reduce( + (state, key) => key ? ({ [key]: state }) : result, {} + ) + + return Object.assign({}, state, changes) + } + }) const middlewares = modules - .filter(module => ( - typeof module.middleware == 'function' || typeof module.middleware == 'object' - )) - .map(module => ( - typeof module.middleware == 'function' ? module.middleware : handleMiddlewares(module.middleware) - )) + .map(module => module.middleware) + .filter(middleware => typeof middleware == 'function' || typeof middleware == 'object') + .map(middleware => typeof middleware == 'function' ? middleware : handleMiddlewares(middleware)) const enhancers = modules - .filter(module => ( - typeof module.enhancer == 'function' - )) .map(module => module.enhancer) + .filter(enhancer => typeof enhancer == 'function') return { reducers, middlewares, - enhancers + enhancers, } } -export function handleMiddlewares(listeners) { - return store => next => action => { - // Execute action listeners, if any was set for the current action. - if (listeners[action.type]) { - return listeners[action.type](store)(next)(action) - } - - return next(action) - } -} +export const handleMiddlewares = (listeners) => store => next => action => ( + // Execute action listeners, if any was set for the current action. + listeners[action.type] ? listeners[action.type](store)(next)(action) : next(action) +)