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';
......@@ -103,15 +108,23 @@ function searchUrisForFrame(frame) {
return uris;
}
/**
* Return the set of URIs that should be used to search for annotations on the
* current page.
*/
function searchUris(state) {
return state.frames.reduce(function (uris, frame) {
return uris.concat(searchUrisForFrame(frame));
}, []);
}
// "selector creator" that uses `shallowEqual` instead of `===` for memoization
const createShallowEqualSelector = createSelectorCreator(
defaultMemoize,
shallowEqual
);
// 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 {
init: init,
......
......@@ -157,7 +157,12 @@ describe('sidebar/store/modules/frames', function () {
testCase.frames.forEach(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