selectorFactory.js 3.9 KB

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