88 lines
3.9 KiB
JavaScript
88 lines
3.9 KiB
JavaScript
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
var _excluded = ["initMapStateToProps", "initMapDispatchToProps", "initMergeProps"];
|
|
import verifySubselectors from './verifySubselectors';
|
|
export function impureFinalPropsSelectorFactory(mapStateToProps, mapDispatchToProps, mergeProps, dispatch) {
|
|
return function impureFinalPropsSelector(state, ownProps) {
|
|
return mergeProps(mapStateToProps(state, ownProps), mapDispatchToProps(dispatch, ownProps), ownProps);
|
|
};
|
|
}
|
|
export function pureFinalPropsSelectorFactory(mapStateToProps, mapDispatchToProps, mergeProps, dispatch, _ref) {
|
|
var areStatesEqual = _ref.areStatesEqual,
|
|
areOwnPropsEqual = _ref.areOwnPropsEqual,
|
|
areStatePropsEqual = _ref.areStatePropsEqual;
|
|
var hasRunAtLeastOnce = false;
|
|
var state;
|
|
var ownProps;
|
|
var stateProps;
|
|
var dispatchProps;
|
|
var mergedProps;
|
|
|
|
function handleFirstCall(firstState, firstOwnProps) {
|
|
state = firstState;
|
|
ownProps = firstOwnProps;
|
|
stateProps = mapStateToProps(state, ownProps);
|
|
dispatchProps = mapDispatchToProps(dispatch, ownProps);
|
|
mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
|
|
hasRunAtLeastOnce = true;
|
|
return mergedProps;
|
|
}
|
|
|
|
function handleNewPropsAndNewState() {
|
|
stateProps = mapStateToProps(state, ownProps);
|
|
if (mapDispatchToProps.dependsOnOwnProps) dispatchProps = mapDispatchToProps(dispatch, ownProps);
|
|
mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
|
|
return mergedProps;
|
|
}
|
|
|
|
function handleNewProps() {
|
|
if (mapStateToProps.dependsOnOwnProps) stateProps = mapStateToProps(state, ownProps);
|
|
if (mapDispatchToProps.dependsOnOwnProps) dispatchProps = mapDispatchToProps(dispatch, ownProps);
|
|
mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
|
|
return mergedProps;
|
|
}
|
|
|
|
function handleNewState() {
|
|
var nextStateProps = mapStateToProps(state, ownProps);
|
|
var statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps);
|
|
stateProps = nextStateProps;
|
|
if (statePropsChanged) mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
|
|
return mergedProps;
|
|
}
|
|
|
|
function handleSubsequentCalls(nextState, nextOwnProps) {
|
|
var propsChanged = !areOwnPropsEqual(nextOwnProps, ownProps);
|
|
var stateChanged = !areStatesEqual(nextState, state, nextOwnProps, ownProps);
|
|
state = nextState;
|
|
ownProps = nextOwnProps;
|
|
if (propsChanged && stateChanged) return handleNewPropsAndNewState();
|
|
if (propsChanged) return handleNewProps();
|
|
if (stateChanged) return handleNewState();
|
|
return mergedProps;
|
|
}
|
|
|
|
return function pureFinalPropsSelector(nextState, nextOwnProps) {
|
|
return hasRunAtLeastOnce ? handleSubsequentCalls(nextState, nextOwnProps) : handleFirstCall(nextState, nextOwnProps);
|
|
};
|
|
} // TODO: Add more comments
|
|
// If pure is true, the selector returned by selectorFactory will memoize its results,
|
|
// allowing connectAdvanced's shouldComponentUpdate to return false if final
|
|
// props have not changed. If false, the selector will always return a new
|
|
// object and shouldComponentUpdate will always return true.
|
|
|
|
export default function finalPropsSelectorFactory(dispatch, _ref2) {
|
|
var initMapStateToProps = _ref2.initMapStateToProps,
|
|
initMapDispatchToProps = _ref2.initMapDispatchToProps,
|
|
initMergeProps = _ref2.initMergeProps,
|
|
options = _objectWithoutPropertiesLoose(_ref2, _excluded);
|
|
|
|
var mapStateToProps = initMapStateToProps(dispatch, options);
|
|
var mapDispatchToProps = initMapDispatchToProps(dispatch, options);
|
|
var mergeProps = initMergeProps(dispatch, options);
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
verifySubselectors(mapStateToProps, mapDispatchToProps, mergeProps, options.displayName);
|
|
}
|
|
|
|
var selectorFactory = options.pure ? pureFinalPropsSelectorFactory : impureFinalPropsSelectorFactory;
|
|
return selectorFactory(mapStateToProps, mapDispatchToProps, mergeProps, dispatch, options);
|
|
} |