use-sync-external-store
1.2.01.5.0
cjs/use-sync-external-store-with-selector.development.js~
cjs/use-sync-external-store-with-selector.development.jsModified+83−151
Index: package/cjs/use-sync-external-store-with-selector.development.js
===================================================================
--- package/cjs/use-sync-external-store-with-selector.development.js
+++ package/cjs/use-sync-external-store-with-selector.development.js
@@ -1,164 +1,96 @@
/**
* @license React
* use-sync-external-store-with-selector.development.js
*
- * Copyright (c) Facebook, Inc. and its affiliates.
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
-'use strict';
-
-if (process.env.NODE_ENV !== "production") {
- (function() {
-
- 'use strict';
-
-/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
-if (
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart ===
- 'function'
-) {
- __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error());
-}
- var React = require('react');
-
-/**
- * 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) {
- return x === y && (x !== 0 || 1 / x === 1 / y) || x !== x && y !== y // eslint-disable-line no-self-compare
- ;
-}
-
-var objectIs = typeof Object.is === 'function' ? Object.is : is;
-
-var useSyncExternalStore = React.useSyncExternalStore;
-
-// for CommonJS interop.
-
-var useRef = React.useRef,
- useEffect = React.useEffect,
- useMemo = React.useMemo,
- useDebugValue = React.useDebugValue; // Same as useSyncExternalStore, but supports selector and isEqual arguments.
-
-function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnapshot, selector, isEqual) {
- // Use this to track the rendered snapshot.
- var instRef = useRef(null);
- var inst;
-
- if (instRef.current === null) {
- inst = {
- hasValue: false,
- value: null
- };
- instRef.current = inst;
- } else {
- inst = instRef.current;
- }
-
- var _useMemo = useMemo(function () {
- // Track the memoized state using closure variables that are local to this
- // memoized instance of a getSnapshot function. Intentionally not using a
- // useRef hook, because that state would be shared across all concurrent
- // copies of the hook/component.
- var hasMemo = false;
- var memoizedSnapshot;
- var memoizedSelection;
-
- var memoizedSelector = function (nextSnapshot) {
- if (!hasMemo) {
- // The first time the hook is called, there is no memoized result.
- hasMemo = true;
- memoizedSnapshot = nextSnapshot;
-
- var _nextSelection = selector(nextSnapshot);
-
- if (isEqual !== undefined) {
- // Even if the selector has changed, the currently rendered selection
- // may be equal to the new selection. We should attempt to reuse the
- // current value if possible, to preserve downstream memoizations.
- if (inst.hasValue) {
- var currentSelection = inst.value;
-
- if (isEqual(currentSelection, _nextSelection)) {
- memoizedSelection = currentSelection;
- return currentSelection;
+"use strict";
+"production" !== process.env.NODE_ENV &&
+ (function () {
+ function is(x, y) {
+ return (x === y && (0 !== x || 1 / x === 1 / y)) || (x !== x && y !== y);
+ }
+ "undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
+ "function" ===
+ typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart &&
+ __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());
+ var React = require("react"),
+ objectIs = "function" === typeof Object.is ? Object.is : is,
+ useSyncExternalStore = React.useSyncExternalStore,
+ useRef = React.useRef,
+ useEffect = React.useEffect,
+ useMemo = React.useMemo,
+ useDebugValue = React.useDebugValue;
+ exports.useSyncExternalStoreWithSelector = function (
+ subscribe,
+ getSnapshot,
+ getServerSnapshot,
+ selector,
+ isEqual
+ ) {
+ var instRef = useRef(null);
+ if (null === instRef.current) {
+ var inst = { hasValue: !1, value: null };
+ instRef.current = inst;
+ } else inst = instRef.current;
+ instRef = useMemo(
+ function () {
+ function memoizedSelector(nextSnapshot) {
+ if (!hasMemo) {
+ hasMemo = !0;
+ memoizedSnapshot = nextSnapshot;
+ nextSnapshot = selector(nextSnapshot);
+ if (void 0 !== isEqual && inst.hasValue) {
+ var currentSelection = inst.value;
+ if (isEqual(currentSelection, nextSnapshot))
+ return (memoizedSelection = currentSelection);
+ }
+ return (memoizedSelection = nextSnapshot);
}
+ currentSelection = memoizedSelection;
+ if (objectIs(memoizedSnapshot, nextSnapshot))
+ return currentSelection;
+ var nextSelection = selector(nextSnapshot);
+ if (void 0 !== isEqual && isEqual(currentSelection, nextSelection))
+ return (memoizedSnapshot = nextSnapshot), currentSelection;
+ memoizedSnapshot = nextSnapshot;
+ return (memoizedSelection = nextSelection);
}
- }
-
- memoizedSelection = _nextSelection;
- return _nextSelection;
- } // We may be able to reuse the previous invocation's result.
-
-
- // We may be able to reuse the previous invocation's result.
- var prevSnapshot = memoizedSnapshot;
- var prevSelection = memoizedSelection;
-
- if (objectIs(prevSnapshot, nextSnapshot)) {
- // The snapshot is the same as last time. Reuse the previous selection.
- return prevSelection;
- } // The snapshot has changed, so we need to compute a new selection.
-
-
- // The snapshot has changed, so we need to compute a new selection.
- var nextSelection = selector(nextSnapshot); // If a custom isEqual function is provided, use that to check if the data
- // has changed. If it hasn't, return the previous selection. That signals
- // to React that the selections are conceptually equal, and we can bail
- // out of rendering.
-
- // If a custom isEqual function is provided, use that to check if the data
- // has changed. If it hasn't, return the previous selection. That signals
- // to React that the selections are conceptually equal, and we can bail
- // out of rendering.
- if (isEqual !== undefined && isEqual(prevSelection, nextSelection)) {
- return prevSelection;
- }
-
- memoizedSnapshot = nextSnapshot;
- memoizedSelection = nextSelection;
- return nextSelection;
- }; // Assigning this to a constant so that Flow knows it can't change.
-
-
- // Assigning this to a constant so that Flow knows it can't change.
- var maybeGetServerSnapshot = getServerSnapshot === undefined ? null : getServerSnapshot;
-
- var getSnapshotWithSelector = function () {
- return memoizedSelector(getSnapshot());
+ var hasMemo = !1,
+ memoizedSnapshot,
+ memoizedSelection,
+ maybeGetServerSnapshot =
+ void 0 === getServerSnapshot ? null : getServerSnapshot;
+ return [
+ function () {
+ return memoizedSelector(getSnapshot());
+ },
+ null === maybeGetServerSnapshot
+ ? void 0
+ : function () {
+ return memoizedSelector(maybeGetServerSnapshot());
+ }
+ ];
+ },
+ [getSnapshot, getServerSnapshot, selector, isEqual]
+ );
+ var value = useSyncExternalStore(subscribe, instRef[0], instRef[1]);
+ useEffect(
+ function () {
+ inst.hasValue = !0;
+ inst.value = value;
+ },
+ [value]
+ );
+ useDebugValue(value);
+ return value;
};
-
- var getServerSnapshotWithSelector = maybeGetServerSnapshot === null ? undefined : function () {
- return memoizedSelector(maybeGetServerSnapshot());
- };
- return [getSnapshotWithSelector, getServerSnapshotWithSelector];
- }, [getSnapshot, getServerSnapshot, selector, isEqual]),
- getSelection = _useMemo[0],
- getServerSelection = _useMemo[1];
-
- var value = useSyncExternalStore(subscribe, getSelection, getServerSelection);
- useEffect(function () {
- inst.hasValue = true;
- inst.value = value;
- }, [value]);
- useDebugValue(value);
- return value;
-}
-
-exports.useSyncExternalStoreWithSelector = useSyncExternalStoreWithSelector;
- /* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
-if (
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
- typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop ===
- 'function'
-) {
- __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error());
-}
-
+ "undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
+ "function" ===
+ typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&
+ __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
})();
-}