wrapMapToProps.js 3.3 KB

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