-
-
Save eisisig/f34a9b69c17d2b09ad115eb0fcd81c59 to your computer and use it in GitHub Desktop.
Dynamic reselect selector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function createDynamicSelector() { | |
let subSelectors = null; | |
let actualSelector = null; | |
return (state, props, ...args) => { | |
// Check to see if the sub state has change | |
const subState = state.get('subState'); | |
if (subSelectors === null || !Set(subState.keys()).equals(Set(subSelectors.keys()))) { | |
// Rebuild our the selectors - could obviously update if required. | |
subSelectors = subState.map((value, key) => { | |
return createSubSelector( | |
(state) => state.getIn(['subState', key]) | |
) | |
}); | |
actualSelector = createSelector( | |
...subSelectors.toArray(), | |
(...results) => { | |
// Merge / convert as required | |
return results; | |
} | |
) | |
} | |
return actualSelector(state, props, ...args); | |
}; | |
} | |
export function createSubSelector(subSelector) { | |
return createSelector( | |
subSelector, | |
// Other selectors can still be in here! | |
(subSubState) => { | |
// Do stuff | |
return subSubState | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment