Commit 0193c0ad authored by Lyza Danger Gardner's avatar Lyza Danger Gardner

Memoize the `searchUris` selector

This is needed so that `searchUris` don’t change when non-relevant
frame state changes
parent b80433f1
import { createSelector } from 'reselect'; import {
createSelector,
createSelectorCreator,
defaultMemoize,
} from 'reselect';
import shallowEqual from 'shallowequal';
import * as util from '../util'; import * as util from '../util';
...@@ -103,15 +108,23 @@ function searchUrisForFrame(frame) { ...@@ -103,15 +108,23 @@ function searchUrisForFrame(frame) {
return uris; return uris;
} }
/** // "selector creator" that uses `shallowEqual` instead of `===` for memoization
* Return the set of URIs that should be used to search for annotations on the const createShallowEqualSelector = createSelectorCreator(
* current page. defaultMemoize,
*/ shallowEqual
function searchUris(state) { );
return state.frames.reduce(function (uris, frame) {
return uris.concat(searchUrisForFrame(frame)); // Memoized selector will return the same array (of URIs) reference unless the
}, []); // values of the array change (are not shallow-equal).
} const searchUris = createShallowEqualSelector(
state => {
return state.frames.reduce(
(uris, frame) => uris.concat(searchUrisForFrame(frame)),
[]
);
},
uris => uris
);
export default { export default {
init: init, init: init,
......
...@@ -157,7 +157,12 @@ describe('sidebar/store/modules/frames', function () { ...@@ -157,7 +157,12 @@ describe('sidebar/store/modules/frames', function () {
testCase.frames.forEach(frame => { testCase.frames.forEach(frame => {
store.connectFrame(frame); store.connectFrame(frame);
}); });
assert.deepEqual(store.searchUris(), testCase.searchUris); const firstResults = store.searchUris();
const secondResults = store.searchUris();
assert.deepEqual(firstResults, testCase.searchUris);
// The selector is memoized and should return the same Array reference
// assuming the list of search URIs hasn't changed
assert.equal(firstResults, secondResults);
}); });
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment