UNPKG

@mui/x-internals

Version:

Utility functions for the MUI X packages (internal use only).

223 lines (217 loc) 8.51 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSelectorMemoizedWithOptions = exports.createSelectorMemoized = exports.createSelector = void 0; var _index = _interopRequireDefault(require("../formatErrorMessage/index.js")); var _reselect = require("reselect"); /* eslint-disable no-underscore-dangle */ // __cacheKey__ const reselectCreateSelector = (0, _reselect.createSelectorCreator)({ memoize: _reselect.lruMemoize, memoizeOptions: { maxSize: 1, equalityCheck: Object.is } }); /** * Creates a selector function that can be used to derive values from the store's state. * * The combiner function can have up to three additional parameters, but it **cannot have optional or default parameters**. * * This function accepts up to six functions and combines them into a single selector function. * The resulting selector will take the state from the combined selectors and any additional parameters required by the combiner. * * The return type of the resulting selector is determined by the return type of the combiner function. * * @example * const selector = createSelector( * (state) => state.disabled * ); * * @example * const selector = createSelector( * (state) => state.disabled, * (state) => state.open, * (disabled, open) => ({ disabled, open }) * ); */ /* eslint-disable id-denylist */ const createSelector = (a, b, c, d, e, f, g, h, ...other) => { if (other.length > 0) { throw new Error(process.env.NODE_ENV !== "production" ? 'MUI X: Unsupported number of selectors. ' + 'The createSelector function supports up to 8 input selectors. ' + 'Consider combining selectors or restructuring your selector logic.' : (0, _index.default)(179)); } let selector; if (a && b && c && d && e && f && g && h) { selector = (state, a1, a2, a3) => { const va = a(state, a1, a2, a3); const vb = b(state, a1, a2, a3); const vc = c(state, a1, a2, a3); const vd = d(state, a1, a2, a3); const ve = e(state, a1, a2, a3); const vf = f(state, a1, a2, a3); const vg = g(state, a1, a2, a3); return h(va, vb, vc, vd, ve, vf, vg, a1, a2, a3); }; } else if (a && b && c && d && e && f && g) { selector = (state, a1, a2, a3) => { const va = a(state, a1, a2, a3); const vb = b(state, a1, a2, a3); const vc = c(state, a1, a2, a3); const vd = d(state, a1, a2, a3); const ve = e(state, a1, a2, a3); const vf = f(state, a1, a2, a3); return g(va, vb, vc, vd, ve, vf, a1, a2, a3); }; } else if (a && b && c && d && e && f) { selector = (state, a1, a2, a3) => { const va = a(state, a1, a2, a3); const vb = b(state, a1, a2, a3); const vc = c(state, a1, a2, a3); const vd = d(state, a1, a2, a3); const ve = e(state, a1, a2, a3); return f(va, vb, vc, vd, ve, a1, a2, a3); }; } else if (a && b && c && d && e) { selector = (state, a1, a2, a3) => { const va = a(state, a1, a2, a3); const vb = b(state, a1, a2, a3); const vc = c(state, a1, a2, a3); const vd = d(state, a1, a2, a3); return e(va, vb, vc, vd, a1, a2, a3); }; } else if (a && b && c && d) { selector = (state, a1, a2, a3) => { const va = a(state, a1, a2, a3); const vb = b(state, a1, a2, a3); const vc = c(state, a1, a2, a3); return d(va, vb, vc, a1, a2, a3); }; } else if (a && b && c) { selector = (state, a1, a2, a3) => { const va = a(state, a1, a2, a3); const vb = b(state, a1, a2, a3); return c(va, vb, a1, a2, a3); }; } else if (a && b) { selector = (state, a1, a2, a3) => { const va = a(state, a1, a2, a3); return b(va, a1, a2, a3); }; } else if (a) { selector = a; } else { throw new Error(process.env.NODE_ENV !== "production" ? 'MUI X: Missing arguments for createSelector. ' + 'At least one selector function is required. ' + 'Provide one or more selector functions as arguments.' : (0, _index.default)(180)); } return selector; }; /* eslint-enable id-denylist */ exports.createSelector = createSelector; const createSelectorMemoizedWithOptions = options => (...inputs) => { const cache = new WeakMap(); let nextCacheId = 1; const combiner = inputs[inputs.length - 1]; const nSelectors = inputs.length - 1 || 1; // (s1, s2, ..., sN, a1, a2, a3) => { ... } const argsLength = Math.max(combiner.length - nSelectors, 0); if (argsLength > 3) { throw new Error(process.env.NODE_ENV !== "production" ? 'MUI X: Unsupported number of arguments for selector combiner. ' + 'The combiner function supports up to 3 additional arguments beyond the selector outputs. ' + 'Consider restructuring your selector to use fewer arguments.' : (0, _index.default)(181)); } // prettier-ignore const selector = (state, a1, a2, a3) => { let cacheKey = state.__cacheKey__; if (!cacheKey) { cacheKey = { id: nextCacheId }; state.__cacheKey__ = cacheKey; nextCacheId += 1; } let fn = cache.get(cacheKey); if (!fn) { const selectors = inputs.length === 1 ? [x => x, combiner] : inputs; let reselectArgs = inputs; const selectorArgs = [undefined, undefined, undefined]; switch (argsLength) { case 0: break; case 1: { reselectArgs = [...selectors.slice(0, -1), () => selectorArgs[0], combiner]; break; } case 2: { reselectArgs = [...selectors.slice(0, -1), () => selectorArgs[0], () => selectorArgs[1], combiner]; break; } case 3: { reselectArgs = [...selectors.slice(0, -1), () => selectorArgs[0], () => selectorArgs[1], () => selectorArgs[2], combiner]; break; } default: throw new Error(process.env.NODE_ENV !== "production" ? 'MUI X: Unsupported number of arguments for selector. ' + 'The memoized selector supports up to 3 additional arguments. ' + 'Consider restructuring your selector to use fewer arguments.' : (0, _index.default)(182)); } if (options) { reselectArgs = [...reselectArgs, options]; } fn = reselectCreateSelector(...reselectArgs); fn.selectorArgs = selectorArgs; cache.set(cacheKey, fn); } /* eslint-disable no-fallthrough */ switch (argsLength) { case 3: fn.selectorArgs[2] = a3; case 2: fn.selectorArgs[1] = a2; case 1: fn.selectorArgs[0] = a1; case 0: default: } switch (argsLength) { case 0: return fn(state); case 1: return fn(state, a1); case 2: return fn(state, a1, a2); case 3: return fn(state, a1, a2, a3); default: throw /* minify-error-disabled */new Error('unreachable'); } }; return selector; }; /** * Creates a memoized selector function that can be used to derive values from the store's state. * This is useful for selectors that produce non-primitive values, such as objects or arrays, where memoization can help prevent unnecessary re-renders in React components. * * The memoization is implemented in a way that only the most recent selector result is cached. * This is suitable for cases where the selector is called with the same state and arguments repeatedly, * but may not be ideal for selectors that are called with a wide variety of states and arguments. * * The combiner function can have up to three additional parameters, but it **cannot have optional or default parameters**. * * This function accepts up to six functions and combines them into a single selector function. * The resulting selector will take the state from the combined selectors and any additional parameters required by the combiner. * * The return type of the resulting selector is determined by the return type of the combiner function. * * @example * const selector = createSelectorMemoized( * (state) => state.disabled * ); * * @example * const selector = createSelectorMemoized( * (state) => state.disabled, * (state) => state.open, * (disabled, open) => ({ disabled, open }) * ); */ exports.createSelectorMemoizedWithOptions = createSelectorMemoizedWithOptions; const createSelectorMemoized = exports.createSelectorMemoized = createSelectorMemoizedWithOptions();