web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.3 KiB

  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.asyncAct = asyncAct;
  8. exports.default = void 0;
  9. var React = _interopRequireWildcard(require("react"));
  10. var _reactDom = _interopRequireDefault(require("react-dom"));
  11. var testUtils = _interopRequireWildcard(require("react-dom/test-utils"));
  12. const reactAct = testUtils.act;
  13. const actSupported = reactAct !== undefined; // act is supported react-dom@16.8.0
  14. // so for versions that don't have act from test utils
  15. // we do this little polyfill. No warnings, but it's
  16. // better than nothing.
  17. function actPolyfill(cb) {
  18. _reactDom.default.unstable_batchedUpdates(cb);
  19. _reactDom.default.render( /*#__PURE__*/React.createElement("div", null), document.createElement('div'));
  20. }
  21. const act = reactAct || actPolyfill;
  22. let youHaveBeenWarned = false;
  23. let isAsyncActSupported = null;
  24. function asyncAct(cb) {
  25. if (actSupported === true) {
  26. if (isAsyncActSupported === null) {
  27. return new Promise((resolve, reject) => {
  28. // patch console.error here
  29. const originalConsoleError = console.error;
  30. console.error = function error(...args) {
  31. /* if console.error fired *with that specific message* */
  32. /* istanbul ignore next */
  33. const firstArgIsString = typeof args[0] === 'string';
  34. if (firstArgIsString && args[0].indexOf('Warning: Do not await the result of calling ReactTestUtils.act') === 0) {
  35. // v16.8.6
  36. isAsyncActSupported = false;
  37. } else if (firstArgIsString && args[0].indexOf('Warning: The callback passed to ReactTestUtils.act(...) function must not return anything') === 0) {// no-op
  38. } else {
  39. originalConsoleError.apply(console, args);
  40. }
  41. };
  42. let cbReturn, result;
  43. try {
  44. result = reactAct(() => {
  45. cbReturn = cb();
  46. return cbReturn;
  47. });
  48. } catch (err) {
  49. console.error = originalConsoleError;
  50. reject(err);
  51. return;
  52. }
  53. result.then(() => {
  54. console.error = originalConsoleError; // if it got here, it means async act is supported
  55. isAsyncActSupported = true;
  56. resolve();
  57. }, err => {
  58. console.error = originalConsoleError;
  59. isAsyncActSupported = true;
  60. reject(err);
  61. }); // 16.8.6's act().then() doesn't call a resolve handler, so we need to manually flush here, sigh
  62. if (isAsyncActSupported === false) {
  63. console.error = originalConsoleError;
  64. /* istanbul ignore next */
  65. if (!youHaveBeenWarned) {
  66. // if act is supported and async act isn't and they're trying to use async
  67. // act, then they need to upgrade from 16.8 to 16.9.
  68. // This is a seamless upgrade, so we'll add a warning
  69. console.error(`It looks like you're using a version of react-dom that supports the "act" function, but not an awaitable version of "act" which you will need. Please upgrade to at least react-dom@16.9.0 to remove this warning.`);
  70. youHaveBeenWarned = true;
  71. }
  72. cbReturn.then(() => {
  73. // a faux-version.
  74. // todo - copy https://github.com/facebook/react/blob/master/packages/shared/enqueueTask.js
  75. Promise.resolve().then(() => {
  76. // use sync act to flush effects
  77. act(() => {});
  78. resolve();
  79. });
  80. }, reject);
  81. }
  82. });
  83. } else if (isAsyncActSupported === false) {
  84. // use the polyfill directly
  85. let result;
  86. act(() => {
  87. result = cb();
  88. });
  89. return result.then(() => {
  90. return Promise.resolve().then(() => {
  91. // use sync act to flush effects
  92. act(() => {});
  93. });
  94. });
  95. } // all good! regular act
  96. return act(cb);
  97. } // use the polyfill
  98. let result;
  99. act(() => {
  100. result = cb();
  101. });
  102. return result.then(() => {
  103. return Promise.resolve().then(() => {
  104. // use sync act to flush effects
  105. act(() => {});
  106. });
  107. });
  108. }
  109. var _default = act;
  110. /* eslint no-console:0 */
  111. exports.default = _default;