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