with-selector.development.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * @license React
  3. * use-sync-external-store-shim/with-selector.development.js
  4. *
  5. * Copyright (c) Meta Platforms, Inc. and affiliates.
  6. *
  7. * This source code is licensed under the MIT license found in the
  8. * LICENSE file in the root directory of this source tree.
  9. */
  10. "use strict";
  11. "production" !== process.env.NODE_ENV &&
  12. (function () {
  13. function is(x, y) {
  14. return (x === y && (0 !== x || 1 / x === 1 / y)) || (x !== x && y !== y);
  15. }
  16. "undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
  17. "function" ===
  18. typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart &&
  19. __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());
  20. var React = require("react"),
  21. shim = require("use-sync-external-store/shim"),
  22. objectIs = "function" === typeof Object.is ? Object.is : is,
  23. useSyncExternalStore = shim.useSyncExternalStore,
  24. useRef = React.useRef,
  25. useEffect = React.useEffect,
  26. useMemo = React.useMemo,
  27. useDebugValue = React.useDebugValue;
  28. exports.useSyncExternalStoreWithSelector = function (
  29. subscribe,
  30. getSnapshot,
  31. getServerSnapshot,
  32. selector,
  33. isEqual
  34. ) {
  35. var instRef = useRef(null);
  36. if (null === instRef.current) {
  37. var inst = { hasValue: !1, value: null };
  38. instRef.current = inst;
  39. } else inst = instRef.current;
  40. instRef = useMemo(
  41. function () {
  42. function memoizedSelector(nextSnapshot) {
  43. if (!hasMemo) {
  44. hasMemo = !0;
  45. memoizedSnapshot = nextSnapshot;
  46. nextSnapshot = selector(nextSnapshot);
  47. if (void 0 !== isEqual && inst.hasValue) {
  48. var currentSelection = inst.value;
  49. if (isEqual(currentSelection, nextSnapshot))
  50. return (memoizedSelection = currentSelection);
  51. }
  52. return (memoizedSelection = nextSnapshot);
  53. }
  54. currentSelection = memoizedSelection;
  55. if (objectIs(memoizedSnapshot, nextSnapshot))
  56. return currentSelection;
  57. var nextSelection = selector(nextSnapshot);
  58. if (void 0 !== isEqual && isEqual(currentSelection, nextSelection))
  59. return (memoizedSnapshot = nextSnapshot), currentSelection;
  60. memoizedSnapshot = nextSnapshot;
  61. return (memoizedSelection = nextSelection);
  62. }
  63. var hasMemo = !1,
  64. memoizedSnapshot,
  65. memoizedSelection,
  66. maybeGetServerSnapshot =
  67. void 0 === getServerSnapshot ? null : getServerSnapshot;
  68. return [
  69. function () {
  70. return memoizedSelector(getSnapshot());
  71. },
  72. null === maybeGetServerSnapshot
  73. ? void 0
  74. : function () {
  75. return memoizedSelector(maybeGetServerSnapshot());
  76. }
  77. ];
  78. },
  79. [getSnapshot, getServerSnapshot, selector, isEqual]
  80. );
  81. var value = useSyncExternalStore(subscribe, instRef[0], instRef[1]);
  82. useEffect(
  83. function () {
  84. inst.hasValue = !0;
  85. inst.value = value;
  86. },
  87. [value]
  88. );
  89. useDebugValue(value);
  90. return value;
  91. };
  92. "undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
  93. "function" ===
  94. typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&
  95. __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
  96. })();