UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

37 lines (34 loc) 1.24 kB
import { lruMemoize, createSelectorCreator } from 'reselect'; const reselectCreateSelector = createSelectorCreator({ memoize: lruMemoize, memoizeOptions: { maxSize: 1, equalityCheck: Object.is } }); const cache = new WeakMap(); /** * Method wrapping reselect's createSelector to provide caching for chart instances. * */ export const createSelector = (...createSelectorArgs) => { const selector = (state, ...selectorArgs) => { const cacheKey = state.cacheKey; // If there is no cache for the current chart instance, create one. let cacheForCurrentChartInstance = cache.get(cacheKey); if (!cacheForCurrentChartInstance) { cacheForCurrentChartInstance = new Map(); cache.set(cacheKey, cacheForCurrentChartInstance); } // If there is a cached selector, execute it. const cachedSelector = cacheForCurrentChartInstance.get(createSelectorArgs); if (cachedSelector) { return cachedSelector(state, ...selectorArgs); } // Otherwise, create a new selector and cache it and execute it. const fn = reselectCreateSelector(...createSelectorArgs); cacheForCurrentChartInstance.set(createSelectorArgs, fn); return fn(state, ...selectorArgs); }; return selector; };