UNPKG

7.42 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
3 typeof define === 'function' && define.amd ? define(factory) :
4 (factory());
5}(this, (function () { 'use strict';
6
7 var classCallCheck = function (instance, Constructor) {
8 if (!(instance instanceof Constructor)) {
9 throw new TypeError("Cannot call a class as a function");
10 }
11 };
12
13 var createClass = function () {
14 function defineProperties(target, props) {
15 for (var i = 0; i < props.length; i++) {
16 var descriptor = props[i];
17 descriptor.enumerable = descriptor.enumerable || false;
18 descriptor.configurable = true;
19 if ("value" in descriptor) descriptor.writable = true;
20 Object.defineProperty(target, descriptor.key, descriptor);
21 }
22 }
23
24 return function (Constructor, protoProps, staticProps) {
25 if (protoProps) defineProperties(Constructor.prototype, protoProps);
26 if (staticProps) defineProperties(Constructor, staticProps);
27 return Constructor;
28 };
29 }();
30
31 var get = function get(object, property, receiver) {
32 if (object === null) object = Function.prototype;
33 var desc = Object.getOwnPropertyDescriptor(object, property);
34
35 if (desc === undefined) {
36 var parent = Object.getPrototypeOf(object);
37
38 if (parent === null) {
39 return undefined;
40 } else {
41 return get(parent, property, receiver);
42 }
43 } else if ("value" in desc) {
44 return desc.value;
45 } else {
46 var getter = desc.get;
47
48 if (getter === undefined) {
49 return undefined;
50 }
51
52 return getter.call(receiver);
53 }
54 };
55
56 var inherits = function (subClass, superClass) {
57 if (typeof superClass !== "function" && superClass !== null) {
58 throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
59 }
60
61 subClass.prototype = Object.create(superClass && superClass.prototype, {
62 constructor: {
63 value: subClass,
64 enumerable: false,
65 writable: true,
66 configurable: true
67 }
68 });
69 if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
70 };
71
72 var possibleConstructorReturn = function (self, call) {
73 if (!self) {
74 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
75 }
76
77 return call && (typeof call === "object" || typeof call === "function") ? call : self;
78 };
79
80 var Emitter = function () {
81 function Emitter() {
82 classCallCheck(this, Emitter);
83
84 this.listeners = {};
85 }
86
87 createClass(Emitter, [{
88 key: 'addEventListener',
89 value: function addEventListener(type, callback) {
90 if (!(type in this.listeners)) {
91 this.listeners[type] = [];
92 }
93 this.listeners[type].push(callback);
94 }
95 }, {
96 key: 'removeEventListener',
97 value: function removeEventListener(type, callback) {
98 if (!(type in this.listeners)) {
99 return;
100 }
101 var stack = this.listeners[type];
102 for (var i = 0, l = stack.length; i < l; i++) {
103 if (stack[i] === callback) {
104 stack.splice(i, 1);
105 return;
106 }
107 }
108 }
109 }, {
110 key: 'dispatchEvent',
111 value: function dispatchEvent(event) {
112 var _this = this;
113
114 if (!(event.type in this.listeners)) {
115 return;
116 }
117 var debounce = function debounce(callback) {
118 setTimeout(function () {
119 return callback.call(_this, event);
120 });
121 };
122 var stack = this.listeners[event.type];
123 for (var i = 0, l = stack.length; i < l; i++) {
124 debounce(stack[i]);
125 }
126 return !event.defaultPrevented;
127 }
128 }]);
129 return Emitter;
130 }();
131
132 var AbortSignal = function (_Emitter) {
133 inherits(AbortSignal, _Emitter);
134
135 function AbortSignal() {
136 classCallCheck(this, AbortSignal);
137
138 var _this2 = possibleConstructorReturn(this, (AbortSignal.__proto__ || Object.getPrototypeOf(AbortSignal)).call(this));
139
140 _this2.aborted = false;
141 _this2.onabort = null;
142 return _this2;
143 }
144
145 createClass(AbortSignal, [{
146 key: 'toString',
147 value: function toString() {
148 return '[object AbortSignal]';
149 }
150 }, {
151 key: 'dispatchEvent',
152 value: function dispatchEvent(event) {
153 if (event.type === 'abort') {
154 this.aborted = true;
155 if (typeof this.onabort === 'function') {
156 this.onabort.call(this, event);
157 }
158 }
159
160 get(AbortSignal.prototype.__proto__ || Object.getPrototypeOf(AbortSignal.prototype), 'dispatchEvent', this).call(this, event);
161 }
162 }]);
163 return AbortSignal;
164 }(Emitter);
165
166 var AbortController = function () {
167 function AbortController() {
168 classCallCheck(this, AbortController);
169
170 this.signal = new AbortSignal();
171 }
172
173 createClass(AbortController, [{
174 key: 'abort',
175 value: function abort() {
176 var event = void 0;
177 try {
178 event = new Event('abort');
179 } catch (e) {
180 if (typeof document !== 'undefined') {
181 if (!document.createEvent) {
182 // For Internet Explorer 8:
183 event = document.createEventObject();
184 event.type = 'abort';
185 } else {
186 // For Internet Explorer 11:
187 event = document.createEvent('Event');
188 event.initEvent('abort', false, false);
189 }
190 } else {
191 // Fallback where document isn't available:
192 event = {
193 type: 'abort',
194 bubbles: false,
195 cancelable: false
196 };
197 }
198 }
199 this.signal.dispatchEvent(event);
200 }
201 }, {
202 key: 'toString',
203 value: function toString() {
204 return '[object AbortController]';
205 }
206 }]);
207 return AbortController;
208 }();
209
210 if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
211 // These are necessary to make sure that we get correct output for:
212 // Object.prototype.toString.call(new AbortController())
213 AbortController.prototype[Symbol.toStringTag] = 'AbortController';
214 AbortSignal.prototype[Symbol.toStringTag] = 'AbortSignal';
215 }
216
217 function polyfillNeeded(self) {
218 if (self.__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL) {
219 console.log('__FORCE_INSTALL_ABORTCONTROLLER_POLYFILL=true is set, will force install polyfill');
220 return true;
221 }
222
223 // Note that the "unfetch" minimal fetch polyfill defines fetch() without
224 // defining window.Request, and this polyfill need to work on top of unfetch
225 // so the below feature detection needs the !self.AbortController part.
226 // The Request.prototype check is also needed because Safari versions 11.1.2
227 // up to and including 12.1.x has a window.AbortController present but still
228 // does NOT correctly implement abortable fetch:
229 // https://bugs.webkit.org/show_bug.cgi?id=174980#c2
230 return typeof self.Request === 'function' && !self.Request.prototype.hasOwnProperty('signal') || !self.AbortController;
231 }
232
233 (function (self) {
234
235 if (!polyfillNeeded(self)) {
236 return;
237 }
238
239 self.AbortController = AbortController;
240 self.AbortSignal = AbortSignal;
241 })(typeof self !== 'undefined' ? self : global);
242
243})));