Skip to content

Commit

Permalink
reduce mount complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Dashney committed Jun 27, 2018
1 parent 71821d0 commit ef36823
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/mount/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
import get from 'lodash.get'
import warning from 'warning'
import globalizeActions from './globalize-actions'
import createActions from '../create-actions'
import mountActions from '../mount-actions'

export default function (pathToState, logic) {
export default function mount (pathToState, logic) {
warning(true, 'redux-modular mount() is deprecated')
validateArgs(...arguments)
return mountLogic(pathToState, logic)
}

function validateArgs (pathToState, logic) {
if (!logic) {
throw new Error('logic must be passed to mount')
}
}

function mountLogic (pathToState, logic) {
const result = {}

let { actions, reducer, selectors } = logic
if (logic.actions) {
result.actions = createAndMountActions(pathToState, logic.actions)
}

if (actions) {
actions = globalizeActions(pathToState, actions)
if (logic.actions && logic.reducer) {
result.reducer = logic.reducer(result.actions)
}

if (actions && reducer) {
reducer = reducer(actions)
if (logic.selectors) {
createSelectors(pathToState, logic.selectors)
}

if (selectors) {
const localStateSelector = pathToState
return result
}

function createAndMountActions (pathToState, actions) {
return mountActions(pathToState, createActions(actions))
}

function createSelectors (pathToState, selectors) {
const localStateSelector = pathToState
? state => get(state, pathToState)
: state => state

selectors = selectors(localStateSelector)
}

return {
actions,
reducer,
selectors
}
return selectors(localStateSelector)
}

0 comments on commit ef36823

Please sign in to comment.