All files / util redux.js

96.55% Statements 56/58
85.42% Branches 41/48
96.15% Functions 25/26
96.55% Lines 56/58
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299                            3x 3x                                                 7x               7x                       7x 7x 7x 3x 7x 3x   7x 2x 7x   3x               3x     7x                                   6x 2x 4x 4x 4x 4x     4x   1x 4x                   4x 4x   4x                                 6x     10x 10x                                       2x 2x   2x             2x                           21x       30x 2x   28x 28x       10x 10x 7x   3x                                   17x 15x     15x     20x                                               7x 7x                       6x   10x       8x 8x                                 135x         7x    
import _ from 'lodash';
import { createSelector } from 'reselect';
import { reduceSelectors, safeMerge } from './state-management.js';
import { logger, isDevMode } from './logger.js';
 
/**
 * thunk
 *
 * Marks a function on the reducer tree as a thunk action creator so it doesn't
 * get incorporated into the redux reducer
 *
 * @return {function} with `isThunk` set to `true`
 */
export function thunk(fn) {
	fn.isThunk = true;
	return fn;
}
 
/**
 * getReduxPrimitives
 *
 * Creates a redux reducer and connectors (inputs to redux-react's `connect`)
 *
 * @param {Object} param
 * @param {Object} param.initialState - the initial state object that the reducer will return
 * @param {Object} param.reducers - a tree of lucid reducers
 * @param {string[]} param.rootPath - array of strings representing the path to local state in global state
 * @param {function} param.rootSelector - a top-level selector which takes as input state that has run through every selector in param.selectors
 * @param {Object} param.selectors - a tree of lucid selectors
 * @return {Object} redux reducer and connectors
 */
 
export function getReduxPrimitives({
	initialState,
	reducers,
	rootPath = [],
	rootSelector = _.identity,
	selectors,
}) {
	/* istanbul ignore if */
	if (isDevMode && _.isEmpty(rootPath)) {
		logger.warn(
			`\`getReduxPrimitives\` warning:
\`rootPath\` is empty`
		);
	}
 
	/* istanbul ignore if */
	if (isDevMode && !initialState) {
		logger.warn(
			`\`getReduxPrimitives\` warning:
Missing \`initialState\` for component at \`rootPath\` ${_.isArray(rootPath) ? rootPath.join(',') : rootPath}
Components should have an \`initialState\` property or a \`getDefaultProps\` defined.
`
		);
	}
 
	// we need this in scope so actionCreators can refer to it
	let dispatchTree;
 
	const reducer = createReduxReducer(reducers, initialState, rootPath);
	const selector = selectors ? reduceSelectors(selectors) : _.identity;
	const rootPathSelector = state =>
		(_.isEmpty(rootPath) ? state : _.get(state, rootPath));
	const mapStateToProps = createSelector([rootPathSelector], rootState =>
		rootSelector(selector(rootState))
	);
	const mapDispatchToProps = dispatch =>
		getDispatchTree(reducers, rootPath, dispatch);
	const devModeMapStateToProps = rootState => {
		/* istanbul ignore if */
		if (isDevMode && !_.has(rootState, rootPath)) {
			logger.warn(
				`\`getReduxPrimitives\` warning:
\`rootPath\` ${rootPath} does not exist in the redux store.
Make sure your \`rootPath\` is correct.
`
			);
		}
		return mapStateToProps(rootState);
	};
 
	return {
		reducer,
		connectors: [
			isDevMode ? devModeMapStateToProps : mapStateToProps,
			mapDispatchToProps,
			mergeProps,
		],
	};
 
	/**
	 * createActionCreator
	 *
	 * @param {function} node - a node in the the reducer tree, either a reducer or a thunk
	 * @param {string[]} path - the path to the reducer in the reducer tree
	 * @param {string[]} rootPath - array of strings representing the path to local state in global state
	 * @return {function} action creator that returns either an action or a thunk
	 */
	function createActionCreator(node, rootPath, path) {
		if (node.isThunk) {
			return function thunk(...args) {
				return function thunkInner(dispatch, getState, ...rest) {
					const pathToLocalDispatchTree = _.slice(path, rootPath.length, -1);
					const pathToLocalState = _.dropRight(path);
					const localDispatchTree = _.isEmpty(pathToLocalDispatchTree)
						? dispatchTree
						: _.get(dispatchTree, pathToLocalDispatchTree);
					const getLocalState = _.isEmpty(pathToLocalState)
						? getState
						: () => _.get(getState(), pathToLocalState);
					return node(...args)(
						localDispatchTree,
						getLocalState,
						dispatch,
						getState,
						...rest
					);
				};
			};
		}
		return function actionCreator(...args) {
			const [payload, ...meta] = isDevMode ? cleanArgs(args) : args;
 
			return {
				type: path.join('.'),
				payload,
				meta,
			};
		};
	}
 
	/**
	 * createActionCreatorTree
	 *
	 * Walks the reducer tree and generates a tree of action creators that correspond to each reducer
	 * @param {Object} reducers - a tree of lucid reducers
	 * @param {string[]} rootPath - array of strings representing the path to local state in global state
	 * @returns {Object} action creator tree
	 */
	function createActionCreatorTree(reducers, rootPath, path = rootPath) {
		return _.reduce(
			reducers,
			(memo, node, key) => {
				const currentPath = path.concat(key);
				return {
					...memo,
					[key]: _.isFunction(node)
						? createActionCreator(node, rootPath, currentPath)
						: createActionCreatorTree(node, rootPath, currentPath),
				};
			},
			{}
		);
	}
 
	/**
	 * getDispatchTree
	 *
	 * Walks the reducer tree and generates an action creator tree, then binds dispatch to each node
	 * @param {Object} reducers - a tree of lucid reducers
	 * @param {string[]} rootPath - array of strings representing the path to local state in global state
	 * @param {function} dispatch - the redux store's `dispatch` function
	 */
	function getDispatchTree(reducers, rootPath, dispatch) {
		const actionCreatorTree = createActionCreatorTree(reducers, rootPath);
		dispatchTree = bindActionCreatorTree(actionCreatorTree, dispatch);
		/* istanbul ignore if */
		Iif (isDevMode) {
			window.lucidReduxUtil = window.lucidReduxUtil || {};
			window.lucidReduxUtil[rootPath] = {
				actionCreatorTree,
				dispatchTree,
			};
		}
		return dispatchTree;
	}
}
 
