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

View File

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