From cc18195cb337f8fe9439ade6050924b17d844719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Tue, 30 Oct 2018 21:10:02 +0100 Subject: [PATCH] Rename inputsAreEqual to areHookInputsEqual & move it to shared --- .../src/server/ReactPartialRendererHooks.js | 22 +--------- .../react-reconciler/src/ReactFiberHooks.js | 38 ++---------------- packages/shared/areHookInputsEqual.js | 40 +++++++++++++++++++ 3 files changed, 46 insertions(+), 54 deletions(-) create mode 100644 packages/shared/areHookInputsEqual.js diff --git a/packages/react-dom/src/server/ReactPartialRendererHooks.js b/packages/react-dom/src/server/ReactPartialRendererHooks.js index 7c8f1784d8e8..1904249de312 100644 --- a/packages/react-dom/src/server/ReactPartialRendererHooks.js +++ b/packages/react-dom/src/server/ReactPartialRendererHooks.js @@ -7,6 +7,7 @@ * @flow */ import type {ReactContext} from 'shared/ReactTypes'; +import areHookInputsEqual from 'shared/areHookInputsEqual'; import invariant from 'shared/invariant'; import warning from 'shared/warning'; @@ -236,7 +237,7 @@ function useMemo( ) { const prevState = workInProgressHook.memoizedState; const prevInputs = prevState[1]; - if (inputsAreEqual(nextInputs, prevInputs)) { + if (areHookInputsEqual(nextInputs, prevInputs)) { return prevState[0]; } } @@ -331,25 +332,6 @@ function dispatchAction( } } -function inputsAreEqual(arr1, arr2) { - // Don't bother comparing lengths because these arrays are always - // passed inline. - for (let i = 0; i < arr1.length; i++) { - // Inlined Object.is polyfill. - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is - const val1 = arr1[i]; - const val2 = arr2[i]; - if ( - (val1 === val2 && (val1 !== 0 || 1 / val1 === 1 / (val2: any))) || - (val1 !== val1 && val2 !== val2) // eslint-disable-line no-self-compare - ) { - continue; - } - return false; - } - return true; -} - function noop(): void {} export const Dispatcher = { diff --git a/packages/react-reconciler/src/ReactFiberHooks.js b/packages/react-reconciler/src/ReactFiberHooks.js index d2542a1b92d3..7e605f1b87e2 100644 --- a/packages/react-reconciler/src/ReactFiberHooks.js +++ b/packages/react-reconciler/src/ReactFiberHooks.js @@ -36,7 +36,7 @@ import { } from './ReactFiberScheduler'; import invariant from 'shared/invariant'; -import warning from 'shared/warning'; +import areHookInputsEqual from 'shared/areHookInputsEqual'; type Update = { expirationTime: ExpirationTime, @@ -548,7 +548,7 @@ function useEffectImpl(fiberEffectTag, hookEffectTag, create, inputs): void { if (currentHook !== null) { const prevEffect = currentHook.memoizedState; destroy = prevEffect.destroy; - if (inputsAreEqual(nextInputs, prevEffect.inputs)) { + if (areHookInputsEqual(nextInputs, prevEffect.inputs)) { pushEffect(NoHookEffect, create, destroy, nextInputs); return; } @@ -612,7 +612,7 @@ export function useCallback( const prevState = workInProgressHook.memoizedState; if (prevState !== null) { const prevInputs = prevState[1]; - if (inputsAreEqual(nextInputs, prevInputs)) { + if (areHookInputsEqual(nextInputs, prevInputs)) { return prevState[0]; } } @@ -633,7 +633,7 @@ export function useMemo( const prevState = workInProgressHook.memoizedState; if (prevState !== null) { const prevInputs = prevState[1]; - if (inputsAreEqual(nextInputs, prevInputs)) { + if (areHookInputsEqual(nextInputs, prevInputs)) { return prevState[0]; } } @@ -704,33 +704,3 @@ function dispatchAction(fiber: Fiber, queue: UpdateQueue, action: A) { scheduleWork(fiber, expirationTime); } } - -function inputsAreEqual(arr1, arr2) { - // Don't bother comparing lengths in prod because these arrays should be - // passed inline. - if (__DEV__) { - warning( - arr1.length === arr2.length, - 'Detected a variable number of hook dependencies. The length of the ' + - 'dependencies array should be constant between renders.\n\n' + - 'Previous: %s\n' + - 'Incoming: %s', - arr1.join(', '), - arr2.join(', '), - ); - } - for (let i = 0; i < arr1.length; i++) { - // Inlined Object.is polyfill. - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is - const val1 = arr1[i]; - const val2 = arr2[i]; - if ( - (val1 === val2 && (val1 !== 0 || 1 / val1 === 1 / (val2: any))) || - (val1 !== val1 && val2 !== val2) // eslint-disable-line no-self-compare - ) { - continue; - } - return false; - } - return true; -} diff --git a/packages/shared/areHookInputsEqual.js b/packages/shared/areHookInputsEqual.js new file mode 100644 index 000000000000..722edaf43e7a --- /dev/null +++ b/packages/shared/areHookInputsEqual.js @@ -0,0 +1,40 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ + +import warning from 'shared/warning'; + +export default function areHookInputsEqual(arr1: any[], arr2: any[]) { + // Don't bother comparing lengths in prod because these arrays should be + // passed inline. + if (__DEV__) { + warning( + arr1.length === arr2.length, + 'Detected a variable number of hook dependencies. The length of the ' + + 'dependencies array should be constant between renders.\n\n' + + 'Previous: %s\n' + + 'Incoming: %s', + arr1.join(', '), + arr2.join(', '), + ); + } + for (let i = 0; i < arr1.length; i++) { + // Inlined Object.is polyfill. + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is + const val1 = arr1[i]; + const val2 = arr2[i]; + if ( + (val1 === val2 && (val1 !== 0 || 1 / val1 === 1 / (val2: any))) || + (val1 !== val1 && val2 !== val2) // eslint-disable-line no-self-compare + ) { + continue; + } + return false; + } + return true; +}