From bb3fb19e96d942200db48cb9307cf8ddf4cbb835 Mon Sep 17 00:00:00 2001 From: Maksim Markelov Date: Tue, 8 Jan 2019 17:21:12 +0300 Subject: [PATCH] Separate Object.is polyfill (#14334) * Separate_Object_Is_Polyfill --- .../src/ReactFiberNewContext.js | 10 ++-------- packages/shared/areHookInputsEqual.js | 10 ++-------- packages/shared/objectIs.js | 20 +++++++++++++++++++ packages/shared/shallowEqual.js | 19 +----------------- 4 files changed, 25 insertions(+), 34 deletions(-) create mode 100644 packages/shared/objectIs.js diff --git a/packages/react-reconciler/src/ReactFiberNewContext.js b/packages/react-reconciler/src/ReactFiberNewContext.js index 8af6bd09e0d2d..23439ff706bcf 100644 --- a/packages/react-reconciler/src/ReactFiberNewContext.js +++ b/packages/react-reconciler/src/ReactFiberNewContext.js @@ -26,6 +26,7 @@ import {ContextProvider, ClassComponent} from 'shared/ReactWorkTags'; import invariant from 'shared/invariant'; import warning from 'shared/warning'; +import is from 'shared/objectIs'; import { createUpdate, enqueueUpdate, @@ -104,14 +105,7 @@ export function calculateChangedBits( newValue: T, oldValue: T, ) { - // Use Object.is to compare the new context value to the old value. Inlined - // Object.is polyfill. - // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is - if ( - (oldValue === newValue && - (oldValue !== 0 || 1 / oldValue === 1 / (newValue: any))) || - (oldValue !== oldValue && newValue !== newValue) // eslint-disable-line no-self-compare - ) { + if (is(oldValue, newValue)) { // No change return 0; } else { diff --git a/packages/shared/areHookInputsEqual.js b/packages/shared/areHookInputsEqual.js index 722edaf43e7a9..8a78a113fbbda 100644 --- a/packages/shared/areHookInputsEqual.js +++ b/packages/shared/areHookInputsEqual.js @@ -8,6 +8,7 @@ */ import warning from 'shared/warning'; +import is from './objectIs'; export default function areHookInputsEqual(arr1: any[], arr2: any[]) { // Don't bother comparing lengths in prod because these arrays should be @@ -24,14 +25,7 @@ export default function areHookInputsEqual(arr1: any[], arr2: any[]) { ); } 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 - ) { + if (is(arr1[i], arr2[i])) { continue; } return false; diff --git a/packages/shared/objectIs.js b/packages/shared/objectIs.js new file mode 100644 index 0000000000000..3110e3fa5a9ca --- /dev/null +++ b/packages/shared/objectIs.js @@ -0,0 +1,20 @@ +/** + * 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 + */ + +/** + * inlined Object.is polyfill to avoid requiring consumers ship their own + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is + */ +function is(x: any, y: any) { + return ( + (x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y) // eslint-disable-line no-self-compare + ); +} + +export default is; diff --git a/packages/shared/shallowEqual.js b/packages/shared/shallowEqual.js index 10b649f40ef6f..8ff87431e0a9a 100644 --- a/packages/shared/shallowEqual.js +++ b/packages/shared/shallowEqual.js @@ -7,27 +7,10 @@ * @flow */ -/*eslint-disable no-self-compare */ +import is from './objectIs'; const hasOwnProperty = Object.prototype.hasOwnProperty; -/** - * inlined Object.is polyfill to avoid requiring consumers ship their own - * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is - */ -function is(x, y) { - // SameValue algorithm - if (x === y) { - // Steps 1-5, 7-10 - // Steps 6.b-6.e: +0 != -0 - // Added the nonzero y check to make Flow happy, but it is redundant - return x !== 0 || y !== 0 || 1 / x === 1 / y; - } else { - // Step 6.a: NaN == NaN - return x !== x && y !== y; - } -} - /** * Performs equality by iterating through keys on an object and returning false * when any key has values which are not strictly equal between the arguments.