PCancelable.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict';
  2. var global = (function () {
  3. if (typeof globalThis !== 'undefined') {
  4. return globalThis;
  5. } else if (typeof global !== 'undefined') {
  6. return global;
  7. } else if (typeof self !== 'undefined') {
  8. return self;
  9. } else if (typeof window !== 'undefined') {
  10. return window;
  11. } else {
  12. return Function('return this')();
  13. }
  14. })();
  15. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  16. Object.defineProperty(exports, '__esModule', {
  17. value: true
  18. });
  19. exports.default = void 0;
  20. var global = (function () {
  21. if (typeof globalThis !== 'undefined') {
  22. return globalThis;
  23. } else if (typeof global !== 'undefined') {
  24. return global;
  25. } else if (typeof self !== 'undefined') {
  26. return self;
  27. } else if (typeof window !== 'undefined') {
  28. return window;
  29. } else {
  30. return Function('return this')();
  31. }
  32. })();
  33. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  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. var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
  48. function _defineProperty(obj, key, value) {
  49. if (key in obj) {
  50. Object.defineProperty(obj, key, {
  51. value: value,
  52. enumerable: true,
  53. configurable: true,
  54. writable: true
  55. });
  56. } else {
  57. obj[key] = value;
  58. }
  59. return obj;
  60. }
  61. /**
  62. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  63. *
  64. * This source code is licensed under the MIT license found in the
  65. * LICENSE file in the root directory of this source tree.
  66. */
  67. class CancelError extends Error {
  68. constructor() {
  69. super('Promise was canceled');
  70. this.name = 'CancelError';
  71. }
  72. }
  73. class PCancelable {
  74. constructor(executor) {
  75. _defineProperty(this, '_pending', true);
  76. _defineProperty(this, '_canceled', false);
  77. _defineProperty(this, '_promise', void 0);
  78. _defineProperty(this, '_cancel', void 0);
  79. _defineProperty(this, '_reject', () => {});
  80. this._promise = new Promise((resolve, reject) => {
  81. this._reject = reject;
  82. return executor(
  83. fn => {
  84. this._cancel = fn;
  85. },
  86. val => {
  87. this._pending = false;
  88. resolve(val);
  89. },
  90. err => {
  91. this._pending = false;
  92. reject(err);
  93. }
  94. );
  95. });
  96. }
  97. then(onFulfilled, onRejected) {
  98. return this._promise.then(onFulfilled, onRejected);
  99. }
  100. catch(onRejected) {
  101. return this._promise.catch(onRejected);
  102. }
  103. cancel() {
  104. if (!this._pending || this._canceled) {
  105. return;
  106. }
  107. if (typeof this._cancel === 'function') {
  108. try {
  109. this._cancel();
  110. } catch (err) {
  111. this._reject(err);
  112. }
  113. }
  114. this._canceled = true;
  115. this._reject(new CancelError());
  116. }
  117. }
  118. exports.default = PCancelable;