All files / src/connect wrapMapToProps.js

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