Skip to content

Instantly share code, notes, and snippets.

@CGamesPlay
Created July 14, 2017 21:16
Show Gist options
  • Save CGamesPlay/e3cb9d62e95be13a364100c707f46dbf to your computer and use it in GitHub Desktop.
Save CGamesPlay/e3cb9d62e95be13a364100c707f46dbf to your computer and use it in GitHub Desktop.
createSelectorSelector
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;
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