connect.js 5.3 KB

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