Faster series selector

This commit is contained in:
ta264 2020-01-05 21:29:58 +00:00 committed by Mark McDowall
parent 283f905d79
commit 43d04cd54e
2 changed files with 15 additions and 4 deletions

View File

@ -16,6 +16,13 @@ const blacklistedProperties = [
'id' 'id'
]; ];
function createItemMap(data) {
return data.reduce((acc, d, index) => {
acc[d.id] = index;
return acc;
}, {});
}
export default function createHandleActions(handlers, defaultState, section) { export default function createHandleActions(handlers, defaultState, section) {
return handleActions({ return handleActions({
@ -42,6 +49,7 @@ export default function createHandleActions(handlers, defaultState, section) {
if (_.isArray(payload.data)) { if (_.isArray(payload.data)) {
newState.items = payload.data; newState.items = payload.data;
newState.itemMap = createItemMap(payload.data);
} else { } else {
newState.item = payload.data; newState.item = payload.data;
} }
@ -75,6 +83,7 @@ export default function createHandleActions(handlers, defaultState, section) {
newState.items.splice(index, 1, { ...item, ...otherProps }); newState.items.splice(index, 1, { ...item, ...otherProps });
} else if (!updateOnly) { } else if (!updateOnly) {
newState.items.push({ ...otherProps }); newState.items.push({ ...otherProps });
newState.itemMap = createItemMap(newState.items);
} }
return updateSectionState(state, payloadSection, newState); return updateSectionState(state, payloadSection, newState);
@ -111,6 +120,8 @@ export default function createHandleActions(handlers, defaultState, section) {
newState.items = [...newState.items]; newState.items = [...newState.items];
_.remove(newState.items, { id: payload.id }); _.remove(newState.items, { id: payload.id });
newState.itemMap = createItemMap(newState.items);
return updateSectionState(state, payloadSection, newState); return updateSectionState(state, payloadSection, newState);
} }

View File

@ -1,12 +1,12 @@
import { createSelector } from 'reselect'; import { createSelector } from 'reselect';
import createAllSeriesSelector from './createAllSeriesSelector';
function createSeriesSelector() { function createSeriesSelector() {
return createSelector( return createSelector(
(state, { seriesId }) => seriesId, (state, { seriesId }) => seriesId,
createAllSeriesSelector(), (state) => state.series.itemMap,
(seriesId, allSeries) => { (state) => state.series.items,
return allSeries.find((series) => series.id === seriesId); (seriesId, itemMap, allSeries) => {
return allSeries[itemMap[seriesId]];
} }
); );
} }