use-sync-external-store-shim.production.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * @license React
  3. * use-sync-external-store-shim.production.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. var React = require("react");
  12. function is(x, y) {
  13. return (x === y && (0 !== x || 1 / x === 1 / y)) || (x !== x && y !== y);
  14. }
  15. var objectIs = "function" === typeof Object.is ? Object.is : is,
  16. useState = React.useState,
  17. useEffect = React.useEffect,
  18. useLayoutEffect = React.useLayoutEffect,
  19. useDebugValue = React.useDebugValue;
  20. function useSyncExternalStore$2(subscribe, getSnapshot) {
  21. var value = getSnapshot(),
  22. _useState = useState({ inst: { value: value, getSnapshot: getSnapshot } }),
  23. inst = _useState[0].inst,
  24. forceUpdate = _useState[1];
  25. useLayoutEffect(
  26. function () {
  27. inst.value = value;
  28. inst.getSnapshot = getSnapshot;
  29. checkIfSnapshotChanged(inst) && forceUpdate({ inst: inst });
  30. },
  31. [subscribe, value, getSnapshot]
  32. );
  33. useEffect(
  34. function () {
  35. checkIfSnapshotChanged(inst) && forceUpdate({ inst: inst });
  36. return subscribe(function () {
  37. checkIfSnapshotChanged(inst) && forceUpdate({ inst: inst });
  38. });
  39. },
  40. [subscribe]
  41. );
  42. useDebugValue(value);
  43. return value;
  44. }
  45. function checkIfSnapshotChanged(inst) {
  46. var latestGetSnapshot = inst.getSnapshot;
  47. inst = inst.value;
  48. try {
  49. var nextValue = latestGetSnapshot();
  50. return !objectIs(inst, nextValue);
  51. } catch (error) {
  52. return !0;
  53. }
  54. }
  55. function useSyncExternalStore$1(subscribe, getSnapshot) {
  56. return getSnapshot();
  57. }
  58. var shim =
  59. "undefined" === typeof window ||
  60. "undefined" === typeof window.document ||
  61. "undefined" === typeof window.document.createElement
  62. ? useSyncExternalStore$1
  63. : useSyncExternalStore$2;
  64. exports.useSyncExternalStore =
  65. void 0 !== React.useSyncExternalStore ? React.useSyncExternalStore : shim;