utils-lib-js 2.0.4 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/umd/index.js CHANGED
@@ -1723,6 +1723,979 @@
1723
1723
 
1724
1724
  __assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1({}, object), base), array), __function), element), __static), event), storage), log), animate), { eventMessageCenter: MessageCenter, taskQueueLib: TaskQueue });
1725
1725
 
1726
+ /**
1727
+ * @author Toru Nagashima <https://github.com/mysticatea>
1728
+ * @copyright 2015 Toru Nagashima. All rights reserved.
1729
+ * See LICENSE file in root directory for full license.
1730
+ */
1731
+ /**
1732
+ * @typedef {object} PrivateData
1733
+ * @property {EventTarget} eventTarget The event target.
1734
+ * @property {{type:string}} event The original event object.
1735
+ * @property {number} eventPhase The current event phase.
1736
+ * @property {EventTarget|null} currentTarget The current event target.
1737
+ * @property {boolean} canceled The flag to prevent default.
1738
+ * @property {boolean} stopped The flag to stop propagation.
1739
+ * @property {boolean} immediateStopped The flag to stop propagation immediately.
1740
+ * @property {Function|null} passiveListener The listener if the current listener is passive. Otherwise this is null.
1741
+ * @property {number} timeStamp The unix time.
1742
+ * @private
1743
+ */
1744
+
1745
+ /**
1746
+ * Private data for event wrappers.
1747
+ * @type {WeakMap<Event, PrivateData>}
1748
+ * @private
1749
+ */
1750
+ const privateData = new WeakMap();
1751
+
1752
+ /**
1753
+ * Cache for wrapper classes.
1754
+ * @type {WeakMap<Object, Function>}
1755
+ * @private
1756
+ */
1757
+ const wrappers = new WeakMap();
1758
+
1759
+ /**
1760
+ * Get private data.
1761
+ * @param {Event} event The event object to get private data.
1762
+ * @returns {PrivateData} The private data of the event.
1763
+ * @private
1764
+ */
1765
+ function pd(event) {
1766
+ const retv = privateData.get(event);
1767
+ console.assert(
1768
+ retv != null,
1769
+ "'this' is expected an Event object, but got",
1770
+ event
1771
+ );
1772
+ return retv
1773
+ }
1774
+
1775
+ /**
1776
+ * https://dom.spec.whatwg.org/#set-the-canceled-flag
1777
+ * @param data {PrivateData} private data.
1778
+ */
1779
+ function setCancelFlag(data) {
1780
+ if (data.passiveListener != null) {
1781
+ if (
1782
+ typeof console !== "undefined" &&
1783
+ typeof console.error === "function"
1784
+ ) {
1785
+ console.error(
1786
+ "Unable to preventDefault inside passive event listener invocation.",
1787
+ data.passiveListener
1788
+ );
1789
+ }
1790
+ return
1791
+ }
1792
+ if (!data.event.cancelable) {
1793
+ return
1794
+ }
1795
+
1796
+ data.canceled = true;
1797
+ if (typeof data.event.preventDefault === "function") {
1798
+ data.event.preventDefault();
1799
+ }
1800
+ }
1801
+
1802
+ /**
1803
+ * @see https://dom.spec.whatwg.org/#interface-event
1804
+ * @private
1805
+ */
1806
+ /**
1807
+ * The event wrapper.
1808
+ * @constructor
1809
+ * @param {EventTarget} eventTarget The event target of this dispatching.
1810
+ * @param {Event|{type:string}} event The original event to wrap.
1811
+ */
1812
+ function Event$1(eventTarget, event) {
1813
+ privateData.set(this, {
1814
+ eventTarget,
1815
+ event,
1816
+ eventPhase: 2,
1817
+ currentTarget: eventTarget,
1818
+ canceled: false,
1819
+ stopped: false,
1820
+ immediateStopped: false,
1821
+ passiveListener: null,
1822
+ timeStamp: event.timeStamp || Date.now(),
1823
+ });
1824
+
1825
+ // https://heycam.github.io/webidl/#Unforgeable
1826
+ Object.defineProperty(this, "isTrusted", { value: false, enumerable: true });
1827
+
1828
+ // Define accessors
1829
+ const keys = Object.keys(event);
1830
+ for (let i = 0; i < keys.length; ++i) {
1831
+ const key = keys[i];
1832
+ if (!(key in this)) {
1833
+ Object.defineProperty(this, key, defineRedirectDescriptor(key));
1834
+ }
1835
+ }
1836
+ }
1837
+
1838
+ // Should be enumerable, but class methods are not enumerable.
1839
+ Event$1.prototype = {
1840
+ /**
1841
+ * The type of this event.
1842
+ * @type {string}
1843
+ */
1844
+ get type() {
1845
+ return pd(this).event.type
1846
+ },
1847
+
1848
+ /**
1849
+ * The target of this event.
1850
+ * @type {EventTarget}
1851
+ */
1852
+ get target() {
1853
+ return pd(this).eventTarget
1854
+ },
1855
+
1856
+ /**
1857
+ * The target of this event.
1858
+ * @type {EventTarget}
1859
+ */
1860
+ get currentTarget() {
1861
+ return pd(this).currentTarget
1862
+ },
1863
+
1864
+ /**
1865
+ * @returns {EventTarget[]} The composed path of this event.
1866
+ */
1867
+ composedPath() {
1868
+ const currentTarget = pd(this).currentTarget;
1869
+ if (currentTarget == null) {
1870
+ return []
1871
+ }
1872
+ return [currentTarget]
1873
+ },
1874
+
1875
+ /**
1876
+ * Constant of NONE.
1877
+ * @type {number}
1878
+ */
1879
+ get NONE() {
1880
+ return 0
1881
+ },
1882
+
1883
+ /**
1884
+ * Constant of CAPTURING_PHASE.
1885
+ * @type {number}
1886
+ */
1887
+ get CAPTURING_PHASE() {
1888
+ return 1
1889
+ },
1890
+
1891
+ /**
1892
+ * Constant of AT_TARGET.
1893
+ * @type {number}
1894
+ */
1895
+ get AT_TARGET() {
1896
+ return 2
1897
+ },
1898
+
1899
+ /**
1900
+ * Constant of BUBBLING_PHASE.
1901
+ * @type {number}
1902
+ */
1903
+ get BUBBLING_PHASE() {
1904
+ return 3
1905
+ },
1906
+
1907
+ /**
1908
+ * The target of this event.
1909
+ * @type {number}
1910
+ */
1911
+ get eventPhase() {
1912
+ return pd(this).eventPhase
1913
+ },
1914
+
1915
+ /**
1916
+ * Stop event bubbling.
1917
+ * @returns {void}
1918
+ */
1919
+ stopPropagation() {
1920
+ const data = pd(this);
1921
+
1922
+ data.stopped = true;
1923
+ if (typeof data.event.stopPropagation === "function") {
1924
+ data.event.stopPropagation();
1925
+ }
1926
+ },
1927
+
1928
+ /**
1929
+ * Stop event bubbling.
1930
+ * @returns {void}
1931
+ */
1932
+ stopImmediatePropagation() {
1933
+ const data = pd(this);
1934
+
1935
+ data.stopped = true;
1936
+ data.immediateStopped = true;
1937
+ if (typeof data.event.stopImmediatePropagation === "function") {
1938
+ data.event.stopImmediatePropagation();
1939
+ }
1940
+ },
1941
+
1942
+ /**
1943
+ * The flag to be bubbling.
1944
+ * @type {boolean}
1945
+ */
1946
+ get bubbles() {
1947
+ return Boolean(pd(this).event.bubbles)
1948
+ },
1949
+
1950
+ /**
1951
+ * The flag to be cancelable.
1952
+ * @type {boolean}
1953
+ */
1954
+ get cancelable() {
1955
+ return Boolean(pd(this).event.cancelable)
1956
+ },
1957
+
1958
+ /**
1959
+ * Cancel this event.
1960
+ * @returns {void}
1961
+ */
1962
+ preventDefault() {
1963
+ setCancelFlag(pd(this));
1964
+ },
1965
+
1966
+ /**
1967
+ * The flag to indicate cancellation state.
1968
+ * @type {boolean}
1969
+ */
1970
+ get defaultPrevented() {
1971
+ return pd(this).canceled
1972
+ },
1973
+
1974
+ /**
1975
+ * The flag to be composed.
1976
+ * @type {boolean}
1977
+ */
1978
+ get composed() {
1979
+ return Boolean(pd(this).event.composed)
1980
+ },
1981
+
1982
+ /**
1983
+ * The unix time of this event.
1984
+ * @type {number}
1985
+ */
1986
+ get timeStamp() {
1987
+ return pd(this).timeStamp
1988
+ },
1989
+
1990
+ /**
1991
+ * The target of this event.
1992
+ * @type {EventTarget}
1993
+ * @deprecated
1994
+ */
1995
+ get srcElement() {
1996
+ return pd(this).eventTarget
1997
+ },
1998
+
1999
+ /**
2000
+ * The flag to stop event bubbling.
2001
+ * @type {boolean}
2002
+ * @deprecated
2003
+ */
2004
+ get cancelBubble() {
2005
+ return pd(this).stopped
2006
+ },
2007
+ set cancelBubble(value) {
2008
+ if (!value) {
2009
+ return
2010
+ }
2011
+ const data = pd(this);
2012
+
2013
+ data.stopped = true;
2014
+ if (typeof data.event.cancelBubble === "boolean") {
2015
+ data.event.cancelBubble = true;
2016
+ }
2017
+ },
2018
+
2019
+ /**
2020
+ * The flag to indicate cancellation state.
2021
+ * @type {boolean}
2022
+ * @deprecated
2023
+ */
2024
+ get returnValue() {
2025
+ return !pd(this).canceled
2026
+ },
2027
+ set returnValue(value) {
2028
+ if (!value) {
2029
+ setCancelFlag(pd(this));
2030
+ }
2031
+ },
2032
+
2033
+ /**
2034
+ * Initialize this event object. But do nothing under event dispatching.
2035
+ * @param {string} type The event type.
2036
+ * @param {boolean} [bubbles=false] The flag to be possible to bubble up.
2037
+ * @param {boolean} [cancelable=false] The flag to be possible to cancel.
2038
+ * @deprecated
2039
+ */
2040
+ initEvent() {
2041
+ // Do nothing.
2042
+ },
2043
+ };
2044
+
2045
+ // `constructor` is not enumerable.
2046
+ Object.defineProperty(Event$1.prototype, "constructor", {
2047
+ value: Event$1,
2048
+ configurable: true,
2049
+ writable: true,
2050
+ });
2051
+
2052
+ // Ensure `event instanceof window.Event` is `true`.
2053
+ if (typeof window !== "undefined" && typeof window.Event !== "undefined") {
2054
+ Object.setPrototypeOf(Event$1.prototype, window.Event.prototype);
2055
+
2056
+ // Make association for wrappers.
2057
+ wrappers.set(window.Event.prototype, Event$1);
2058
+ }
2059
+
2060
+ /**
2061
+ * Get the property descriptor to redirect a given property.
2062
+ * @param {string} key Property name to define property descriptor.
2063
+ * @returns {PropertyDescriptor} The property descriptor to redirect the property.
2064
+ * @private
2065
+ */
2066
+ function defineRedirectDescriptor(key) {
2067
+ return {
2068
+ get() {
2069
+ return pd(this).event[key]
2070
+ },
2071
+ set(value) {
2072
+ pd(this).event[key] = value;
2073
+ },
2074
+ configurable: true,
2075
+ enumerable: true,
2076
+ }
2077
+ }
2078
+
2079
+ /**
2080
+ * Get the property descriptor to call a given method property.
2081
+ * @param {string} key Property name to define property descriptor.
2082
+ * @returns {PropertyDescriptor} The property descriptor to call the method property.
2083
+ * @private
2084
+ */
2085
+ function defineCallDescriptor(key) {
2086
+ return {
2087
+ value() {
2088
+ const event = pd(this).event;
2089
+ return event[key].apply(event, arguments)
2090
+ },
2091
+ configurable: true,
2092
+ enumerable: true,
2093
+ }
2094
+ }
2095
+
2096
+ /**
2097
+ * Define new wrapper class.
2098
+ * @param {Function} BaseEvent The base wrapper class.
2099
+ * @param {Object} proto The prototype of the original event.
2100
+ * @returns {Function} The defined wrapper class.
2101
+ * @private
2102
+ */
2103
+ function defineWrapper(BaseEvent, proto) {
2104
+ const keys = Object.keys(proto);
2105
+ if (keys.length === 0) {
2106
+ return BaseEvent
2107
+ }
2108
+
2109
+ /** CustomEvent */
2110
+ function CustomEvent(eventTarget, event) {
2111
+ BaseEvent.call(this, eventTarget, event);
2112
+ }
2113
+
2114
+ CustomEvent.prototype = Object.create(BaseEvent.prototype, {
2115
+ constructor: { value: CustomEvent, configurable: true, writable: true },
2116
+ });
2117
+
2118
+ // Define accessors.
2119
+ for (let i = 0; i < keys.length; ++i) {
2120
+ const key = keys[i];
2121
+ if (!(key in BaseEvent.prototype)) {
2122
+ const descriptor = Object.getOwnPropertyDescriptor(proto, key);
2123
+ const isFunc = typeof descriptor.value === "function";
2124
+ Object.defineProperty(
2125
+ CustomEvent.prototype,
2126
+ key,
2127
+ isFunc
2128
+ ? defineCallDescriptor(key)
2129
+ : defineRedirectDescriptor(key)
2130
+ );
2131
+ }
2132
+ }
2133
+
2134
+ return CustomEvent
2135
+ }
2136
+
2137
+ /**
2138
+ * Get the wrapper class of a given prototype.
2139
+ * @param {Object} proto The prototype of the original event to get its wrapper.
2140
+ * @returns {Function} The wrapper class.
2141
+ * @private
2142
+ */
2143
+ function getWrapper(proto) {
2144
+ if (proto == null || proto === Object.prototype) {
2145
+ return Event$1
2146
+ }
2147
+
2148
+ let wrapper = wrappers.get(proto);
2149
+ if (wrapper == null) {
2150
+ wrapper = defineWrapper(getWrapper(Object.getPrototypeOf(proto)), proto);
2151
+ wrappers.set(proto, wrapper);
2152
+ }
2153
+ return wrapper
2154
+ }
2155
+
2156
+ /**
2157
+ * Wrap a given event to management a dispatching.
2158
+ * @param {EventTarget} eventTarget The event target of this dispatching.
2159
+ * @param {Object} event The event to wrap.
2160
+ * @returns {Event} The wrapper instance.
2161
+ * @private
2162
+ */
2163
+ function wrapEvent(eventTarget, event) {
2164
+ const Wrapper = getWrapper(Object.getPrototypeOf(event));
2165
+ return new Wrapper(eventTarget, event)
2166
+ }
2167
+
2168
+ /**
2169
+ * Get the immediateStopped flag of a given event.
2170
+ * @param {Event} event The event to get.
2171
+ * @returns {boolean} The flag to stop propagation immediately.
2172
+ * @private
2173
+ */
2174
+ function isStopped(event) {
2175
+ return pd(event).immediateStopped
2176
+ }
2177
+
2178
+ /**
2179
+ * Set the current event phase of a given event.
2180
+ * @param {Event} event The event to set current target.
2181
+ * @param {number} eventPhase New event phase.
2182
+ * @returns {void}
2183
+ * @private
2184
+ */
2185
+ function setEventPhase(event, eventPhase) {
2186
+ pd(event).eventPhase = eventPhase;
2187
+ }
2188
+
2189
+ /**
2190
+ * Set the current target of a given event.
2191
+ * @param {Event} event The event to set current target.
2192
+ * @param {EventTarget|null} currentTarget New current target.
2193
+ * @returns {void}
2194
+ * @private
2195
+ */
2196
+ function setCurrentTarget(event, currentTarget) {
2197
+ pd(event).currentTarget = currentTarget;
2198
+ }
2199
+
2200
+ /**
2201
+ * Set a passive listener of a given event.
2202
+ * @param {Event} event The event to set current target.
2203
+ * @param {Function|null} passiveListener New passive listener.
2204
+ * @returns {void}
2205
+ * @private
2206
+ */
2207
+ function setPassiveListener(event, passiveListener) {
2208
+ pd(event).passiveListener = passiveListener;
2209
+ }
2210
+
2211
+ /**
2212
+ * @typedef {object} ListenerNode
2213
+ * @property {Function} listener
2214
+ * @property {1|2|3} listenerType
2215
+ * @property {boolean} passive
2216
+ * @property {boolean} once
2217
+ * @property {ListenerNode|null} next
2218
+ * @private
2219
+ */
2220
+
2221
+ /**
2222
+ * @type {WeakMap<object, Map<string, ListenerNode>>}
2223
+ * @private
2224
+ */
2225
+ const listenersMap = new WeakMap();
2226
+
2227
+ // Listener types
2228
+ const CAPTURE = 1;
2229
+ const BUBBLE = 2;
2230
+ const ATTRIBUTE = 3;
2231
+
2232
+ /**
2233
+ * Check whether a given value is an object or not.
2234
+ * @param {any} x The value to check.
2235
+ * @returns {boolean} `true` if the value is an object.
2236
+ */
2237
+ function isObject(x) {
2238
+ return x !== null && typeof x === "object" //eslint-disable-line no-restricted-syntax
2239
+ }
2240
+
2241
+ /**
2242
+ * Get listeners.
2243
+ * @param {EventTarget} eventTarget The event target to get.
2244
+ * @returns {Map<string, ListenerNode>} The listeners.
2245
+ * @private
2246
+ */
2247
+ function getListeners(eventTarget) {
2248
+ const listeners = listenersMap.get(eventTarget);
2249
+ if (listeners == null) {
2250
+ throw new TypeError(
2251
+ "'this' is expected an EventTarget object, but got another value."
2252
+ )
2253
+ }
2254
+ return listeners
2255
+ }
2256
+
2257
+ /**
2258
+ * Get the property descriptor for the event attribute of a given event.
2259
+ * @param {string} eventName The event name to get property descriptor.
2260
+ * @returns {PropertyDescriptor} The property descriptor.
2261
+ * @private
2262
+ */
2263
+ function defineEventAttributeDescriptor(eventName) {
2264
+ return {
2265
+ get() {
2266
+ const listeners = getListeners(this);
2267
+ let node = listeners.get(eventName);
2268
+ while (node != null) {
2269
+ if (node.listenerType === ATTRIBUTE) {
2270
+ return node.listener
2271
+ }
2272
+ node = node.next;
2273
+ }
2274
+ return null
2275
+ },
2276
+
2277
+ set(listener) {
2278
+ if (typeof listener !== "function" && !isObject(listener)) {
2279
+ listener = null; // eslint-disable-line no-param-reassign
2280
+ }
2281
+ const listeners = getListeners(this);
2282
+
2283
+ // Traverse to the tail while removing old value.
2284
+ let prev = null;
2285
+ let node = listeners.get(eventName);
2286
+ while (node != null) {
2287
+ if (node.listenerType === ATTRIBUTE) {
2288
+ // Remove old value.
2289
+ if (prev !== null) {
2290
+ prev.next = node.next;
2291
+ } else if (node.next !== null) {
2292
+ listeners.set(eventName, node.next);
2293
+ } else {
2294
+ listeners.delete(eventName);
2295
+ }
2296
+ } else {
2297
+ prev = node;
2298
+ }
2299
+
2300
+ node = node.next;
2301
+ }
2302
+
2303
+ // Add new value.
2304
+ if (listener !== null) {
2305
+ const newNode = {
2306
+ listener,
2307
+ listenerType: ATTRIBUTE,
2308
+ passive: false,
2309
+ once: false,
2310
+ next: null,
2311
+ };
2312
+ if (prev === null) {
2313
+ listeners.set(eventName, newNode);
2314
+ } else {
2315
+ prev.next = newNode;
2316
+ }
2317
+ }
2318
+ },
2319
+ configurable: true,
2320
+ enumerable: true,
2321
+ }
2322
+ }
2323
+
2324
+ /**
2325
+ * Define an event attribute (e.g. `eventTarget.onclick`).
2326
+ * @param {Object} eventTargetPrototype The event target prototype to define an event attrbite.
2327
+ * @param {string} eventName The event name to define.
2328
+ * @returns {void}
2329
+ */
2330
+ function defineEventAttribute(eventTargetPrototype, eventName) {
2331
+ Object.defineProperty(
2332
+ eventTargetPrototype,
2333
+ `on${eventName}`,
2334
+ defineEventAttributeDescriptor(eventName)
2335
+ );
2336
+ }
2337
+
2338
+ /**
2339
+ * Define a custom EventTarget with event attributes.
2340
+ * @param {string[]} eventNames Event names for event attributes.
2341
+ * @returns {EventTarget} The custom EventTarget.
2342
+ * @private
2343
+ */
2344
+ function defineCustomEventTarget(eventNames) {
2345
+ /** CustomEventTarget */
2346
+ function CustomEventTarget() {
2347
+ EventTarget.call(this);
2348
+ }
2349
+
2350
+ CustomEventTarget.prototype = Object.create(EventTarget.prototype, {
2351
+ constructor: {
2352
+ value: CustomEventTarget,
2353
+ configurable: true,
2354
+ writable: true,
2355
+ },
2356
+ });
2357
+
2358
+ for (let i = 0; i < eventNames.length; ++i) {
2359
+ defineEventAttribute(CustomEventTarget.prototype, eventNames[i]);
2360
+ }
2361
+
2362
+ return CustomEventTarget
2363
+ }
2364
+
2365
+ /**
2366
+ * EventTarget.
2367
+ *
2368
+ * - This is constructor if no arguments.
2369
+ * - This is a function which returns a CustomEventTarget constructor if there are arguments.
2370
+ *
2371
+ * For example:
2372
+ *
2373
+ * class A extends EventTarget {}
2374
+ * class B extends EventTarget("message") {}
2375
+ * class C extends EventTarget("message", "error") {}
2376
+ * class D extends EventTarget(["message", "error"]) {}
2377
+ */
2378
+ function EventTarget() {
2379
+ /*eslint-disable consistent-return */
2380
+ if (this instanceof EventTarget) {
2381
+ listenersMap.set(this, new Map());
2382
+ return
2383
+ }
2384
+ if (arguments.length === 1 && Array.isArray(arguments[0])) {
2385
+ return defineCustomEventTarget(arguments[0])
2386
+ }
2387
+ if (arguments.length > 0) {
2388
+ const types = new Array(arguments.length);
2389
+ for (let i = 0; i < arguments.length; ++i) {
2390
+ types[i] = arguments[i];
2391
+ }
2392
+ return defineCustomEventTarget(types)
2393
+ }
2394
+ throw new TypeError("Cannot call a class as a function")
2395
+ /*eslint-enable consistent-return */
2396
+ }
2397
+
2398
+ // Should be enumerable, but class methods are not enumerable.
2399
+ EventTarget.prototype = {
2400
+ /**
2401
+ * Add a given listener to this event target.
2402
+ * @param {string} eventName The event name to add.
2403
+ * @param {Function} listener The listener to add.
2404
+ * @param {boolean|{capture?:boolean,passive?:boolean,once?:boolean}} [options] The options for this listener.
2405
+ * @returns {void}
2406
+ */
2407
+ addEventListener(eventName, listener, options) {
2408
+ if (listener == null) {
2409
+ return
2410
+ }
2411
+ if (typeof listener !== "function" && !isObject(listener)) {
2412
+ throw new TypeError("'listener' should be a function or an object.")
2413
+ }
2414
+
2415
+ const listeners = getListeners(this);
2416
+ const optionsIsObj = isObject(options);
2417
+ const capture = optionsIsObj
2418
+ ? Boolean(options.capture)
2419
+ : Boolean(options);
2420
+ const listenerType = capture ? CAPTURE : BUBBLE;
2421
+ const newNode = {
2422
+ listener,
2423
+ listenerType,
2424
+ passive: optionsIsObj && Boolean(options.passive),
2425
+ once: optionsIsObj && Boolean(options.once),
2426
+ next: null,
2427
+ };
2428
+
2429
+ // Set it as the first node if the first node is null.
2430
+ let node = listeners.get(eventName);
2431
+ if (node === undefined) {
2432
+ listeners.set(eventName, newNode);
2433
+ return
2434
+ }
2435
+
2436
+ // Traverse to the tail while checking duplication..
2437
+ let prev = null;
2438
+ while (node != null) {
2439
+ if (
2440
+ node.listener === listener &&
2441
+ node.listenerType === listenerType
2442
+ ) {
2443
+ // Should ignore duplication.
2444
+ return
2445
+ }
2446
+ prev = node;
2447
+ node = node.next;
2448
+ }
2449
+
2450
+ // Add it.
2451
+ prev.next = newNode;
2452
+ },
2453
+
2454
+ /**
2455
+ * Remove a given listener from this event target.
2456
+ * @param {string} eventName The event name to remove.
2457
+ * @param {Function} listener The listener to remove.
2458
+ * @param {boolean|{capture?:boolean,passive?:boolean,once?:boolean}} [options] The options for this listener.
2459
+ * @returns {void}
2460
+ */
2461
+ removeEventListener(eventName, listener, options) {
2462
+ if (listener == null) {
2463
+ return
2464
+ }
2465
+
2466
+ const listeners = getListeners(this);
2467
+ const capture = isObject(options)
2468
+ ? Boolean(options.capture)
2469
+ : Boolean(options);
2470
+ const listenerType = capture ? CAPTURE : BUBBLE;
2471
+
2472
+ let prev = null;
2473
+ let node = listeners.get(eventName);
2474
+ while (node != null) {
2475
+ if (
2476
+ node.listener === listener &&
2477
+ node.listenerType === listenerType
2478
+ ) {
2479
+ if (prev !== null) {
2480
+ prev.next = node.next;
2481
+ } else if (node.next !== null) {
2482
+ listeners.set(eventName, node.next);
2483
+ } else {
2484
+ listeners.delete(eventName);
2485
+ }
2486
+ return
2487
+ }
2488
+
2489
+ prev = node;
2490
+ node = node.next;
2491
+ }
2492
+ },
2493
+
2494
+ /**
2495
+ * Dispatch a given event.
2496
+ * @param {Event|{type:string}} event The event to dispatch.
2497
+ * @returns {boolean} `false` if canceled.
2498
+ */
2499
+ dispatchEvent(event) {
2500
+ if (event == null || typeof event.type !== "string") {
2501
+ throw new TypeError('"event.type" should be a string.')
2502
+ }
2503
+
2504
+ // If listeners aren't registered, terminate.
2505
+ const listeners = getListeners(this);
2506
+ const eventName = event.type;
2507
+ let node = listeners.get(eventName);
2508
+ if (node == null) {
2509
+ return true
2510
+ }
2511
+
2512
+ // Since we cannot rewrite several properties, so wrap object.
2513
+ const wrappedEvent = wrapEvent(this, event);
2514
+
2515
+ // This doesn't process capturing phase and bubbling phase.
2516
+ // This isn't participating in a tree.
2517
+ let prev = null;
2518
+ while (node != null) {
2519
+ // Remove this listener if it's once
2520
+ if (node.once) {
2521
+ if (prev !== null) {
2522
+ prev.next = node.next;
2523
+ } else if (node.next !== null) {
2524
+ listeners.set(eventName, node.next);
2525
+ } else {
2526
+ listeners.delete(eventName);
2527
+ }
2528
+ } else {
2529
+ prev = node;
2530
+ }
2531
+
2532
+ // Call this listener
2533
+ setPassiveListener(
2534
+ wrappedEvent,
2535
+ node.passive ? node.listener : null
2536
+ );
2537
+ if (typeof node.listener === "function") {
2538
+ try {
2539
+ node.listener.call(this, wrappedEvent);
2540
+ } catch (err) {
2541
+ if (
2542
+ typeof console !== "undefined" &&
2543
+ typeof console.error === "function"
2544
+ ) {
2545
+ console.error(err);
2546
+ }
2547
+ }
2548
+ } else if (
2549
+ node.listenerType !== ATTRIBUTE &&
2550
+ typeof node.listener.handleEvent === "function"
2551
+ ) {
2552
+ node.listener.handleEvent(wrappedEvent);
2553
+ }
2554
+
2555
+ // Break if `event.stopImmediatePropagation` was called.
2556
+ if (isStopped(wrappedEvent)) {
2557
+ break
2558
+ }
2559
+
2560
+ node = node.next;
2561
+ }
2562
+ setPassiveListener(wrappedEvent, null);
2563
+ setEventPhase(wrappedEvent, 0);
2564
+ setCurrentTarget(wrappedEvent, null);
2565
+
2566
+ return !wrappedEvent.defaultPrevented
2567
+ },
2568
+ };
2569
+
2570
+ // `constructor` is not enumerable.
2571
+ Object.defineProperty(EventTarget.prototype, "constructor", {
2572
+ value: EventTarget,
2573
+ configurable: true,
2574
+ writable: true,
2575
+ });
2576
+
2577
+ // Ensure `eventTarget instanceof window.EventTarget` is `true`.
2578
+ if (
2579
+ typeof window !== "undefined" &&
2580
+ typeof window.EventTarget !== "undefined"
2581
+ ) {
2582
+ Object.setPrototypeOf(EventTarget.prototype, window.EventTarget.prototype);
2583
+ }
2584
+
2585
+ /**
2586
+ * @author Toru Nagashima <https://github.com/mysticatea>
2587
+ * See LICENSE file in root directory for full license.
2588
+ */
2589
+
2590
+ /**
2591
+ * The signal class.
2592
+ * @see https://dom.spec.whatwg.org/#abortsignal
2593
+ */
2594
+ class AbortSignal extends EventTarget {
2595
+ /**
2596
+ * AbortSignal cannot be constructed directly.
2597
+ */
2598
+ constructor() {
2599
+ super();
2600
+ throw new TypeError("AbortSignal cannot be constructed directly");
2601
+ }
2602
+ /**
2603
+ * Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
2604
+ */
2605
+ get aborted() {
2606
+ const aborted = abortedFlags.get(this);
2607
+ if (typeof aborted !== "boolean") {
2608
+ throw new TypeError(`Expected 'this' to be an 'AbortSignal' object, but got ${this === null ? "null" : typeof this}`);
2609
+ }
2610
+ return aborted;
2611
+ }
2612
+ }
2613
+ defineEventAttribute(AbortSignal.prototype, "abort");
2614
+ /**
2615
+ * Create an AbortSignal object.
2616
+ */
2617
+ function createAbortSignal() {
2618
+ const signal = Object.create(AbortSignal.prototype);
2619
+ EventTarget.call(signal);
2620
+ abortedFlags.set(signal, false);
2621
+ return signal;
2622
+ }
2623
+ /**
2624
+ * Abort a given signal.
2625
+ */
2626
+ function abortSignal(signal) {
2627
+ if (abortedFlags.get(signal) !== false) {
2628
+ return;
2629
+ }
2630
+ abortedFlags.set(signal, true);
2631
+ signal.dispatchEvent({ type: "abort" });
2632
+ }
2633
+ /**
2634
+ * Aborted flag for each instances.
2635
+ */
2636
+ const abortedFlags = new WeakMap();
2637
+ // Properties should be enumerable.
2638
+ Object.defineProperties(AbortSignal.prototype, {
2639
+ aborted: { enumerable: true },
2640
+ });
2641
+ // `toString()` should return `"[object AbortSignal]"`
2642
+ if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") {
2643
+ Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, {
2644
+ configurable: true,
2645
+ value: "AbortSignal",
2646
+ });
2647
+ }
2648
+
2649
+ /**
2650
+ * The AbortController.
2651
+ * @see https://dom.spec.whatwg.org/#abortcontroller
2652
+ */
2653
+ let AbortController$1 = class AbortController {
2654
+ /**
2655
+ * Initialize this controller.
2656
+ */
2657
+ constructor() {
2658
+ signals.set(this, createAbortSignal());
2659
+ }
2660
+ /**
2661
+ * Returns the `AbortSignal` object associated with this object.
2662
+ */
2663
+ get signal() {
2664
+ return getSignal(this);
2665
+ }
2666
+ /**
2667
+ * Abort and signal to any observers that the associated activity is to be aborted.
2668
+ */
2669
+ abort() {
2670
+ abortSignal(getSignal(this));
2671
+ }
2672
+ };
2673
+ /**
2674
+ * Associated signals.
2675
+ */
2676
+ const signals = new WeakMap();
2677
+ /**
2678
+ * Get the associated signal of a given controller.
2679
+ */
2680
+ function getSignal(controller) {
2681
+ const signal = signals.get(controller);
2682
+ if (signal == null) {
2683
+ throw new TypeError(`Expected 'this' to be an 'AbortController' object, but got ${controller === null ? "null" : typeof controller}`);
2684
+ }
2685
+ return signal;
2686
+ }
2687
+ // Properties should be enumerable.
2688
+ Object.defineProperties(AbortController$1.prototype, {
2689
+ signal: { enumerable: true },
2690
+ abort: { enumerable: true },
2691
+ });
2692
+ if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") {
2693
+ Object.defineProperty(AbortController$1.prototype, Symbol.toStringTag, {
2694
+ configurable: true,
2695
+ value: "AbortController",
2696
+ });
2697
+ }
2698
+
1726
2699
  /******************************************************************************
1727
2700
  Copyright (c) Microsoft Corporation.
1728
2701
 
@@ -1786,20 +2759,21 @@
1786
2759
  var httpRequest;
1787
2760
  var httpsRequest;
1788
2761
  var parse;
1789
- if (typeof window !== 'undefined') {
1790
- CustomAbortController = AbortController;
2762
+ var hasAbort = typeof AbortController !== "undefined";
2763
+ if (typeof window !== 'undefined' || hasAbort) {
2764
+ CustomAbortController = globalThis.AbortController;
1791
2765
  }
1792
2766
  else if (typeof require !== "undefined") {
1793
- CustomAbortController = require("abort-controller");
1794
2767
  httpRequest = require("http").request;
1795
2768
  httpsRequest = require("https").request;
1796
2769
  parse = require("url").parse;
2770
+ CustomAbortController = require('abort-controller');
1797
2771
  }
1798
2772
  else if (typeof globalThis === "object") {
1799
2773
  httpRequest = http.request;
1800
2774
  httpsRequest = https.request;
1801
2775
  parse = url.parse;
1802
- CustomAbortController = globalThis.AbortController;
2776
+ CustomAbortController = AbortController$1;
1803
2777
  }
1804
2778
  else {
1805
2779
  CustomAbortController = function () {