From cd635dcecae9e51ecd1d2a2e867971829ab8056f Mon Sep 17 00:00:00 2001 From: Chris Beer <cabeer@stanford.edu> Date: Mon, 15 Apr 2019 13:44:25 -0700 Subject: [PATCH] Add special memoizing logic to getLocales to keep providing the same array each call --- src/state/selectors/manifests.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/state/selectors/manifests.js b/src/state/selectors/manifests.js index 28a96ade9..095cd40fe 100644 --- a/src/state/selectors/manifests.js +++ b/src/state/selectors/manifests.js @@ -1,5 +1,6 @@ -import { createSelector } from 'reselect'; +import { createSelector, createSelectorCreator } from 'reselect'; import manifesto, { LanguageMap } from 'manifesto.js'; +import memoize from 'lodash/memoize'; import ManifestoCanvas from '../../lib/ManifestoCanvas'; /** Get the relevant manifest information */ @@ -346,7 +347,15 @@ function getLocales(resource) { return Object.keys(languages); } -export const getMetadataLocales = createSelector( +/** */ +const manifestHashFn = manifest => manifest && manifest.id; +// this has the potential to be an annoying memory leak, because the memoize store will grow +// unbounded. +const manifestMemoizingSelectorCreator = createSelectorCreator(memoize, manifestHashFn); + +// this is specially memoized because getLocales is creating new array instances +// every time it is called. +export const getMetadataLocales = manifestMemoizingSelectorCreator( [getManifestoInstance], manifest => getLocales(manifest), ); -- GitLab