queueRunner.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = queueRunner;
  6. var _jestUtil = require('jest-util');
  7. var _PCancelable = _interopRequireDefault(require('./PCancelable'));
  8. var _pTimeout = _interopRequireDefault(require('./pTimeout'));
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {default: obj};
  11. }
  12. var global = (function () {
  13. if (typeof globalThis !== 'undefined') {
  14. return globalThis;
  15. } else if (typeof global !== 'undefined') {
  16. return global;
  17. } else if (typeof self !== 'undefined') {
  18. return self;
  19. } else if (typeof window !== 'undefined') {
  20. return window;
  21. } else {
  22. return Function('return this')();
  23. }
  24. })();
  25. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  26. var global = (function () {
  27. if (typeof globalThis !== 'undefined') {
  28. return globalThis;
  29. } else if (typeof global !== 'undefined') {
  30. return global;
  31. } else if (typeof self !== 'undefined') {
  32. return self;
  33. } else if (typeof window !== 'undefined') {
  34. return window;
  35. } else {
  36. return Function('return this')();
  37. }
  38. })();
  39. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  40. var global = (function () {
  41. if (typeof globalThis !== 'undefined') {
  42. return globalThis;
  43. } else if (typeof global !== 'undefined') {
  44. return global;
  45. } else if (typeof self !== 'undefined') {
  46. return self;
  47. } else if (typeof window !== 'undefined') {
  48. return window;
  49. } else {
  50. return Function('return this')();
  51. }
  52. })();
  53. var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
  54. function queueRunner(options) {
  55. const token = new _PCancelable.default((onCancel, resolve) => {
  56. onCancel(resolve);
  57. });
  58. const mapper = ({fn, timeout, initError = new Error()}) => {
  59. let promise = new Promise(resolve => {
  60. const next = function (...args) {
  61. const err = args[0];
  62. if (err) {
  63. options.fail.apply(null, args);
  64. }
  65. resolve();
  66. };
  67. next.fail = function (...args) {
  68. options.fail.apply(null, args);
  69. resolve();
  70. };
  71. try {
  72. fn.call(options.userContext, next);
  73. } catch (e) {
  74. options.onException(e);
  75. resolve();
  76. }
  77. });
  78. promise = Promise.race([promise, token]);
  79. if (!timeout) {
  80. return promise;
  81. }
  82. const timeoutMs = timeout();
  83. return (0, _pTimeout.default)(
  84. promise,
  85. timeoutMs,
  86. options.clearTimeout,
  87. options.setTimeout,
  88. () => {
  89. initError.message =
  90. 'Timeout - Async callback was not invoked within the ' +
  91. (0, _jestUtil.formatTime)(timeoutMs) +
  92. ' timeout specified by jest.setTimeout.';
  93. initError.stack = initError.message + initError.stack;
  94. options.onException(initError);
  95. }
  96. );
  97. };
  98. const result = options.queueableFns.reduce(
  99. (promise, fn) => promise.then(() => mapper(fn)),
  100. Promise.resolve()
  101. );
  102. return {
  103. cancel: token.cancel.bind(token),
  104. catch: result.catch.bind(result),
  105. then: result.then.bind(result)
  106. };
  107. }