Skip to content

Commit

Permalink
Rename inputsAreEqual to areHookInputsEqual & move it to shared (#14036)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored and sophiebits committed Nov 2, 2018
1 parent c898020 commit ae196e8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 54 deletions.
22 changes: 2 additions & 20 deletions packages/react-dom/src/server/ReactPartialRendererHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -236,7 +237,7 @@ function useMemo<T>(
) {
const prevState = workInProgressHook.memoizedState;
const prevInputs = prevState[1];
if (inputsAreEqual(nextInputs, prevInputs)) {
if (areHookInputsEqual(nextInputs, prevInputs)) {
return prevState[0];
}
}
Expand Down Expand Up @@ -331,25 +332,6 @@ function dispatchAction<A>(
}
}

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 = {
Expand Down
38 changes: 4 additions & 34 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
} from './ReactFiberScheduler';

import invariant from 'shared/invariant';
import warning from 'shared/warning';
import areHookInputsEqual from 'shared/areHookInputsEqual';

type Update<A> = {
expirationTime: ExpirationTime,
Expand Down Expand Up @@ -545,7 +545,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;
}
Expand Down Expand Up @@ -609,7 +609,7 @@ export function useCallback<T>(
const prevState = workInProgressHook.memoizedState;
if (prevState !== null) {
const prevInputs = prevState[1];
if (inputsAreEqual(nextInputs, prevInputs)) {
if (areHookInputsEqual(nextInputs, prevInputs)) {
return prevState[0];
}
}
Expand All @@ -630,7 +630,7 @@ export function useMemo<T>(
const prevState = workInProgressHook.memoizedState;
if (prevState !== null) {
const prevInputs = prevState[1];
if (inputsAreEqual(nextInputs, prevInputs)) {
if (areHookInputsEqual(nextInputs, prevInputs)) {
return prevState[0];
}
}
Expand Down Expand Up @@ -701,33 +701,3 @@ function dispatchAction<A>(fiber: Fiber, queue: UpdateQueue<A>, 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;
}
40 changes: 40 additions & 0 deletions packages/shared/areHookInputsEqual.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit ae196e8

Please sign in to comment.