Created
July 14, 2017 21:16
-
-
Save CGamesPlay/e3cb9d62e95be13a364100c707f46dbf to your computer and use it in GitHub Desktop.
createSelectorSelector
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
import { createSelector } from 'reselect'; | |
import _ from 'underscore'; | |
const createSelectorSelector = (...funcs) => { | |
const base = createSelector(...funcs); | |
const selector = (...args) => base(...args)(...args); | |
_.extendOwn(selector, base); | |
return selector; | |
}; | |
export default createSelectorSelector; |
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
import createSelectorSelector from "./createSelectorSelector"; | |
describe("createSelectorSelector", () => { | |
it("efficiently recomputes", () => { | |
var state = { choiceA: 'aaa', choiceB: 'bbb', choice: 'choiceA' }; | |
const getChoiceA = s => s.choiceA; | |
const getChoiceB = s => s.choiceB; | |
const getChoice = s => s.choice; | |
const selector = createSelectorSelector( | |
[ getChoice ], | |
choice => { | |
return choice == 'choiceA' ? getChoiceA : getChoiceB; | |
} | |
); | |
expect(selector(state)).toBe('aaa'); | |
expect(selector(state)).toBe('aaa'); | |
expect(selector.recomputations()).toBe(1); | |
state = { ...state, choice: 'choiceB' }; | |
expect(selector(state)).toBe('bbb'); | |
expect(selector(state)).toBe('bbb'); | |
expect(selector.recomputations()).toBe(2); | |
state = { ...state, choiceB: 'BBB' }; | |
expect(selector(state)).toBe('BBB'); | |
expect(selector.recomputations()).toBe(2); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment