connect.js 5.3 KB

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