selectorFactory.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
  2. import verifySubselectors from './verifySubselectors';
  3. export function impureFinalPropsSelectorFactory(mapStateToProps, mapDispatchToProps, mergeProps, dispatch) {
  4. return function impureFinalPropsSelector(state, ownProps) {
  5. return mergeProps(mapStateToProps(state, ownProps), mapDispatchToProps(dispatch, ownProps), ownProps);
  6. };
  7. }
  8. export function pureFinalPropsSelectorFactory(mapStateToProps, mapDispatchToProps, mergeProps, dispatch, _ref) {
  9. var areStatesEqual = _ref.areStatesEqual,
  10. areOwnPropsEqual = _ref.areOwnPropsEqual,
  11. areStatePropsEqual = _ref.areStatePropsEqual;
  12. var hasRunAtLeastOnce = false;
  13. var state;
  14. var ownProps;
  15. var stateProps;
  16. var dispatchProps;
  17. var mergedProps;
  18. function handleFirstCall(firstState, firstOwnProps) {
  19. state = firstState;
  20. ownProps = firstOwnProps;
  21. stateProps = mapStateToProps(state, ownProps);
  22. dispatchProps = mapDispatchToProps(dispatch, ownProps);
  23. mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
  24. hasRunAtLeastOnce = true;
  25. return mergedProps;
  26. }
  27. function handleNewPropsAndNewState() {
  28. stateProps = mapStateToProps(state, ownProps);
  29. if (mapDispatchToProps.dependsOnOwnProps) dispatchProps = mapDispatchToProps(dispatch, ownProps);
  30. mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
  31. return mergedProps;
  32. }
  33. function handleNewProps() {
  34. if (mapStateToProps.dependsOnOwnProps) stateProps = mapStateToProps(state, ownProps);
  35. if (mapDispatchToProps.dependsOnOwnProps) dispatchProps = mapDispatchToProps(dispatch, ownProps);
  36. mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
  37. return mergedProps;
  38. }
  39. function handleNewState() {
  40. var nextStateProps = mapStateToProps(state, ownProps);
  41. var statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps);
  42. stateProps = nextStateProps;
  43. if (statePropsChanged) mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
  44. return mergedProps;
  45. }
  46. function handleSubsequentCalls(nextState, nextOwnProps) {
  47. var propsChanged = !areOwnPropsEqual(nextOwnProps, ownProps);
  48. var stateChanged = !areStatesEqual(nextState, state);
  49. state = nextState;
  50. ownProps = nextOwnProps;
  51. if (propsChanged && stateChanged) return handleNewPropsAndNewState();
  52. if (propsChanged) return handleNewProps();
  53. if (stateChanged) return handleNewState();
  54. return mergedProps;
  55. }
  56. return function pureFinalPropsSelector(nextState, nextOwnProps) {
  57. return hasRunAtLeastOnce ? handleSubsequentCalls(nextState, nextOwnProps) : handleFirstCall(nextState, nextOwnProps);
  58. };
  59. } // TODO: Add more comments
  60. // If pure is true, the selector returned by selectorFactory will memoize its results,
  61. // allowing connectAdvanced's shouldComponentUpdate to return false if final
  62. // props have not changed. If false, the selector will always return a new
  63. // object and shouldComponentUpdate will always return true.
  64. export default function finalPropsSelectorFactory(dispatch, _ref2) {
  65. var initMapStateToProps = _ref2.initMapStateToProps,
  66. initMapDispatchToProps = _ref2.initMapDispatchToProps,
  67. initMergeProps = _ref2.initMergeProps,
  68. options = _objectWithoutPropertiesLoose(_ref2, ["initMapStateToProps", "initMapDispatchToProps", "initMergeProps"]);
  69. var mapStateToProps = initMapStateToProps(dispatch, options);
  70. var mapDispatchToProps = initMapDispatchToProps(dispatch, options);
  71. var mergeProps = initMergeProps(dispatch, options);
  72. if (process.env.NODE_ENV !== 'production') {
  73. verifySubselectors(mapStateToProps, mapDispatchToProps, mergeProps, options.displayName);
  74. }
  75. var selectorFactory = options.pure ? pureFinalPropsSelectorFactory : impureFinalPropsSelectorFactory;
  76. return selectorFactory(mapStateToProps, mapDispatchToProps, mergeProps, dispatch, options);
  77. }