103 lines
5.2 KiB
JavaScript
103 lines
5.2 KiB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
|
|
|
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
|
|
|
|
import connectAdvanced from '../components/connectAdvanced';
|
|
import shallowEqual from '../utils/shallowEqual';
|
|
import defaultMapDispatchToPropsFactories from './mapDispatchToProps';
|
|
import defaultMapStateToPropsFactories from './mapStateToProps';
|
|
import defaultMergePropsFactories from './mergeProps';
|
|
import defaultSelectorFactory from './selectorFactory';
|
|
|
|
/*
|
|
connect is a facade over connectAdvanced. It turns its args into a compatible
|
|
selectorFactory, which has the signature:
|
|
|
|
(dispatch, options) => (nextState, nextOwnProps) => nextFinalProps
|
|
|
|
connect passes its args to connectAdvanced as options, which will in turn pass them to
|
|
selectorFactory each time a Connect component instance is instantiated or hot reloaded.
|
|
|
|
selectorFactory returns a final props selector from its mapStateToProps,
|
|
mapStateToPropsFactories, mapDispatchToProps, mapDispatchToPropsFactories, mergeProps,
|
|
mergePropsFactories, and pure args.
|
|
|
|
The resulting final props selector is called by the Connect component instance whenever
|
|
it receives new props or store state.
|
|
*/
|
|
|
|
function match(arg, factories, name) {
|
|
for (var i = factories.length - 1; i >= 0; i--) {
|
|
var result = factories[i](arg);
|
|
if (result) return result;
|
|
}
|
|
|
|
return function (dispatch, options) {
|
|
throw new Error('Invalid value of type ' + typeof arg + ' for ' + name + ' argument when connecting component ' + options.wrappedComponentName + '.');
|
|
};
|
|
}
|
|
|
|
function strictEqual(a, b) {
|
|
return a === b;
|
|
}
|
|
|
|
// createConnect with default args builds the 'official' connect behavior. Calling it with
|
|
// different options opens up some testing and extensibility scenarios
|
|
export function createConnect() {
|
|
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
_ref$connectHOC = _ref.connectHOC,
|
|
connectHOC = _ref$connectHOC === undefined ? connectAdvanced : _ref$connectHOC,
|
|
_ref$mapStateToPropsF = _ref.mapStateToPropsFactories,
|
|
mapStateToPropsFactories = _ref$mapStateToPropsF === undefined ? defaultMapStateToPropsFactories : _ref$mapStateToPropsF,
|
|
_ref$mapDispatchToPro = _ref.mapDispatchToPropsFactories,
|
|
mapDispatchToPropsFactories = _ref$mapDispatchToPro === undefined ? defaultMapDispatchToPropsFactories : _ref$mapDispatchToPro,
|
|
_ref$mergePropsFactor = _ref.mergePropsFactories,
|
|
mergePropsFactories = _ref$mergePropsFactor === undefined ? defaultMergePropsFactories : _ref$mergePropsFactor,
|
|
_ref$selectorFactory = _ref.selectorFactory,
|
|
selectorFactory = _ref$selectorFactory === undefined ? defaultSelectorFactory : _ref$selectorFactory;
|
|
|
|
return function connect(mapStateToProps, mapDispatchToProps, mergeProps) {
|
|
var _ref2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
|
|
_ref2$pure = _ref2.pure,
|
|
pure = _ref2$pure === undefined ? true : _ref2$pure,
|
|
_ref2$areStatesEqual = _ref2.areStatesEqual,
|
|
areStatesEqual = _ref2$areStatesEqual === undefined ? strictEqual : _ref2$areStatesEqual,
|
|
_ref2$areOwnPropsEqua = _ref2.areOwnPropsEqual,
|
|
areOwnPropsEqual = _ref2$areOwnPropsEqua === undefined ? shallowEqual : _ref2$areOwnPropsEqua,
|
|
_ref2$areStatePropsEq = _ref2.areStatePropsEqual,
|
|
areStatePropsEqual = _ref2$areStatePropsEq === undefined ? shallowEqual : _ref2$areStatePropsEq,
|
|
_ref2$areMergedPropsE = _ref2.areMergedPropsEqual,
|
|
areMergedPropsEqual = _ref2$areMergedPropsE === undefined ? shallowEqual : _ref2$areMergedPropsE,
|
|
extraOptions = _objectWithoutProperties(_ref2, ['pure', 'areStatesEqual', 'areOwnPropsEqual', 'areStatePropsEqual', 'areMergedPropsEqual']);
|
|
|
|
var initMapStateToProps = match(mapStateToProps, mapStateToPropsFactories, 'mapStateToProps');
|
|
var initMapDispatchToProps = match(mapDispatchToProps, mapDispatchToPropsFactories, 'mapDispatchToProps');
|
|
var initMergeProps = match(mergeProps, mergePropsFactories, 'mergeProps');
|
|
|
|
return connectHOC(selectorFactory, _extends({
|
|
// used in error messages
|
|
methodName: 'connect',
|
|
|
|
// used to compute Connect's displayName from the wrapped component's displayName.
|
|
getDisplayName: function getDisplayName(name) {
|
|
return 'Connect(' + name + ')';
|
|
},
|
|
|
|
// if mapStateToProps is falsy, the Connect component doesn't subscribe to store state changes
|
|
shouldHandleStateChanges: Boolean(mapStateToProps),
|
|
|
|
// passed through to selectorFactory
|
|
initMapStateToProps: initMapStateToProps,
|
|
initMapDispatchToProps: initMapDispatchToProps,
|
|
initMergeProps: initMergeProps,
|
|
pure: pure,
|
|
areStatesEqual: areStatesEqual,
|
|
areOwnPropsEqual: areOwnPropsEqual,
|
|
areStatePropsEqual: areStatePropsEqual,
|
|
areMergedPropsEqual: areMergedPropsEqual
|
|
|
|
}, extraOptions));
|
|
};
|
|
}
|
|
|
|
export default createConnect(); |