/**
 * createReduxReducerTree
 *
 * Walks the reducer tree and generates a tree of redux reducers, converting the
 * signature from `(state, payload) => state` to `(state, action) => state`
 * @param {Object} reducers - a tree of lucid reducers
 * @param {string[]} path - array of strings representing the path to the reducer
 * @return {Object} redux reducer tree
 */
function createReduxReducerTree(reducers, path = []) {
	return _.reduce(
		reducers,
		(memo, node, key) => {
			// filter out thunks from the reducer tree
			if (node.isThunk) {
				return memo;
			}
			const currentPath = path.concat(key);
			return {
				...memo,
				[key]: _.isFunction(node)
					? function reduxReducer(state, action) {
							const { type, payload, meta = [] } = action;
							if (_.isUndefined(state) || type !== currentPath.join('.')) {
								return state;
							}
							return node(state, payload, ...meta);
						}
					: createReduxReducerTree(node, currentPath),
			};
		},
		{}
	);
}
 
/**
 * createReducerFromReducerTree
 *
 * Returns a function that calls every reducer in the reducer tree with the reducer's local state and action
 * @param {Object} reduxReducerTree - tree of redux reducers with signature `(state, action) => state`
 * @param {Object} initialState - the initial state object that the reducer will return
 * @return {function} the redux reducer
 */
function createReducerFromReducerTree(reduxReducerTree, initialState) {
	return function reduxReducer(state, action) {
		Iif (_.isUndefined(state)) {
			return initialState;
		}
		return _.reduce(
			reduxReducerTree,
			(state, node, key) => {
				return {
					...state,
					...(_.isFunction(node)
						? node(state, action)
						: {
								[key]: createReducerFromReducerTree(node)(state[key], action),
							}),
				};
			},
			state
		);
	};
}
 
/**
 * createReduxReducer
 *
 * Generates a redux reducer from a tree of lucid reducers
 * @param {Object} reducers - a tree of lucid reducers
 * @param {Object} initialState - the initial state object that the reducer will return
 * @param {string[]} rootPath - array of strings representing the path to part of global state this reducer applies to
 * @return {function} the redux reducer
 */
function createReduxReducer(reducers, initialState, rootPath) {
	const reducerTree = createReduxReducerTree(reducers, rootPath);
	return createReducerFromReducerTree(reducerTree, initialState);
}
 
/**
 * bindActionCreatorTree
 *
 * Binds redux store.dispatch to actionCreators in a tree
 * @param {Object} actionCreatorTree - a tree of redux action creator functions
 * @param {function} dispatch - the redux store's `dispatch` function
 * @param {string[]} path - array of strings representing the path to the action creator
 */
function bindActionCreatorTree(actionCreatorTree, dispatch, path = []) {
	return _.reduce(
		actionCreatorTree,
		(memo, node, key) => ({
			...memo,
			[key]: _.isFunction(node)
				? function boundActionCreator(...args) {
						const action = actionCreatorTree[key](...args);
						return dispatch(action);
					}
				: bindActionCreatorTree(node, dispatch, path.concat(key)),
		}),
		{}
	);
}
 
/**
 * mergeProps
 *
 * Merges state, dispatchTree, and ownProps into a single props object
 * @param {Object} state
 * @param {Object} dispatchTree
 * @param {Object} ownProps
 * @return {Object}
 */
const mergeProps = _.memoize((state, dispatchTree, ownProps) => {
	return _.mergeWith({}, state, dispatchTree, ownProps, safeMerge);
});
 
export function cleanArgs(args) {
	return _.has(_.last(args), 'event') ? _.dropRight(args) : args;
}