wrapMapToProps.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
  3. exports.__esModule = true;
  4. exports.getDependsOnOwnProps = getDependsOnOwnProps;
  5. exports.wrapMapToPropsConstant = wrapMapToPropsConstant;
  6. exports.wrapMapToPropsFunc = wrapMapToPropsFunc;
  7. var _verifyPlainObject = _interopRequireDefault(require("../utils/verifyPlainObject"));
  8. function wrapMapToPropsConstant(getConstant) {
  9. return function initConstantSelector(dispatch, options) {
  10. var constant = getConstant(dispatch, options);
  11. function constantSelector() {
  12. return constant;
  13. }
  14. constantSelector.dependsOnOwnProps = false;
  15. return constantSelector;
  16. };
  17. } // dependsOnOwnProps is used by createMapToPropsProxy to determine whether to pass props as args
  18. // to the mapToProps function being wrapped. It is also used by makePurePropsSelector to determine
  19. // whether mapToProps needs to be invoked when props have changed.
  20. //
  21. // A length of one signals that mapToProps does not depend on props from the parent component.
  22. // A length of zero is assumed to mean mapToProps is getting args via arguments or ...args and
  23. // therefore not reporting its length accurately..
  24. function getDependsOnOwnProps(mapToProps) {
  25. return mapToProps.dependsOnOwnProps !== null && mapToProps.dependsOnOwnProps !== undefined ? Boolean(mapToProps.dependsOnOwnProps) : mapToProps.length !== 1;
  26. } // Used by whenMapStateToPropsIsFunction and whenMapDispatchToPropsIsFunction,
  27. // this function wraps mapToProps in a proxy function which does several things:
  28. //
  29. // * Detects whether the mapToProps function being called depends on props, which
  30. // is used by selectorFactory to decide if it should reinvoke on props changes.
  31. //
  32. // * On first call, handles mapToProps if returns another function, and treats that
  33. // new function as the true mapToProps for subsequent calls.
  34. //
  35. // * On first call, verifies the first result is a plain object, in order to warn
  36. // the developer that their mapToProps function is not returning a valid result.
  37. //
  38. function wrapMapToPropsFunc(mapToProps, methodName) {
  39. return function initProxySelector(dispatch, _ref) {
  40. var displayName = _ref.displayName;
  41. var proxy = function mapToPropsProxy(stateOrDispatch, ownProps) {
  42. return proxy.dependsOnOwnProps ? proxy.mapToProps(stateOrDispatch, ownProps) : proxy.mapToProps(stateOrDispatch);
  43. }; // allow detectFactoryAndVerify to get ownProps
  44. proxy.dependsOnOwnProps = true;
  45. proxy.mapToProps = function detectFactoryAndVerify(stateOrDispatch, ownProps) {
  46. proxy.mapToProps = mapToProps;
  47. proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps);
  48. var props = proxy(stateOrDispatch, ownProps);
  49. if (typeof props === 'function') {
  50. proxy.mapToProps = props;
  51. proxy.dependsOnOwnProps = getDependsOnOwnProps(props);
  52. props = proxy(stateOrDispatch, ownProps);
  53. }
  54. if (process.env.NODE_ENV !== 'production') (0, _verifyPlainObject["default"])(props, displayName, methodName);
  55. return props;
  56. };
  57. return proxy;
  58. };
  59. }