act-compat.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. exports.getIsReactActEnvironment = getIsReactActEnvironment;
  7. exports.setReactActEnvironment = setIsReactActEnvironment;
  8. var testUtils = _interopRequireWildcard(require("react-dom/test-utils"));
  9. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  10. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  11. const domAct = testUtils.act;
  12. function getGlobalThis() {
  13. /* istanbul ignore else */
  14. if (typeof globalThis !== 'undefined') {
  15. return globalThis;
  16. }
  17. /* istanbul ignore next */
  18. if (typeof self !== 'undefined') {
  19. return self;
  20. }
  21. /* istanbul ignore next */
  22. if (typeof window !== 'undefined') {
  23. return window;
  24. }
  25. /* istanbul ignore next */
  26. if (typeof global !== 'undefined') {
  27. return global;
  28. }
  29. /* istanbul ignore next */
  30. throw new Error('unable to locate global object');
  31. }
  32. function setIsReactActEnvironment(isReactActEnvironment) {
  33. getGlobalThis().IS_REACT_ACT_ENVIRONMENT = isReactActEnvironment;
  34. }
  35. function getIsReactActEnvironment() {
  36. return getGlobalThis().IS_REACT_ACT_ENVIRONMENT;
  37. }
  38. function withGlobalActEnvironment(actImplementation) {
  39. return callback => {
  40. const previousActEnvironment = getIsReactActEnvironment();
  41. setIsReactActEnvironment(true);
  42. try {
  43. // The return value of `act` is always a thenable.
  44. let callbackNeedsToBeAwaited = false;
  45. const actResult = actImplementation(() => {
  46. const result = callback();
  47. if (result !== null && typeof result === 'object' && typeof result.then === 'function') {
  48. callbackNeedsToBeAwaited = true;
  49. }
  50. return result;
  51. });
  52. if (callbackNeedsToBeAwaited) {
  53. const thenable = actResult;
  54. return {
  55. then: (resolve, reject) => {
  56. thenable.then(returnValue => {
  57. setIsReactActEnvironment(previousActEnvironment);
  58. resolve(returnValue);
  59. }, error => {
  60. setIsReactActEnvironment(previousActEnvironment);
  61. reject(error);
  62. });
  63. }
  64. };
  65. } else {
  66. setIsReactActEnvironment(previousActEnvironment);
  67. return actResult;
  68. }
  69. } catch (error) {
  70. // Can't be a `finally {}` block since we don't know if we have to immediately restore IS_REACT_ACT_ENVIRONMENT
  71. // or if we have to await the callback first.
  72. setIsReactActEnvironment(previousActEnvironment);
  73. throw error;
  74. }
  75. };
  76. }
  77. const act = withGlobalActEnvironment(domAct);
  78. var _default = act;
  79. /* eslint no-console:0 */
  80. exports.default = _default;