This commit is contained in:
53
frontend/node_modules/reselect/src/devModeChecks/identityFunctionCheck.ts
generated
vendored
Normal file
53
frontend/node_modules/reselect/src/devModeChecks/identityFunctionCheck.ts
generated
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
import type { AnyFunction } from '../types'
|
||||
|
||||
/**
|
||||
* Runs a check to determine if the given result function behaves as an
|
||||
* identity function. An identity function is one that returns its
|
||||
* input unchanged, for example, `x => x`. This check helps ensure
|
||||
* efficient memoization and prevent unnecessary re-renders by encouraging
|
||||
* proper use of transformation logic in result functions and
|
||||
* extraction logic in input selectors.
|
||||
*
|
||||
* @param resultFunc - The result function to be checked.
|
||||
* @param inputSelectorsResults - The results of the input selectors.
|
||||
* @param outputSelectorResult - The result of the output selector.
|
||||
*
|
||||
* @see {@link https://reselect.js.org/api/development-only-stability-checks#identityfunctioncheck `identityFunctionCheck`}
|
||||
*
|
||||
* @since 5.0.0
|
||||
* @internal
|
||||
*/
|
||||
export const runIdentityFunctionCheck = (
|
||||
resultFunc: AnyFunction,
|
||||
inputSelectorsResults: unknown[],
|
||||
outputSelectorResult: unknown
|
||||
) => {
|
||||
if (
|
||||
inputSelectorsResults.length === 1 &&
|
||||
inputSelectorsResults[0] === outputSelectorResult
|
||||
) {
|
||||
let isInputSameAsOutput = false
|
||||
try {
|
||||
const emptyObject = {}
|
||||
if (resultFunc(emptyObject) === emptyObject) isInputSameAsOutput = true
|
||||
} catch {
|
||||
// Do nothing
|
||||
}
|
||||
if (isInputSameAsOutput) {
|
||||
let stack: string | undefined = undefined
|
||||
try {
|
||||
throw new Error()
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-extra-semi, no-extra-semi
|
||||
;({ stack } = e as Error)
|
||||
}
|
||||
console.warn(
|
||||
'The result function returned its own inputs without modification. e.g' +
|
||||
'\n`createSelector([state => state.todos], todos => todos)`' +
|
||||
'\nThis could lead to inefficient memoization and unnecessary re-renders.' +
|
||||
'\nEnsure transformation logic is in the result function, and extraction logic is in the input selectors.',
|
||||
{ stack }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
59
frontend/node_modules/reselect/src/devModeChecks/inputStabilityCheck.ts
generated
vendored
Normal file
59
frontend/node_modules/reselect/src/devModeChecks/inputStabilityCheck.ts
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
import type { CreateSelectorOptions, UnknownMemoizer } from '../types'
|
||||
|
||||
/**
|
||||
* Runs a stability check to ensure the input selector results remain stable
|
||||
* when provided with the same arguments. This function is designed to detect
|
||||
* changes in the output of input selectors, which can impact the performance of memoized selectors.
|
||||
*
|
||||
* @param inputSelectorResultsObject - An object containing two arrays: `inputSelectorResults` and `inputSelectorResultsCopy`, representing the results of input selectors.
|
||||
* @param options - Options object consisting of a `memoize` function and a `memoizeOptions` object.
|
||||
* @param inputSelectorArgs - List of arguments being passed to the input selectors.
|
||||
*
|
||||
* @see {@link https://reselect.js.org/api/development-only-stability-checks/#inputstabilitycheck `inputStabilityCheck`}
|
||||
*
|
||||
* @since 5.0.0
|
||||
* @internal
|
||||
*/
|
||||
export const runInputStabilityCheck = (
|
||||
inputSelectorResultsObject: {
|
||||
inputSelectorResults: unknown[]
|
||||
inputSelectorResultsCopy: unknown[]
|
||||
},
|
||||
options: Required<
|
||||
Pick<
|
||||
CreateSelectorOptions<UnknownMemoizer, UnknownMemoizer>,
|
||||
'memoize' | 'memoizeOptions'
|
||||
>
|
||||
>,
|
||||
inputSelectorArgs: unknown[] | IArguments
|
||||
) => {
|
||||
const { memoize, memoizeOptions } = options
|
||||
const { inputSelectorResults, inputSelectorResultsCopy } =
|
||||
inputSelectorResultsObject
|
||||
const createAnEmptyObject = memoize(() => ({}), ...memoizeOptions)
|
||||
// if the memoize method thinks the parameters are equal, these *should* be the same reference
|
||||
const areInputSelectorResultsEqual =
|
||||
createAnEmptyObject.apply(null, inputSelectorResults) ===
|
||||
createAnEmptyObject.apply(null, inputSelectorResultsCopy)
|
||||
if (!areInputSelectorResultsEqual) {
|
||||
let stack: string | undefined = undefined
|
||||
try {
|
||||
throw new Error()
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-extra-semi, no-extra-semi
|
||||
;({ stack } = e as Error)
|
||||
}
|
||||
console.warn(
|
||||
'An input selector returned a different result when passed same arguments.' +
|
||||
'\nThis means your output selector will likely run more frequently than intended.' +
|
||||
'\nAvoid returning a new reference inside your input selector, e.g.' +
|
||||
'\n`createSelector([state => state.todos.map(todo => todo.id)], todoIds => todoIds.length)`',
|
||||
{
|
||||
arguments: inputSelectorArgs,
|
||||
firstInputs: inputSelectorResults,
|
||||
secondInputs: inputSelectorResultsCopy,
|
||||
stack
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
63
frontend/node_modules/reselect/src/devModeChecks/setGlobalDevModeChecks.ts
generated
vendored
Normal file
63
frontend/node_modules/reselect/src/devModeChecks/setGlobalDevModeChecks.ts
generated
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
import type { DevModeChecks } from '../types'
|
||||
|
||||
/**
|
||||
* Global configuration for development mode checks. This specifies the default
|
||||
* frequency at which each development mode check should be performed.
|
||||
*
|
||||
* @since 5.0.0
|
||||
* @internal
|
||||
*/
|
||||
export const globalDevModeChecks: DevModeChecks = {
|
||||
inputStabilityCheck: 'once',
|
||||
identityFunctionCheck: 'once'
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides the development mode checks settings for all selectors.
|
||||
*
|
||||
* Reselect performs additional checks in development mode to help identify and
|
||||
* warn about potential issues in selector behavior. This function allows you to
|
||||
* customize the behavior of these checks across all selectors in your application.
|
||||
*
|
||||
* **Note**: This setting can still be overridden per selector inside `createSelector`'s `options` object.
|
||||
* See {@link https://github.com/reduxjs/reselect#2-per-selector-by-passing-an-identityfunctioncheck-option-directly-to-createselector per-selector-configuration}
|
||||
* and {@linkcode CreateSelectorOptions.identityFunctionCheck identityFunctionCheck} for more details.
|
||||
*
|
||||
* _The development mode checks do not run in production builds._
|
||||
*
|
||||
* @param devModeChecks - An object specifying the desired settings for development mode checks. You can provide partial overrides. Unspecified settings will retain their current values.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { setGlobalDevModeChecks } from 'reselect'
|
||||
* import { DevModeChecks } from '../types'
|
||||
*
|
||||
* // Run only the first time the selector is called. (default)
|
||||
* setGlobalDevModeChecks({ inputStabilityCheck: 'once' })
|
||||
*
|
||||
* // Run every time the selector is called.
|
||||
* setGlobalDevModeChecks({ inputStabilityCheck: 'always' })
|
||||
*
|
||||
* // Never run the input stability check.
|
||||
* setGlobalDevModeChecks({ inputStabilityCheck: 'never' })
|
||||
*
|
||||
* // Run only the first time the selector is called. (default)
|
||||
* setGlobalDevModeChecks({ identityFunctionCheck: 'once' })
|
||||
*
|
||||
* // Run every time the selector is called.
|
||||
* setGlobalDevModeChecks({ identityFunctionCheck: 'always' })
|
||||
*
|
||||
* // Never run the identity function check.
|
||||
* setGlobalDevModeChecks({ identityFunctionCheck: 'never' })
|
||||
* ```
|
||||
* @see {@link https://reselect.js.org/api/development-only-stability-checks Development-Only Stability Checks}
|
||||
* @see {@link https://reselect.js.org/api/development-only-stability-checks#1-globally-through-setglobaldevmodechecks global-configuration}
|
||||
*
|
||||
* @since 5.0.0
|
||||
* @public
|
||||
*/
|
||||
export const setGlobalDevModeChecks = (
|
||||
devModeChecks: Partial<DevModeChecks>
|
||||
) => {
|
||||
Object.assign(globalDevModeChecks, devModeChecks)
|
||||
}
|
||||
Reference in New Issue
Block a user