UNPKG

5.95 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var classCallCheck = function (instance, Constructor) {
6 if (!(instance instanceof Constructor)) {
7 throw new TypeError("Cannot call a class as a function");
8 }
9};
10
11var createClass = function () {
12 function defineProperties(target, props) {
13 for (var i = 0; i < props.length; i++) {
14 var descriptor = props[i];
15 descriptor.enumerable = descriptor.enumerable || false;
16 descriptor.configurable = true;
17 if ("value" in descriptor) descriptor.writable = true;
18 Object.defineProperty(target, descriptor.key, descriptor);
19 }
20 }
21
22 return function (Constructor, protoProps, staticProps) {
23 if (protoProps) defineProperties(Constructor.prototype, protoProps);
24 if (staticProps) defineProperties(Constructor, staticProps);
25 return Constructor;
26 };
27}();
28
29var get = function get(object, property, receiver) {
30 if (object === null) object = Function.prototype;
31 var desc = Object.getOwnPropertyDescriptor(object, property);
32
33 if (desc === undefined) {
34 var parent = Object.getPrototypeOf(object);
35
36 if (parent === null) {
37 return undefined;
38 } else {
39 return get(parent, property, receiver);
40 }
41 } else if ("value" in desc) {
42 return desc.value;
43 } else {
44 var getter = desc.get;
45
46 if (getter === undefined) {
47 return undefined;
48 }
49
50 return getter.call(receiver);
51 }
52};
53
54var inherits = function (subClass, superClass) {
55 if (typeof superClass !== "function" && superClass !== null) {
56 throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
57 }
58
59 subClass.prototype = Object.create(superClass && superClass.prototype, {
60 constructor: {
61 value: subClass,
62 enumerable: false,
63 writable: true,
64 configurable: true
65 }
66 });
67 if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
68};
69
70var possibleConstructorReturn = function (self, call) {
71 if (!self) {
72 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
73 }
74
75 return call && (typeof call === "object" || typeof call === "function") ? call : self;
76};
77
78var Emitter = function () {
79 function Emitter() {
80 classCallCheck(this, Emitter);
81
82 this.listeners = {};
83 }
84
85 createClass(Emitter, [{
86 key: 'addEventListener',
87 value: function addEventListener(type, callback) {
88 if (!(type in this.listeners)) {
89 this.listeners[type] = [];
90 }
91 this.listeners[type].push(callback);
92 }
93 }, {
94 key: 'removeEventListener',
95 value: function removeEventListener(type, callback) {
96 if (!(type in this.listeners)) {
97 return;
98 }
99 var stack = this.listeners[type];
100 for (var i = 0, l = stack.length; i < l; i++) {
101 if (stack[i] === callback) {
102 stack.splice(i, 1);
103 return;
104 }
105 }
106 }
107 }, {
108 key: 'dispatchEvent',
109 value: function dispatchEvent(event) {
110 var _this = this;
111
112 if (!(event.type in this.listeners)) {
113 return;
114 }
115 var debounce = function debounce(callback) {
116 setTimeout(function () {
117 return callback.call(_this, event);
118 });
119 };
120 var stack = this.listeners[event.type];
121 for (var i = 0, l = stack.length; i < l; i++) {
122 debounce(stack[i]);
123 }
124 return !event.defaultPrevented;
125 }
126 }]);
127 return Emitter;
128}();
129
130var AbortSignal = function (_Emitter) {
131 inherits(AbortSignal, _Emitter);
132
133 function AbortSignal() {
134 classCallCheck(this, AbortSignal);
135
136 var _this2 = possibleConstructorReturn(this, (AbortSignal.__proto__ || Object.getPrototypeOf(AbortSignal)).call(this));
137
138 _this2.aborted = false;
139 _this2.onabort = null;
140 return _this2;
141 }
142
143 createClass(AbortSignal, [{
144 key: 'toString',
145 value: function toString() {
146 return '[object AbortSignal]';
147 }
148 }, {
149 key: 'dispatchEvent',
150 value: function dispatchEvent(event) {
151 if (event.type === 'abort') {
152 this.aborted = true;
153 if (typeof this.onabort === 'function') {
154 this.onabort.call(this, event);
155 }
156 }
157
158 get(AbortSignal.prototype.__proto__ || Object.getPrototypeOf(AbortSignal.prototype), 'dispatchEvent', this).call(this, event);
159 }
160 }]);
161 return AbortSignal;
162}(Emitter);
163
164var AbortController = function () {
165 function AbortController() {
166 classCallCheck(this, AbortController);
167
168 this.signal = new AbortSignal();
169 }
170
171 createClass(AbortController, [{
172 key: 'abort',
173 value: function abort() {
174 var event = void 0;
175 try {
176 event = new Event('abort');
177 } catch (e) {
178 if (typeof document !== 'undefined') {
179 if (!document.createEvent) {
180 // For Internet Explorer 8:
181 event = document.createEventObject();
182 event.type = 'abort';
183 } else {
184 // For Internet Explorer 11:
185 event = document.createEvent('Event');
186 event.initEvent('abort', false, false);
187 }
188 } else {
189 // Fallback where document isn't available:
190 event = {
191 type: 'abort',
192 bubbles: false,
193 cancelable: false
194 };
195 }
196 }
197 this.signal.dispatchEvent(event);
198 }
199 }, {
200 key: 'toString',
201 value: function toString() {
202 return '[object AbortController]';
203 }
204 }]);
205 return AbortController;
206}();
207
208if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
209 // These are necessary to make sure that we get correct output for:
210 // Object.prototype.toString.call(new AbortController())
211 AbortController.prototype[Symbol.toStringTag] = 'AbortController';
212 AbortSignal.prototype[Symbol.toStringTag] = 'AbortSignal';
213}
214
215exports.AbortSignal = AbortSignal;
216exports.AbortController = AbortController;
217exports.default = AbortController;