use-sync-external-store-shim.development.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @license React
  3. * use-sync-external-store-shim.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. function useSyncExternalStore$2(subscribe, getSnapshot) {
  17. didWarnOld18Alpha ||
  18. void 0 === React.startTransition ||
  19. ((didWarnOld18Alpha = !0),
  20. console.error(
  21. "You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."
  22. ));
  23. var value = getSnapshot();
  24. if (!didWarnUncachedGetSnapshot) {
  25. var cachedValue = getSnapshot();
  26. objectIs(value, cachedValue) ||
  27. (console.error(
  28. "The result of getSnapshot should be cached to avoid an infinite loop"
  29. ),
  30. (didWarnUncachedGetSnapshot = !0));
  31. }
  32. cachedValue = useState({
  33. inst: { value: value, getSnapshot: getSnapshot }
  34. });
  35. var inst = cachedValue[0].inst,
  36. forceUpdate = cachedValue[1];
  37. useLayoutEffect(
  38. function () {
  39. inst.value = value;
  40. inst.getSnapshot = getSnapshot;
  41. checkIfSnapshotChanged(inst) && forceUpdate({ inst: inst });
  42. },
  43. [subscribe, value, getSnapshot]
  44. );
  45. useEffect(
  46. function () {
  47. checkIfSnapshotChanged(inst) && forceUpdate({ inst: inst });
  48. return subscribe(function () {
  49. checkIfSnapshotChanged(inst) && forceUpdate({ inst: inst });
  50. });
  51. },
  52. [subscribe]
  53. );
  54. useDebugValue(value);
  55. return value;
  56. }
  57. function checkIfSnapshotChanged(inst) {
  58. var latestGetSnapshot = inst.getSnapshot;
  59. inst = inst.value;
  60. try {
  61. var nextValue = latestGetSnapshot();
  62. return !objectIs(inst, nextValue);
  63. } catch (error) {
  64. return !0;
  65. }
  66. }
  67. function useSyncExternalStore$1(subscribe, getSnapshot) {
  68. return getSnapshot();
  69. }
  70. "undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
  71. "function" ===
  72. typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart &&
  73. __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(Error());
  74. var React = require("react"),
  75. objectIs = "function" === typeof Object.is ? Object.is : is,
  76. useState = React.useState,
  77. useEffect = React.useEffect,
  78. useLayoutEffect = React.useLayoutEffect,
  79. useDebugValue = React.useDebugValue,
  80. didWarnOld18Alpha = !1,
  81. didWarnUncachedGetSnapshot = !1,
  82. shim =
  83. "undefined" === typeof window ||
  84. "undefined" === typeof window.document ||
  85. "undefined" === typeof window.document.createElement
  86. ? useSyncExternalStore$1
  87. : useSyncExternalStore$2;
  88. exports.useSyncExternalStore =
  89. void 0 !== React.useSyncExternalStore ? React.useSyncExternalStore : shim;
  90. "undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
  91. "function" ===
  92. typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&
  93. __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(Error());
  94. })();