All files / src/connect selectorFactory.js

98.11% Statements 52/53
94.44% Branches 17/18
100% Functions 9/9
98% Lines 49/50
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 1041x   91x               39x 39x 39x 39x 39x 39x 39x 39x 39x     39x 39x 39x 39x 39x 39x 39x       3x   3x     3x 3x       20x 7x   20x 4x   20x 20x       26x 26x 26x   26x 20x   26x       91x 91x 91x 91x   91x 88x 68x 42x     39x 130x                               117x 39x 39x 39x   39x              
import shallowEqual from './utils/shallowEqual'
 
function strictEqual(a, b) { return a === b }
 
export function pureFinalPropsSelectorFactory(
  mapStateToProps,
  mapSendToProps,
  mergeProps,
  send
) {
  const areStatesEqual = strictEqual;
  const areOwnPropsEqual = shallowEqual;
  const areStatePropsEqual = shallowEqual;
  let hasRunAtLeastOnce = false
  let state
  let ownProps
  let stateProps
  let sendProps
  let mergedProps
 
  function handleFirstCall(firstState, firstOwnProps) {
    state = firstState
    ownProps = firstOwnProps
    stateProps = mapStateToProps(state, ownProps)
    sendProps = mapSendToProps(send, ownProps)
    mergedProps = mergeProps(stateProps, sendProps, ownProps)
    hasRunAtLeastOnce = true
    return mergedProps
  }
 
  function handleNewPropsAndNewState() {
    stateProps = mapStateToProps(state, ownProps)
 
    Iif (mapSendToProps.dependsOnOwnProps)
      sendProps = mapSendToProps(send, ownProps)
 
    mergedProps = mergeProps(stateProps, sendProps, ownProps)
    return mergedProps
  }
 
  function handleNewProps() {
    if (mapStateToProps.dependsOnOwnProps)
      stateProps = mapStateToProps(state, ownProps)
 
    if (mapSendToProps.dependsOnOwnProps)
      sendProps = mapSendToProps(send, ownProps)
 
    mergedProps = mergeProps(stateProps, sendProps, ownProps)
    return mergedProps
  }
 
  function handleNewState() {
    const nextStateProps = mapStateToProps(state, ownProps)
    const statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps)
    stateProps = nextStateProps
    
    if (statePropsChanged)
      mergedProps = mergeProps(stateProps, sendProps, ownProps)
 
    return mergedProps
  }
 
  function handleSubsequentCalls(nextState, nextOwnProps) {
    const propsChanged = !areOwnPropsEqual(nextOwnProps, ownProps)
    const stateChanged = !areStatesEqual(nextState, state)
    state = nextState
    ownProps = nextOwnProps
 
    if (propsChanged && stateChanged) return handleNewPropsAndNewState()
    if (propsChanged) return handleNewProps()
    if (stateChanged) return handleNewState()
    return mergedProps
  }
 
  return function pureFinalPropsSelector(nextState, nextOwnProps) {
    return hasRunAtLeastOnce
      ? handleSubsequentCalls(nextState, nextOwnProps)
      : handleFirstCall(nextState, nextOwnProps)
  }
}
 
 
// If pure is true, the selector returned by selectorFactory will memoize its results,
// allowing connect's shouldComponentUpdate to return false if final
// props have not changed. If false, the selector will always return a new
// object and shouldComponentUpdate will always return true.
 
export default function finalPropsSelectorFactory(send, {
  initMapStateToProps,
  initMapSendToProps,
  initMergeProps,
}) {
  const mapStateToProps = initMapStateToProps(send);
  const mapSendToProps = initMapSendToProps(send);
  const mergeProps = initMergeProps(send);
 
  return pureFinalPropsSelectorFactory(
    mapStateToProps,
    mapSendToProps,
    mergeProps,
    send
  )
}