use-sync-external-store-with-selector.development.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @license React
  3. * use-sync-external-store-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. objectIs = "function" === typeof Object.is ? Object.is : is,
  22. useSyncExternalStore = React.useSyncExternalStore,
  23. useRef = React.useRef,
  24. useEffect = React.useEffect,
  25. useMemo = React.useMemo,
  26. useDebugValue = React.useDebugValue;
  27. exports.useSyncExternalStoreWithSelector = function (
  28. subscribe,
  29. getSnapshot,
  30. getServerSnapshot,
  31. selector,
  32. isEqual
  33. ) {
  34. var instRef = useRef(null);
  35. if (null === instRef.current) {
  36. var inst = { hasValue: !1, value: null };
  37. instRef.current = inst;
  38. } else inst = instRef.current;
  39. instRef = useMemo(
  40. function () {
  41. function memoizedSelector(nextSnapshot) {
  42. if (!hasMemo) {
  43. hasMemo = !0;
  44. memoizedSnapshot = nextSnapshot;
  45. nextSnapshot = selector(nextSnapshot);
  46. if (void 0 !== isEqual && inst.hasValue) {
  47. var currentSelection = inst.value;
  48. if (isEqual(currentSelection, nextSnapshot))
  49. return (memoizedSelection = currentSelection);
  50. }
  51. return (memoizedSelection = nextSnapshot);
  52. }
  53. currentSelection = memoizedSelection;
  54. if (objectIs(memoizedSnapshot, nextSnapshot))
  55. return currentSelection;
  56. var nextSelection = selector(nextSnapshot);
  57. if (void 0 !== isEqual && isEqual(currentSelection, nextSelection))
  58. return (memoizedSnapshot = nextSnapshot), currentSelection;
  59. memoizedSnapshot = nextSnapshot;
  60. return (memoizedSelection = nextSelection);
  61. }
  62. var hasMemo = !1,
  63. memoizedSnapshot,
  64. memoizedSelection,
  65. maybeGetServerSnapshot =
  66. void 0 === getServerSnapshot ? null : getServerSnapshot;
  67. return [
  68. function () {
  69. return memoizedSelector(getSnapshot());
  70. },
  71. null === maybeGetServerSnapshot
  72. ? void 0
  73. : function () {
  74. return memoizedSelector(maybeGetServerSnapshot());
  75. }
  76. ];
  77. },
  78. [getSnapshot, getServerSnapshot, selector, isEqual]
  79. );
  80. var value = useSyncExternalStore(subscribe, instRef[0], instRef[1]);
  81. useEffect(
  82. function () {
  83. inst.hasValue = !0;
  84. inst.value = value;
  85. },
  86. [value]
  87. );
  88. useDebugValue(value);
  89. return value;
  90. };
  91. "undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
  92. "function" ===
  93. typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&
  94. __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
  95. })();