Unverified Commit a63ef4a6 authored by Hannah Stepanek's avatar Hannah Stepanek Committed by GitHub

Merge pull request #1070 from hypothesis/faster-group-selectors

Make group selectors more efficient
parents d0ab6158 ea09632a
...@@ -101,6 +101,7 @@ ...@@ -101,6 +101,7 @@
"redux": "^4.0.1", "redux": "^4.0.1",
"redux-thunk": "^2.1.0", "redux-thunk": "^2.1.0",
"request": "^2.76.0", "request": "^2.76.0",
"reselect": "^4.0.0",
"retry": "^0.12.0", "retry": "^0.12.0",
"scroll-into-view": "^1.8.2", "scroll-into-view": "^1.8.2",
"seamless-immutable": "^7.1.4", "seamless-immutable": "^7.1.4",
......
'use strict'; 'use strict';
const { createSelector } = require('reselect');
const util = require('../util'); const util = require('../util');
const memoize = require('../../util/memoize');
const { selectors: sessionSelectors } = require('./session'); const { selectors: sessionSelectors } = require('./session');
const { isLoggedIn } = sessionSelectors; const { isLoggedIn } = sessionSelectors;
...@@ -126,37 +127,44 @@ function getGroup(state, id) { ...@@ -126,37 +127,44 @@ function getGroup(state, id) {
* *
* @return {Group[]} * @return {Group[]}
*/ */
const getMyGroups = memoize(state => { const getMyGroups = createSelector(
// If logged out, the Public group still has isMember set to true so only state => state.groups,
// return groups with membership in logged in state. isLoggedIn,
if (isLoggedIn(state)) { (groups, loggedIn) => {
return state.groups.filter(g => g.isMember); // If logged out, the Public group still has isMember set to true so only
// return groups with membership in logged in state.
if (loggedIn) {
return groups.filter(g => g.isMember);
}
return [];
} }
return []; );
});
/** /**
* Return groups the user isn't a member of that are scoped to the URI. * Return groups the user isn't a member of that are scoped to the URI.
* *
* @return {Group[]} * @return {Group[]}
*/ */
const getFeaturedGroups = memoize(state => { const getFeaturedGroups = createSelector(
return state.groups.filter(group => !group.isMember && group.isScopedToUri); state => state.groups,
}); groups => groups.filter(group => !group.isMember && group.isScopedToUri)
);
/** /**
* Return groups that don't show up in Featured and My Groups. * Return groups that don't show up in Featured and My Groups.
* *
* @return {Group[]} * @return {Group[]}
*/ */
const getCurrentlyViewingGroups = memoize(state => { const getCurrentlyViewingGroups = createSelector(
const myGroups = getMyGroups(state); allGroups,
const featuredGroups = getFeaturedGroups(state); getMyGroups,
getFeaturedGroups,
return state.groups.filter( (allGroups, myGroups, featuredGroups) => {
g => !myGroups.includes(g) && !featuredGroups.includes(g) return allGroups.filter(
); g => !myGroups.includes(g) && !featuredGroups.includes(g)
}); );
}
);
/** /**
* Return groups that are scoped to the uri. This is used to return the groups * Return groups that are scoped to the uri. This is used to return the groups
...@@ -165,9 +173,10 @@ const getCurrentlyViewingGroups = memoize(state => { ...@@ -165,9 +173,10 @@ const getCurrentlyViewingGroups = memoize(state => {
* *
* @return {Group[]} * @return {Group[]}
*/ */
const getInScopeGroups = memoize(state => { const getInScopeGroups = createSelector(
return state.groups.filter(g => g.isScopedToUri); state => state.groups,
}); groups => groups.filter(g => g.isScopedToUri)
);
module.exports = { module.exports = {
init, init,
......
...@@ -8431,6 +8431,11 @@ requires-port@^1.0.0: ...@@ -8431,6 +8431,11 @@ requires-port@^1.0.0:
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff" resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
integrity sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8= integrity sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=
reselect@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/reselect/-/reselect-4.0.0.tgz#f2529830e5d3d0e021408b246a206ef4ea4437f7"
integrity sha512-qUgANli03jjAyGlnbYVAV5vvnOmJnODyABz51RdBN7M4WaVu8mecZWgyQNkG8Yqe3KRGRt0l4K4B3XVEULC4CA==
resolve-dir@^1.0.0, resolve-dir@^1.0.1: resolve-dir@^1.0.0, resolve-dir@^1.0.1:
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/resolve-dir/-/resolve-dir-1.0.1.tgz#79a40644c362be82f26effe739c9bb5382046f43" resolved "https://registry.yarnpkg.com/resolve-dir/-/resolve-dir-1.0.1.tgz#79a40644c362be82f26effe739c9bb5382046f43"
......
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