connect.js 4.8 KB

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