HEX
Server: nginx/1.28.1
System: Linux 10-41-63-61 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64
User: www (1001)
PHP: 7.4.33
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: //proc/2165/cwd/node_modules/.pnpm/node_modules/reselect/src/devModeChecks/identityFunctionCheck.ts
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 }
      )
    }
  }
}