Process action creators too
This commit is contained in:
		
							parent
							
								
									16cdc1acf7
								
							
						
					
					
						commit
						444ef30587
					
				@ -34,6 +34,7 @@ export const createStore = options => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/** Turn a reducer definition object to a function
 | 
					/** Turn a reducer definition object to a function
 | 
				
			||||||
 * @param {Object} reducer: Object definition of a reducer: {<type>: (state, payload) => <newState>}
 | 
					 * @param {Object} reducer: Object definition of a reducer: {<type>: (state, payload) => <newState>}
 | 
				
			||||||
 | 
					 * @param {Object} options
 | 
				
			||||||
 * @returns {Function}: Proper reducer
 | 
					 * @returns {Function}: Proper reducer
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function makeReducerFn(reducer, { onUpdate, logging }) {
 | 
					function makeReducerFn(reducer, { onUpdate, logging }) {
 | 
				
			||||||
@ -49,17 +50,30 @@ function makeReducerFn(reducer, { onUpdate, logging }) {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/** Returns a set of actions that are bound to the store
 | 
					/** Produces a set of actions that are bound to a specific store
 | 
				
			||||||
 * @param {Object} actions: {<name>: payload => ({dispatch, type}) => }
 | 
					 * The thunks object can contain action creators and/or thunks.
 | 
				
			||||||
 | 
					 * - Action creators: args => action
 | 
				
			||||||
 | 
					 *   are dispatched directly when called
 | 
				
			||||||
 | 
					 * - Thunks args => dispatch => dispatch(action)
 | 
				
			||||||
 | 
					 *   are called with dispatch as first argument.
 | 
				
			||||||
 | 
					 * The dispatch function will be patched to insert an action type if not given.
 | 
				
			||||||
 | 
					 * This action type will be the SNAKE_CASE'd action name
 | 
				
			||||||
 | 
					 * @param {Object} thunks
 | 
				
			||||||
 | 
					 * @param {Function} dispatch
 | 
				
			||||||
 | 
					 * @returns {object}
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function bindThunks(thunks, dispatch) {
 | 
					function bindThunks(thunks, dispatch) {
 | 
				
			||||||
  const _thunks = {}
 | 
					  const boundThunks = {}
 | 
				
			||||||
  for (let name of Object.keys(thunks)) {
 | 
					  for (let name of Object.keys(thunks)) {
 | 
				
			||||||
    const defaultType = camelToSnakeCase(name).toUpperCase()
 | 
					    const defaultType = camelToSnakeCase(name).toUpperCase()
 | 
				
			||||||
    _thunks[name] = (...args) =>
 | 
					    const patchedDispatch = patchDispatch(dispatch, defaultType)
 | 
				
			||||||
      thunks[name](...args)(patchDispatch(dispatch, defaultType))
 | 
					    boundThunks[name] = (...args) => {
 | 
				
			||||||
 | 
					      const thunk = thunks[name](...args)
 | 
				
			||||||
 | 
					      if (typeof thunk !== 'function') patchedDispatch(thunk)
 | 
				
			||||||
 | 
					      else thunk(patchedDispatch)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  return _thunks
 | 
					  return boundThunks
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const camelToSnakeCase = str =>
 | 
					const camelToSnakeCase = str =>
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user