selectorFactory.js 4.2 KB

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