Skip to content

Commit

Permalink
Improved syntax for modules processor.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasconstantino committed Jul 17, 2016
1 parent decd182 commit be6dd3e
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions src/processModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

0 comments on commit be6dd3e

Please sign in to comment.