From be6dd3e0e4a3dfa4b6db4ba85734c654cad1f04a Mon Sep 17 00:00:00 2001 From: Lucas Constantino Silva Date: Sun, 17 Jul 2016 14:04:09 -0300 Subject: [PATCH 1/3] Improved syntax for modules processor. --- src/processModules.js | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/src/processModules.js b/src/processModules.js index f213284..2de2c84 100644 --- a/src/processModules.js +++ b/src/processModules.js @@ -2,41 +2,27 @@ import {handleActions} from 'redux-actions' export default function processModules(modules) { const reducers = modules - .filter(module => ( - typeof module.reducer == 'function' || typeof module.reducer == 'object' - )) - .map(module => ( - typeof module.reducer == 'function' ? module.reducer : handleActions(module.reducer) - )) + .map(module => module.reducer) + .filter(reducer => typeof reducer == 'function' || typeof reducer == 'object') + .map(reducer => typeof reducer == 'function' ? reducer : handleActions(reducer)) 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) +) From 74c842b7e537eddb888634e3beed7bbddf5a7c7f Mon Sep 17 00:00:00 2001 From: Lucas Constantino Silva Date: Sun, 17 Jul 2016 16:06:04 -0300 Subject: [PATCH 2/3] Selector proposal. --- src/processModules.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/processModules.js b/src/processModules.js index 2de2c84..03ec2ad 100644 --- a/src/processModules.js +++ b/src/processModules.js @@ -2,9 +2,23 @@ import {handleActions} from 'redux-actions' export default function processModules(modules) { const reducers = modules - .map(module => module.reducer) - .filter(reducer => typeof reducer == 'function' || typeof reducer == 'object') - .map(reducer => typeof reducer == 'function' ? reducer : handleActions(reducer)) + .filter(module => ( + typeof module.reducer == 'function' || typeof module.reducer == 'object' + )) + .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 { ...state, ...changes } + } + }) const middlewares = modules .map(module => module.middleware) From 925310e097aff94168d3f9747ab5553f8f25406e Mon Sep 17 00:00:00 2001 From: Lucas Constantino Silva Date: Sun, 17 Jul 2016 17:06:53 -0300 Subject: [PATCH 3/3] Use object assign to avoid destructing conflicts between multiple selectors. --- src/processModules.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/processModules.js b/src/processModules.js index 03ec2ad..5ee5b39 100644 --- a/src/processModules.js +++ b/src/processModules.js @@ -16,7 +16,7 @@ export default function processModules(modules) { (state, key) => key ? ({ [key]: state }) : result, {} ) - return { ...state, ...changes } + return Object.assign({}, state, changes) } })