connect.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
  3. var _excluded = ["pure", "areStatesEqual", "areOwnPropsEqual", "areStatePropsEqual", "areMergedPropsEqual"];
  4. import connectAdvanced from '../components/connectAdvanced';
  5. import shallowEqual from '../utils/shallowEqual';
  6. import defaultMapDispatchToPropsFactories from './mapDispatchToProps';
  7. import defaultMapStateToPropsFactories from './mapStateToProps';
  8. import defaultMergePropsFactories from './mergeProps';
  9. import defaultSelectorFactory from './selectorFactory';
  10. /*
  11. connect is a facade over connectAdvanced. It turns its args into a compatible
  12. selectorFactory, which has the signature:
  13. (dispatch, options) => (nextState, nextOwnProps) => nextFinalProps
  14. connect passes its args to connectAdvanced as options, which will in turn pass them to
  15. selectorFactory each time a Connect component instance is instantiated or hot reloaded.
  16. selectorFactory returns a final props selector from its mapStateToProps,
  17. mapStateToPropsFactories, mapDispatchToProps, mapDispatchToPropsFactories, mergeProps,
  18. mergePropsFactories, and pure args.
  19. The resulting final props selector is called by the Connect component instance whenever
  20. it receives new props or store state.
  21. */
  22. function match(arg, factories, name) {
  23. for (var i = factories.length - 1; i >= 0; i--) {
  24. var result = factories[i](arg);
  25. if (result) return result;
  26. }
  27. return function (dispatch, options) {
  28. throw new Error("Invalid value of type " + typeof arg + " for " + name + " argument when connecting component " + options.wrappedComponentName + ".");
  29. };
  30. }
  31. function strictEqual(a, b) {
  32. return a === b;
  33. } // createConnect with default args builds the 'official' connect behavior. Calling it with
  34. // different options opens up some testing and extensibility scenarios
  35. export function createConnect(_temp) {
  36. var _ref = _temp === void 0 ? {} : _temp,
  37. _ref$connectHOC = _ref.connectHOC,
  38. connectHOC = _ref$connectHOC === void 0 ? connectAdvanced : _ref$connectHOC,
  39. _ref$mapStateToPropsF = _ref.mapStateToPropsFactories,
  40. mapStateToPropsFactories = _ref$mapStateToPropsF === void 0 ? defaultMapStateToPropsFactories : _ref$mapStateToPropsF,
  41. _ref$mapDispatchToPro = _ref.mapDispatchToPropsFactories,
  42. mapDispatchToPropsFactories = _ref$mapDispatchToPro === void 0 ? defaultMapDispatchToPropsFactories : _ref$mapDispatchToPro,
  43. _ref$mergePropsFactor = _ref.mergePropsFactories,
  44. mergePropsFactories = _ref$mergePropsFactor === void 0 ? defaultMergePropsFactories : _ref$mergePropsFactor,
  45. _ref$selectorFactory = _ref.selectorFactory,
  46. selectorFactory = _ref$selectorFactory === void 0 ? defaultSelectorFactory : _ref$selectorFactory;
  47. return function connect(mapStateToProps, mapDispatchToProps, mergeProps, _ref2) {
  48. if (_ref2 === void 0) {
  49. _ref2 = {};
  50. }
  51. var _ref3 = _ref2,
  52. _ref3$pure = _ref3.pure,
  53. pure = _ref3$pure === void 0 ? true : _ref3$pure,
  54. _ref3$areStatesEqual = _ref3.areStatesEqual,
  55. areStatesEqual = _ref3$areStatesEqual === void 0 ? strictEqual : _ref3$areStatesEqual,
  56. _ref3$areOwnPropsEqua = _ref3.areOwnPropsEqual,
  57. areOwnPropsEqual = _ref3$areOwnPropsEqua === void 0 ? shallowEqual : _ref3$areOwnPropsEqua,
  58. _ref3$areStatePropsEq = _ref3.areStatePropsEqual,
  59. areStatePropsEqual = _ref3$areStatePropsEq === void 0 ? shallowEqual : _ref3$areStatePropsEq,
  60. _ref3$areMergedPropsE = _ref3.areMergedPropsEqual,
  61. areMergedPropsEqual = _ref3$areMergedPropsE === void 0 ? shallowEqual : _ref3$areMergedPropsE,
  62. extraOptions = _objectWithoutPropertiesLoose(_ref3, _excluded);
  63. var initMapStateToProps = match(mapStateToProps, mapStateToPropsFactories, 'mapStateToProps');
  64. var initMapDispatchToProps = match(mapDispatchToProps, mapDispatchToPropsFactories, 'mapDispatchToProps');
  65. var initMergeProps = match(mergeProps, mergePropsFactories, 'mergeProps');
  66. return connectHOC(selectorFactory, _extends({
  67. // used in error messages
  68. methodName: 'connect',
  69. // used to compute Connect's displayName from the wrapped component's displayName.
  70. getDisplayName: function getDisplayName(name) {
  71. return "Connect(" + name + ")";
  72. },
  73. // if mapStateToProps is falsy, the Connect component doesn't subscribe to store state changes
  74. shouldHandleStateChanges: Boolean(mapStateToProps),
  75. // passed through to selectorFactory
  76. initMapStateToProps: initMapStateToProps,
  77. initMapDispatchToProps: initMapDispatchToProps,
  78. initMergeProps: initMergeProps,
  79. pure: pure,
  80. areStatesEqual: areStatesEqual,
  81. areOwnPropsEqual: areOwnPropsEqual,
  82. areStatePropsEqual: areStatePropsEqual,
  83. areMergedPropsEqual: areMergedPropsEqual
  84. }, extraOptions));
  85. };
  86. }
  87. export default /*#__PURE__*/createConnect();