All files / util state-management.js

93.1% Statements 54/58
90.7% Branches 39/43
95% Functions 19/20
93.1% Lines 54/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              40x     88x               14751x     80488x 3787x 76701x 58713x   80488x                     67x 67x   37x 27x   10x       10x               18x     63x                     16x   16x 244x 10x 4x 4x       234x     16x 72x 2x             70x     16x   8x                 35x                                               138x 2820x                     2820x 35x   72x                         490x 4x           486x         10x                                 2822x   2822x 1x         1x     2821x   2821x               6x 6x                 6x 6x   46x             20x 20x         26x             26x                                
import React, { isValidElement } from 'react';
import _ from 'lodash';
import { logger } from './logger';
import { createSelector } from 'reselect';
import createClass from 'create-react-class';
 
export function getDeepPaths(obj, path = []) {
	return _.reduce(
		obj,
		(terminalKeys, value, key) =>
			(_.isPlainObject(value)
				? terminalKeys.concat(getDeepPaths(value, path.concat(key)))
				: terminalKeys.concat([path.concat(key)])),
		[]
	);
}
 
export function omitFunctionPropsDeep(obj) {
	return _.reduce(
		obj,
		(memo, value, key) => {
			if (_.isPlainObject(value)) {
				memo[key] = omitFunctionPropsDeep(value);
			} else if (!_.isFunction(value)) {
				memo[key] = value;
			}
			return memo;
		},
		{}
	);
}
 
export function bindReducerToState(
	reducerFunction,
	{ getState, setState },
	path = []
) {
	const localPath = _.take(path, _.size(path) - 1);
	return _.assign(
		function(...args) {
			if (_.isEmpty(localPath)) {
				setState(reducerFunction(getState(), ...args));
			} else {
				const localNextState = reducerFunction(
					_.get(getState(), localPath),
					...args
				);
				setState(_.set(_.clone(getState()), localPath, localNextState));
			}
		},
		{ path }
	);
}
 
export function bindReducersToState(reducers, { getState, setState }) {
	return _.reduce(
		getDeepPaths(reducers),
		(memo, path) => {
			return _.set(
				memo,
				path,
				bindReducerToState(_.get(reducers, path), { getState, setState }, path)
			);
		},
		{}
	);
}
 
export function getStatefulPropsContext(reducers, { getState, setState }) {
	const boundReducers = bindReducersToState(reducers, { getState, setState });
 
	const combineFunctionsCustomizer = (objValue, srcValue) => {
		if (_.isFunction(srcValue) && _.isFunction(objValue)) {
			return function(...args) {
				objValue(...args);
				return srcValue(...args);
			};
		}
 
		return safeMerge(objValue, srcValue);
	};
 
	const bindFunctionOverwritesCustomizer = (objValue, srcValue) => {
		if (_.isFunction(srcValue) && _.isFunction(objValue)) {
			return bindReducerToState(
				srcValue,
				{ getState, setState },
				objValue.path
			);
		}
 
		return safeMerge(objValue, srcValue);
	};
 
	return {
		getPropReplaceReducers(props) {
			return _.mergeWith(
				{},
				boundReducers,
				getState(),
				props,
				bindFunctionOverwritesCustomizer
			);
		},
		getProps(props) {
			return _.mergeWith(
				{},
				boundReducers,
				getState(),
				props,
				combineFunctionsCustomizer
			);
		},
	};
}
 
/**
 * reduceSelectors
 *
 * Generates a root selector from a tree of selectors
 * @param {Object} selectors - a tree of selectors
 * @returns {function} root selector that when called with state, calls each of
 * the selectors in the tree with the state local to that selector.
 *
 * This function is memoized because it's recursive, and we want it to reuse
 * the functions created in the recursive reduce because those functions are
 * also memoized (reselect selectors are memoized with a cache of 1) and we want
 * to maintain their caches.
 */
export const reduceSelectors = _.memoize(selectors => {
	Iif (!_.isPlainObject(selectors)) {
		throw new Error(
			'Selectors must be a plain object with function or plain object values'
		);
	}
 
	/**
	 * For each iteration of `reduceSelectors`, we return a memoized selector so
	 * that individual branches maintain reference equality if they haven't been
	 * modified, even if a sibling (and therefore the parent) has been modified.
	 */
	return createSelector(_.identity, state =>
		_.reduce(
			selectors,
			(acc, selector, key) => ({
				...acc,
				[key]: _.isFunction(selector)
					? selector(state)
					: reduceSelectors(selector)(state[key]),
			}),
			state
		)
	);
});
 
export function safeMerge(objValue, srcValue) {
	// don't merge arrays
	if (_.isArray(srcValue) && _.isArray(objValue)) {
		return srcValue;
	}
 
	// guards against traversing react elements which can cause cyclical recursion
	// If we don't have this clause, lodash (as of 4.7.0) will attempt to
	// deeply clone the react children, which is really freaking slow.
	if (
		isValidElement(srcValue) ||
		(_.isArray(srcValue) && _.some(srcValue, isValidElement)) ||
		(_.isArray(srcValue) && _.isUndefined(objValue))
	) {
		return srcValue;
	}
}
 
export function buildHybridComponent(
	baseComponent,
	{
		replaceEvents = false, // if true, function props replace the existing reducers, else they are invoked *after* state reducer returns
		reducers = _.get(baseComponent, 'definition.statics.reducers', {}),
		selectors = _.get(baseComponent, 'definition.statics.selectors', {}),
	} = {}
) {
	const {
		_isLucidHybridComponent,
		displayName,
		propTypes,
		definition: { statics = {} } = {},
	} = baseComponent;
 
	if (_isLucidHybridComponent) {
		logger.warnOnce(
			displayName,
			`Lucid: you are trying to apply buildHybridComponent to ${displayName}, which is already a hybrid component. Lucid exports hybrid components by default. To access the dumb components, use the -Dumb suffix, e.g. "ComponentDumb"`
		);
 
		return baseComponent;
	}
 
	const selector = reduceSelectors(selectors);
 
	return createClass({
		propTypes,
		statics: {
			_isLucidHybridComponent: true,
			...statics,
		},
		displayName,
		getInitialState() {
			const { initialState } = this.props; //initial state overrides
			return _.mergeWith(
				{},
				omitFunctionPropsDeep(baseComponent.getDefaultProps()),
				initialState,
				omitFunctionPropsDeep(this.props),
				safeMerge
			);
		},
		componentWillMount() {
			let synchronousState = this.state; //store reference to state, use in place of `this.state` in `getState`
			this.boundContext = getStatefulPropsContext(reducers, {
				getState: () =>
					_.mergeWith(
						{},
						omitFunctionPropsDeep(synchronousState),
						omitFunctionPropsDeep(this.props),
						safeMerge
					),
				setState: state => {
					synchronousState = state; //synchronously update the state reference
					this.setState(state);
				},
			});
		},
		render() {
			Iif (replaceEvents) {
				return React.createElement(
					baseComponent,
					selector(this.boundContext.getPropReplaceReducers(this.props)),
					this.props.children
				);
			}
			return React.createElement(
				baseComponent,
				selector(this.boundContext.getProps(this.props)),
				this.props.children
			);
		},
	});
}
 
export function buildStatefulComponent(...args) {
	logger.warnOnce(
		'buildHybridComponent-once',
		'Lucid: buildStatefulComponent has been renamed to buildHybridComponent.'
	);
	return buildHybridComponent(...args);
}