From 43d04cd54efcf5569cb316a5856878d4291f96be Mon Sep 17 00:00:00 2001 From: ta264 Date: Sun, 5 Jan 2020 21:29:58 +0000 Subject: [PATCH] Faster series selector --- .../src/Store/Actions/Creators/createHandleActions.js | 11 +++++++++++ frontend/src/Store/Selectors/createSeriesSelector.js | 8 ++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/frontend/src/Store/Actions/Creators/createHandleActions.js b/frontend/src/Store/Actions/Creators/createHandleActions.js index c3315ce94..d40678fc5 100644 --- a/frontend/src/Store/Actions/Creators/createHandleActions.js +++ b/frontend/src/Store/Actions/Creators/createHandleActions.js @@ -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); } diff --git a/frontend/src/Store/Selectors/createSeriesSelector.js b/frontend/src/Store/Selectors/createSeriesSelector.js index fc5057b95..c5e280192 100644 --- a/frontend/src/Store/Selectors/createSeriesSelector.js +++ b/frontend/src/Store/Selectors/createSeriesSelector.js @@ -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]]; } ); }