|
| 1 | +/** |
| 2 | + * Make the native AbortSignal instances support the reason property and the throwIfAborted method. |
| 3 | + * @param {AbortSignal} signal native AbortSignal instance |
| 4 | + * @returns {AbortSignal} AbortSignal instance |
| 5 | + */ |
| 6 | +export function abortsignalPonyfill(signal) { |
| 7 | + if (!('reason' in signal)) { |
| 8 | + signal._reason = undefined |
| 9 | + signal._onabort = null |
| 10 | + |
| 11 | + Object.defineProperties(signal, { |
| 12 | + __ponyfill__: { |
| 13 | + value: true, |
| 14 | + }, |
| 15 | + |
| 16 | + reason: { |
| 17 | + get() { |
| 18 | + return this._reason |
| 19 | + } |
| 20 | + }, |
| 21 | + |
| 22 | + onabort: { |
| 23 | + get() { |
| 24 | + return this._onabort |
| 25 | + }, |
| 26 | + set(callback) { |
| 27 | + const existing = this._onabort |
| 28 | + if (existing) { |
| 29 | + this.removeEventListener('abort', existing) |
| 30 | + } |
| 31 | + this._onabort = callback |
| 32 | + this.addEventListener('abort', callback) |
| 33 | + } |
| 34 | + } |
| 35 | + }) |
| 36 | + |
| 37 | + const { dispatchEvent, addEventListener, removeEventListener } = signal |
| 38 | + |
| 39 | + signal.addEventListener = function (type, callback, options) { |
| 40 | + if (type === 'abort' && callback && this.__ponyfill__) { |
| 41 | + if (!callback.__ponyfill__) { |
| 42 | + const rawCallback = callback |
| 43 | + Object.defineProperty(callback, '__ponyfill__', { |
| 44 | + value(e) { |
| 45 | + if (e.__ponyfill__) { |
| 46 | + return rawCallback.call(this, e) |
| 47 | + } |
| 48 | + } |
| 49 | + }) |
| 50 | + } |
| 51 | + callback = callback.__ponyfill__ |
| 52 | + } |
| 53 | + return addEventListener.call(this, type, callback, options) |
| 54 | + } |
| 55 | + |
| 56 | + signal.removeEventListener = function (type, callback, options) { |
| 57 | + if (type === 'abort' && callback && this.__ponyfill__ && callback.__ponyfill__) { |
| 58 | + callback = callback.__ponyfill__ |
| 59 | + } |
| 60 | + return removeEventListener.call(this, type, callback, options) |
| 61 | + } |
| 62 | + |
| 63 | + signal.dispatchEvent = function (event) { |
| 64 | + if (event.type === 'abort') { |
| 65 | + Object.defineProperty(event, '__ponyfill__', { |
| 66 | + value: true, |
| 67 | + }) |
| 68 | + } |
| 69 | + return dispatchEvent.call(this, event) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if (!('throwIfAborted') in signal) { |
| 74 | + signal.throwIfAborted = function throwIfAborted() { |
| 75 | + if (this.aborted) { |
| 76 | + throw this.reason |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return signal |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * @param {any} reason abort reason |
| 86 | + */ |
| 87 | +export function createAbortEvent(reason) { |
| 88 | + let event; |
| 89 | + try { |
| 90 | + event = new Event('abort'); |
| 91 | + } catch (e) { |
| 92 | + if (typeof document !== 'undefined') { |
| 93 | + if (!document.createEvent) { |
| 94 | + // For Internet Explorer 8: |
| 95 | + event = document.createEventObject(); |
| 96 | + event.type = 'abort'; |
| 97 | + } else { |
| 98 | + // For Internet Explorer 11: |
| 99 | + event = document.createEvent('Event'); |
| 100 | + event.initEvent('abort', false, false); |
| 101 | + } |
| 102 | + } else { |
| 103 | + // Fallback where document isn't available: |
| 104 | + event = { |
| 105 | + type: 'abort', |
| 106 | + bubbles: false, |
| 107 | + cancelable: false, |
| 108 | + }; |
| 109 | + } |
| 110 | + } |
| 111 | + event.reason = reason |
| 112 | + return event |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * @param {any} reason abort reason |
| 117 | + */ |
| 118 | +export function normalizeAbortReason(reason) { |
| 119 | + if (reason === undefined) { |
| 120 | + if (typeof document === 'undefined') { |
| 121 | + reason = new Error('This operation was aborted'); |
| 122 | + reason.name = 'AbortError'; |
| 123 | + } else { |
| 124 | + try { |
| 125 | + reason = new DOMException('signal is aborted without reason'); |
| 126 | + reason.name = 'AbortError'; |
| 127 | + } catch (err) { |
| 128 | + // IE 11 does not support calling the DOMException constructor, use a |
| 129 | + // regular error object on it instead. |
| 130 | + reason = new Error('This operation was aborted'); |
| 131 | + reason.name = 'AbortError'; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + return reason |
| 136 | +} |
0 commit comments