|
| 1 | +https: //cdn.jsdelivr.net/npm/[email protected]/dist/abortcontroller-polyfill-only.js |
| 2 | +(function (factory) { |
| 3 | + typeof define === 'function' && define.amd ? define(factory) : |
| 4 | + factory(); |
| 5 | +})((function () { 'use strict'; |
| 6 | + |
| 7 | + function _classCallCheck(instance, Constructor) { |
| 8 | + if (!(instance instanceof Constructor)) { |
| 9 | + throw new TypeError("Cannot call a class as a function"); |
| 10 | + } |
| 11 | + } |
| 12 | + |
| 13 | + function _defineProperties(target, props) { |
| 14 | + for (var i = 0; i < props.length; i++) { |
| 15 | + var descriptor = props[i]; |
| 16 | + descriptor.enumerable = descriptor.enumerable || false; |
| 17 | + descriptor.configurable = true; |
| 18 | + if ("value" in descriptor) descriptor.writable = true; |
| 19 | + Object.defineProperty(target, descriptor.key, descriptor); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + function _createClass(Constructor, protoProps, staticProps) { |
| 24 | + if (protoProps) _defineProperties(Constructor.prototype, protoProps); |
| 25 | + if (staticProps) _defineProperties(Constructor, staticProps); |
| 26 | + Object.defineProperty(Constructor, "prototype", { |
| 27 | + writable: false |
| 28 | + }); |
| 29 | + return Constructor; |
| 30 | + } |
| 31 | + |
| 32 | + function _inherits(subClass, superClass) { |
| 33 | + if (typeof superClass !== "function" && superClass !== null) { |
| 34 | + throw new TypeError("Super expression must either be null or a function"); |
| 35 | + } |
| 36 | + |
| 37 | + subClass.prototype = Object.create(superClass && superClass.prototype, { |
| 38 | + constructor: { |
| 39 | + value: subClass, |
| 40 | + writable: true, |
| 41 | + configurable: true |
| 42 | + } |
| 43 | + }); |
| 44 | + Object.defineProperty(subClass, "prototype", { |
| 45 | + writable: false |
| 46 | + }); |
| 47 | + if (superClass) _setPrototypeOf(subClass, superClass); |
| 48 | + } |
| 49 | + |
| 50 | + function _getPrototypeOf(o) { |
| 51 | + _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { |
| 52 | + return o.__proto__ || Object.getPrototypeOf(o); |
| 53 | + }; |
| 54 | + return _getPrototypeOf(o); |
| 55 | + } |
| 56 | + |
| 57 | + function _setPrototypeOf(o, p) { |
| 58 | + _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { |
| 59 | + o.__proto__ = p; |
| 60 | + return o; |
| 61 | + }; |
| 62 | + return _setPrototypeOf(o, p); |
| 63 | + } |
| 64 | + |
| 65 | + function _isNativeReflectConstruct() { |
| 66 | + if (typeof Reflect === "undefined" || !Reflect.construct) return false; |
| 67 | + if (Reflect.construct.sham) return false; |
| 68 | + if (typeof Proxy === "function") return true; |
| 69 | + |
| 70 | + try { |
| 71 | + Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); |
| 72 | + return true; |
| 73 | + } catch (e) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + function _assertThisInitialized(self) { |
| 79 | + if (self === void 0) { |
| 80 | + throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); |
| 81 | + } |
| 82 | + |
| 83 | + return self; |
| 84 | + } |
| 85 | + |
| 86 | + function _possibleConstructorReturn(self, call) { |
| 87 | + if (call && (typeof call === "object" || typeof call === "function")) { |
| 88 | + return call; |
| 89 | + } else if (call !== void 0) { |
| 90 | + throw new TypeError("Derived constructors may only return object or undefined"); |
| 91 | + } |
| 92 | + |
| 93 | + return _assertThisInitialized(self); |
| 94 | + } |
| 95 | + |
| 96 | + function _createSuper(Derived) { |
| 97 | + var hasNativeReflectConstruct = _isNativeReflectConstruct(); |
| 98 | + |
| 99 | + return function _createSuperInternal() { |
| 100 | + var Super = _getPrototypeOf(Derived), |
| 101 | + result; |
| 102 | + |
| 103 | + if (hasNativeReflectConstruct) { |
| 104 | + var NewTarget = _getPrototypeOf(this).constructor; |
| 105 | + |
| 106 | + result = Reflect.construct(Super, arguments, NewTarget); |
| 107 | + } else { |
| 108 | + result = Super.apply(this, arguments); |
| 109 | + } |
| 110 | + |
| 111 | + return _possibleConstructorReturn(this, result); |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + function _superPropBase(object, property) { |
| 116 | + while (!Object.prototype.hasOwnProperty.call(object, property)) { |
| 117 | + object = _getPrototypeOf(object); |
| 118 | + if (object === null) break; |
| 119 | + } |
| 120 | + |
| 121 | + return object; |
| 122 | + } |
| 123 | + |
| 124 | + function _get() { |
| 125 | + if (typeof Reflect !== "undefined" && Reflect.get) { |
| 126 | + _get = Reflect.get.bind(); |
| 127 | + } else { |
| 128 | + _get = function _get(target, property, receiver) { |
| 129 | + var base = _superPropBase(target, property); |
| 130 | + |
| 131 | + if (!base) return; |
| 132 | + var desc = Object.getOwnPropertyDescriptor(base, property); |
| 133 | + |
| 134 | + if (desc.get) { |
| 135 | + return desc.get.call(arguments.length < 3 ? target : receiver); |
| 136 | + } |
| 137 | + |
| 138 | + return desc.value; |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + return _get.apply(this, arguments); |
| 143 | + } |
| 144 | + |
| 145 | + var Emitter = /*#__PURE__*/function () { |
| 146 | + function Emitter() { |
| 147 | + _classCallCheck(this, Emitter); |
| 148 | + |
| 149 | + Object.defineProperty(this, 'listeners', { |
| 150 | + value: {}, |
| 151 | + writable: true, |
| 152 | + configurable: true |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + _createClass(Emitter, [{ |
| 157 | + key: "addEventListener", |
| 158 | + value: function addEventListener(type, callback, options) { |
| 159 | + if (!(type in this.listeners)) { |
| 160 | + this.listeners[type] = []; |
| 161 | + } |
| 162 | + |
| 163 | + this.listeners[type].push({ |
| 164 | + callback: callback, |
| 165 | + options: options |
| 166 | + }); |
| 167 | + } |
| 168 | + }, { |
| 169 | + key: "removeEventListener", |
| 170 | + value: function removeEventListener(type, callback) { |
| 171 | + if (!(type in this.listeners)) { |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + var stack = this.listeners[type]; |
| 176 | + |
| 177 | + for (var i = 0, l = stack.length; i < l; i++) { |
| 178 | + if (stack[i].callback === callback) { |
| 179 | + stack.splice(i, 1); |
| 180 | + return; |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + }, { |
| 185 | + key: "dispatchEvent", |
| 186 | + value: function dispatchEvent(event) { |
| 187 | + if (!(event.type in this.listeners)) { |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + var stack = this.listeners[event.type]; |
| 192 | + var stackToCall = stack.slice(); |
| 193 | + |
| 194 | + for (var i = 0, l = stackToCall.length; i < l; i++) { |
| 195 | + var listener = stackToCall[i]; |
| 196 | + |
| 197 | + try { |
| 198 | + listener.callback.call(this, event); |
| 199 | + } catch (e) { |
| 200 | + Promise.resolve().then(function () { |
| 201 | + throw e; |
| 202 | + }); |
| 203 | + } |
| 204 | + |
| 205 | + if (listener.options && listener.options.once) { |
| 206 | + this.removeEventListener(event.type, listener.callback); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + return !event.defaultPrevented; |
| 211 | + } |
| 212 | + }]); |
| 213 | + |
| 214 | + return Emitter; |
| 215 | + }(); |
| 216 | + |
| 217 | + var AbortSignal = /*#__PURE__*/function (_Emitter) { |
| 218 | + _inherits(AbortSignal, _Emitter); |
| 219 | + |
| 220 | + var _super = _createSuper(AbortSignal); |
| 221 | + |
| 222 | + function AbortSignal() { |
| 223 | + var _this; |
| 224 | + |
| 225 | + _classCallCheck(this, AbortSignal); |
| 226 | + |
| 227 | + _this = _super.call(this); // Some versions of babel does not transpile super() correctly for IE <= 10, if the parent |
| 228 | + // constructor has failed to run, then "this.listeners" will still be undefined and then we call |
| 229 | + // the parent constructor directly instead as a workaround. For general details, see babel bug: |
| 230 | + // https://github.com/babel/babel/issues/3041 |
| 231 | + // This hack was added as a fix for the issue described here: |
| 232 | + // https://github.com/Financial-Times/polyfill-library/pull/59#issuecomment-477558042 |
| 233 | + |
| 234 | + if (!_this.listeners) { |
| 235 | + Emitter.call(_assertThisInitialized(_this)); |
| 236 | + } // Compared to assignment, Object.defineProperty makes properties non-enumerable by default and |
| 237 | + // we want Object.keys(new AbortController().signal) to be [] for compat with the native impl |
| 238 | + |
| 239 | + |
| 240 | + Object.defineProperty(_assertThisInitialized(_this), 'aborted', { |
| 241 | + value: false, |
| 242 | + writable: true, |
| 243 | + configurable: true |
| 244 | + }); |
| 245 | + Object.defineProperty(_assertThisInitialized(_this), 'onabort', { |
| 246 | + value: null, |
| 247 | + writable: true, |
| 248 | + configurable: true |
| 249 | + }); |
| 250 | + Object.defineProperty(_assertThisInitialized(_this), 'reason', { |
| 251 | + value: undefined, |
| 252 | + writable: true, |
| 253 | + configurable: true |
| 254 | + }); |
| 255 | + return _this; |
| 256 | + } |
| 257 | + |
| 258 | + _createClass(AbortSignal, [{ |
| 259 | + key: "toString", |
| 260 | + value: function toString() { |
| 261 | + return '[object AbortSignal]'; |
| 262 | + } |
| 263 | + }, { |
| 264 | + key: "dispatchEvent", |
| 265 | + value: function dispatchEvent(event) { |
| 266 | + if (event.type === 'abort') { |
| 267 | + this.aborted = true; |
| 268 | + |
| 269 | + if (typeof this.onabort === 'function') { |
| 270 | + this.onabort.call(this, event); |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + _get(_getPrototypeOf(AbortSignal.prototype), "dispatchEvent", this).call(this, event); |
| 275 | + } |
| 276 | + }]); |
| 277 | + |
| 278 | + return AbortSignal; |
| 279 | + }(Emitter); |
| 280 | + var AbortController = /*#__PURE__*/function () { |
| 281 | + function AbortController() { |
| 282 | + _classCallCheck(this, AbortController); |
| 283 | + |
| 284 | + // Compared to assignment, Object.defineProperty makes properties non-enumerable by default and |
| 285 | + // we want Object.keys(new AbortController()) to be [] for compat with the native impl |
| 286 | + Object.defineProperty(this, 'signal', { |
| 287 | + value: new AbortSignal(), |
| 288 | + writable: true, |
| 289 | + configurable: true |
| 290 | + }); |
| 291 | + } |
| 292 | + |
| 293 | + _createClass(AbortController, [{ |
| 294 | + key: "abort", |
| 295 | + value: function abort(reason) { |
| 296 | + var event; |
| 297 | + |
| 298 | + try { |
| 299 | + event = new Event('abort'); |
| 300 | + } catch (e) { |
| 301 | + if (typeof document !== 'undefined') { |
| 302 | + if (!document.createEvent) { |
| 303 | + // For Internet Explorer 8: |
| 304 | + event = document.createEventObject(); |
| 305 | + event.type = 'abort'; |
| 306 | + } else { |
| 307 | + // For Internet Explorer 11: |
| 308 | + event = document.createEvent('Event'); |
| 309 | + event.initEvent('abort', false, false); |
| 310 | + } |
| 311 | + } else { |
| 312 | + // Fallback where document isn't available: |
| 313 | + event = { |
| 314 | + type: 'abort', |
| 315 | + bubbles: false, |
| 316 | + cancelable: false |
| 317 | + }; |
| 318 | + } |
| 319 | + } |
| 320 | + |
| 321 | + var signalReason = reason; |
| 322 | + |
| 323 | + if (signalReason === undefined) { |
| 324 | + if (typeof document === 'undefined') { |
| 325 | + signalReason = new Error('This operation was aborted'); |
| 326 | + signalReason.name = 'AbortError'; |
| 327 | + } else { |
| 328 | + try { |
| 329 | + signalReason = new DOMException('signal is aborted without reason'); |
| 330 | + } catch (err) { |
| 331 | + // IE 11 does not support calling the DOMException constructor, use a |
| 332 | + // regular error object on it instead. |
| 333 | + signalReason = new Error('This operation was aborted'); |
| 334 | + signalReason.name = 'AbortError'; |
| 335 | + } |
| 336 | + } |
| 337 | + } |
| 338 | + |
| 339 | + this.signal.reason = signalReason; |
| 340 | + this.signal.dispatchEvent(event); |
| 341 | + } |
| 342 | + }, { |
| 343 | + key: "toString", |
| 344 | + value: function toString() { |
| 345 | + return '[object AbortController]'; |
| 346 | + } |
| 347 | + }]); |
| 348 | + |
| 349 | + return AbortController; |
| 350 | + }(); |
| 351 | + |
| 352 | + if (typeof Symbol !== 'undefined' && Symbol.toStringTag) { |
| 353 | + // These are necessary to make sure that we get correct output for: |
| 354 | + // Object.prototype.toString.call(new AbortController()) |
| 355 | + AbortController.prototype[Symbol.toStringTag] = 'AbortController'; |
| 356 | + AbortSignal.prototype[Symbol.toStringTag] = 'AbortSignal'; |
| 357 | + } |
| 358 | + |
| 359 | + function polyfillNeeded(self) { |
| 360 | + if (self.__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL) { |
| 361 | + console.log('__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL=true is set, will force install polyfill'); |
| 362 | + return true; |
| 363 | + } // Note that the "unfetch" minimal fetch polyfill defines fetch() without |
| 364 | + // defining window.Request, and this polyfill need to work on top of unfetch |
| 365 | + // so the below feature detection needs the !self.AbortController part. |
| 366 | + // The Request.prototype check is also needed because Safari versions 11.1.2 |
| 367 | + // up to and including 12.1.x has a window.AbortController present but still |
| 368 | + // does NOT correctly implement abortable fetch: |
| 369 | + // https://bugs.webkit.org/show_bug.cgi?id=174980#c2 |
| 370 | + |
| 371 | + |
| 372 | + return typeof self.Request === 'function' && !self.Request.prototype.hasOwnProperty('signal') || !self.AbortController; |
| 373 | + } |
| 374 | + |
| 375 | + (function (self) { |
| 376 | + |
| 377 | + if (!polyfillNeeded(self)) { |
| 378 | + return; |
| 379 | + } |
| 380 | + |
| 381 | + self.AbortController = AbortController; |
| 382 | + self.AbortSignal = AbortSignal; |
| 383 | + })(typeof self !== 'undefined' ? self : global); |
| 384 | + |
| 385 | +})); |
0 commit comments