Created
July 14, 2017 00:10
-
-
Save joedski/a7ad9ad444abb4af92c19323a4d76e5c to your computer and use it in GitHub Desktop.
Multiparameter Memoization: Example
This file contains hidden or 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
const reselect = require('reselect'); | |
const createSelectorCreator = passSelectorCountToMemoizer(reselect.createSelectorCreator); | |
const createSelector = createSelectorCreator(createTieredMemoizer({ memoizers: [multiMemo] })); | |
const getFooBy = (state, id) => state.foos.byId[id]; | |
const getBar = state => state.bar; | |
const getBaz = state => state.baz; | |
const getStuff = createSelector( | |
getFooBy, getBar, getBaz, | |
// something something createStructuredSelector. | |
(foo, bar, baz) => ({ foo, bar, baz }) | |
); | |
const state0 = { | |
foos: { | |
byId: { | |
'1': { name: 'Foo' }, | |
'2': { name: 'Fooby' }, | |
'3': { name: 'Fooborp' }, | |
}, | |
}, | |
bar: 'Bar!', | |
baz: 'Baz!', | |
}; | |
const state1 = { | |
...state0, | |
foos: { | |
byId: { | |
...state0.foos.byId, | |
'2': { name: 'Foobifoob' }, | |
}, | |
}, | |
}; | |
const a00 = getStuff(state0, '1'); | |
const a01 = getStuff(state0, '1'); | |
const a10 = getStuff(state1, '1'); | |
const a02 = getStuff(state0, '2'); | |
const a11 = getStuff(state1, '1'); | |
console.log(`getStuff(id = '1'):`, JSON.stringify(a00)); | |
console.log(` Does it return the same value for the same state?`, a00 === a01); | |
console.log(` Does it return the same value when something else in the state is different?`, a01 === a10); | |
console.log(` Does it return the same value between other selections?`, a10 === a11); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment