Skip to content

Commit

Permalink
Simplify pausableConnect to pause regardless of selector result
Browse files Browse the repository at this point in the history
Rather than using cachedResult.isActiveRoute (which allowed for more
flexibility, but was a potential footgun), we now just update when
isActiveRoute=true or transitions from true to false.
  • Loading branch information
chromakode committed Jul 21, 2017
1 parent 95ae2d2 commit c877cba
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions shared/util/pausable-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,27 @@ import typeof {connect} from 'react-redux'

function selectorFactory(dispatch, factoryOptions) {
const selector = defaultSelectorFactory(dispatch, factoryOptions)
let wasActiveRoute = false
let cachedResult
const pausableSelector = function(state, ownProps) {
// We run the selector the first time the HoC is mounted, even if not
// active. Some connected components have expectations of the structure of
// their props, so we can't render them until we have data from the
// selector.
//
// The selector can pause itself by returning isActiveRoute=false. It will
// not run and the cached result will be returned as long as
// ownProps.isActiveRoute=false.
// The selector will run when ownProps.isActiveRoute=true, or on
// transitions from true to false. Otherwise, the last cached result will
// be used and the connected component will not update.
if (
cachedResult === undefined ||
cachedResult.isActiveRoute ||
cachedResult.isActiveRoute !== ownProps.isActiveRoute
wasActiveRoute !== ownProps.isActiveRoute
) {
cachedResult = selector(state, ownProps)
}

if (__DEV__) {
if (cachedResult.isActiveRoute !== true && cachedResult.isActiveRoute !== false) {
console.warn('pausableConnect: selector did not return a value for isActiveRoute', cachedResult)
}
}
wasActiveRoute = ownProps.isActiveRoute

return cachedResult
}
// If what we return is === what we returned prior, the connected component
Expand Down

0 comments on commit c877cba

Please sign in to comment.