wrapMapToProps.js 2.7 KB

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