Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selector proposal #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions src/processModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)