1 | 'use strict';
|
2 |
|
3 | var global = (function () {
|
4 | if (typeof globalThis !== 'undefined') {
|
5 | return globalThis;
|
6 | } else if (typeof global !== 'undefined') {
|
7 | return global;
|
8 | } else if (typeof self !== 'undefined') {
|
9 | return self;
|
10 | } else if (typeof window !== 'undefined') {
|
11 | return window;
|
12 | } else {
|
13 | return Function('return this')();
|
14 | }
|
15 | })();
|
16 |
|
17 | var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
18 | Object.defineProperty(exports, '__esModule', {
|
19 | value: true
|
20 | });
|
21 | exports.default = void 0;
|
22 |
|
23 | var global = (function () {
|
24 | if (typeof globalThis !== 'undefined') {
|
25 | return globalThis;
|
26 | } else if (typeof global !== 'undefined') {
|
27 | return global;
|
28 | } else if (typeof self !== 'undefined') {
|
29 | return self;
|
30 | } else if (typeof window !== 'undefined') {
|
31 | return window;
|
32 | } else {
|
33 | return Function('return this')();
|
34 | }
|
35 | })();
|
36 |
|
37 | var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
|
38 |
|
39 | var global = (function () {
|
40 | if (typeof globalThis !== 'undefined') {
|
41 | return globalThis;
|
42 | } else if (typeof global !== 'undefined') {
|
43 | return global;
|
44 | } else if (typeof self !== 'undefined') {
|
45 | return self;
|
46 | } else if (typeof window !== 'undefined') {
|
47 | return window;
|
48 | } else {
|
49 | return Function('return this')();
|
50 | }
|
51 | })();
|
52 |
|
53 | var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
|
54 |
|
55 | function _defineProperty(obj, key, value) {
|
56 | if (key in obj) {
|
57 | Object.defineProperty(obj, key, {
|
58 | value: value,
|
59 | enumerable: true,
|
60 | configurable: true,
|
61 | writable: true
|
62 | });
|
63 | } else {
|
64 | obj[key] = value;
|
65 | }
|
66 | return obj;
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | class CancelError extends Error {
|
76 | constructor() {
|
77 | super('Promise was canceled');
|
78 | this.name = 'CancelError';
|
79 | }
|
80 | }
|
81 |
|
82 | class PCancelable {
|
83 | constructor(executor) {
|
84 | _defineProperty(this, '_pending', true);
|
85 |
|
86 | _defineProperty(this, '_canceled', false);
|
87 |
|
88 | _defineProperty(this, '_promise', void 0);
|
89 |
|
90 | _defineProperty(this, '_cancel', void 0);
|
91 |
|
92 | _defineProperty(this, '_reject', () => {});
|
93 |
|
94 | this._promise = new Promise((resolve, reject) => {
|
95 | this._reject = reject;
|
96 | return executor(
|
97 | fn => {
|
98 | this._cancel = fn;
|
99 | },
|
100 | val => {
|
101 | this._pending = false;
|
102 | resolve(val);
|
103 | },
|
104 | err => {
|
105 | this._pending = false;
|
106 | reject(err);
|
107 | }
|
108 | );
|
109 | });
|
110 | }
|
111 |
|
112 | then(onFulfilled, onRejected) {
|
113 | return this._promise.then(onFulfilled, onRejected);
|
114 | }
|
115 |
|
116 | catch(onRejected) {
|
117 | return this._promise.catch(onRejected);
|
118 | }
|
119 |
|
120 | cancel() {
|
121 | if (!this._pending || this._canceled) {
|
122 | return;
|
123 | }
|
124 |
|
125 | if (typeof this._cancel === 'function') {
|
126 | try {
|
127 | this._cancel();
|
128 | } catch (err) {
|
129 | this._reject(err);
|
130 | }
|
131 | }
|
132 |
|
133 | this._canceled = true;
|
134 |
|
135 | this._reject(new CancelError());
|
136 | }
|
137 | }
|
138 |
|
139 | exports.default = PCancelable;
|