eventemitter3.umd.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.EventEmitter3 = factory());
  5. })(this, (function () { 'use strict';
  6. var eventemitter3Exports = {};
  7. var eventemitter3$1 = {
  8. get exports(){ return eventemitter3Exports; },
  9. set exports(v){ eventemitter3Exports = v; },
  10. };
  11. (function (module) {
  12. var has = Object.prototype.hasOwnProperty
  13. , prefix = '~';
  14. /**
  15. * Constructor to create a storage for our `EE` objects.
  16. * An `Events` instance is a plain object whose properties are event names.
  17. *
  18. * @constructor
  19. * @private
  20. */
  21. function Events() {}
  22. //
  23. // We try to not inherit from `Object.prototype`. In some engines creating an
  24. // instance in this way is faster than calling `Object.create(null)` directly.
  25. // If `Object.create(null)` is not supported we prefix the event names with a
  26. // character to make sure that the built-in object properties are not
  27. // overridden or used as an attack vector.
  28. //
  29. if (Object.create) {
  30. Events.prototype = Object.create(null);
  31. //
  32. // This hack is needed because the `__proto__` property is still inherited in
  33. // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
  34. //
  35. if (!new Events().__proto__) prefix = false;
  36. }
  37. /**
  38. * Representation of a single event listener.
  39. *
  40. * @param {Function} fn The listener function.
  41. * @param {*} context The context to invoke the listener with.
  42. * @param {Boolean} [once=false] Specify if the listener is a one-time listener.
  43. * @constructor
  44. * @private
  45. */
  46. function EE(fn, context, once) {
  47. this.fn = fn;
  48. this.context = context;
  49. this.once = once || false;
  50. }
  51. /**
  52. * Add a listener for a given event.
  53. *
  54. * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
  55. * @param {(String|Symbol)} event The event name.
  56. * @param {Function} fn The listener function.
  57. * @param {*} context The context to invoke the listener with.
  58. * @param {Boolean} once Specify if the listener is a one-time listener.
  59. * @returns {EventEmitter}
  60. * @private
  61. */
  62. function addListener(emitter, event, fn, context, once) {
  63. if (typeof fn !== 'function') {
  64. throw new TypeError('The listener must be a function');
  65. }
  66. var listener = new EE(fn, context || emitter, once)
  67. , evt = prefix ? prefix + event : event;
  68. if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
  69. else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
  70. else emitter._events[evt] = [emitter._events[evt], listener];
  71. return emitter;
  72. }
  73. /**
  74. * Clear event by name.
  75. *
  76. * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
  77. * @param {(String|Symbol)} evt The Event name.
  78. * @private
  79. */
  80. function clearEvent(emitter, evt) {
  81. if (--emitter._eventsCount === 0) emitter._events = new Events();
  82. else delete emitter._events[evt];
  83. }
  84. /**
  85. * Minimal `EventEmitter` interface that is molded against the Node.js
  86. * `EventEmitter` interface.
  87. *
  88. * @constructor
  89. * @public
  90. */
  91. function EventEmitter() {
  92. this._events = new Events();
  93. this._eventsCount = 0;
  94. }
  95. /**
  96. * Return an array listing the events for which the emitter has registered
  97. * listeners.
  98. *
  99. * @returns {Array}
  100. * @public
  101. */
  102. EventEmitter.prototype.eventNames = function eventNames() {
  103. var names = []
  104. , events
  105. , name;
  106. if (this._eventsCount === 0) return names;
  107. for (name in (events = this._events)) {
  108. if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
  109. }
  110. if (Object.getOwnPropertySymbols) {
  111. return names.concat(Object.getOwnPropertySymbols(events));
  112. }
  113. return names;
  114. };
  115. /**
  116. * Return the listeners registered for a given event.
  117. *
  118. * @param {(String|Symbol)} event The event name.
  119. * @returns {Array} The registered listeners.
  120. * @public
  121. */
  122. EventEmitter.prototype.listeners = function listeners(event) {
  123. var evt = prefix ? prefix + event : event
  124. , handlers = this._events[evt];
  125. if (!handlers) return [];
  126. if (handlers.fn) return [handlers.fn];
  127. for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
  128. ee[i] = handlers[i].fn;
  129. }
  130. return ee;
  131. };
  132. /**
  133. * Return the number of listeners listening to a given event.
  134. *
  135. * @param {(String|Symbol)} event The event name.
  136. * @returns {Number} The number of listeners.
  137. * @public
  138. */
  139. EventEmitter.prototype.listenerCount = function listenerCount(event) {
  140. var evt = prefix ? prefix + event : event
  141. , listeners = this._events[evt];
  142. if (!listeners) return 0;
  143. if (listeners.fn) return 1;
  144. return listeners.length;
  145. };
  146. /**
  147. * Calls each of the listeners registered for a given event.
  148. *
  149. * @param {(String|Symbol)} event The event name.
  150. * @returns {Boolean} `true` if the event had listeners, else `false`.
  151. * @public
  152. */
  153. EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
  154. var evt = prefix ? prefix + event : event;
  155. if (!this._events[evt]) return false;
  156. var listeners = this._events[evt]
  157. , len = arguments.length
  158. , args
  159. , i;
  160. if (listeners.fn) {
  161. if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
  162. switch (len) {
  163. case 1: return listeners.fn.call(listeners.context), true;
  164. case 2: return listeners.fn.call(listeners.context, a1), true;
  165. case 3: return listeners.fn.call(listeners.context, a1, a2), true;
  166. case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
  167. case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
  168. case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
  169. }
  170. for (i = 1, args = new Array(len -1); i < len; i++) {
  171. args[i - 1] = arguments[i];
  172. }
  173. listeners.fn.apply(listeners.context, args);
  174. } else {
  175. var length = listeners.length
  176. , j;
  177. for (i = 0; i < length; i++) {
  178. if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
  179. switch (len) {
  180. case 1: listeners[i].fn.call(listeners[i].context); break;
  181. case 2: listeners[i].fn.call(listeners[i].context, a1); break;
  182. case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
  183. case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break;
  184. default:
  185. if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
  186. args[j - 1] = arguments[j];
  187. }
  188. listeners[i].fn.apply(listeners[i].context, args);
  189. }
  190. }
  191. }
  192. return true;
  193. };
  194. /**
  195. * Add a listener for a given event.
  196. *
  197. * @param {(String|Symbol)} event The event name.
  198. * @param {Function} fn The listener function.
  199. * @param {*} [context=this] The context to invoke the listener with.
  200. * @returns {EventEmitter} `this`.
  201. * @public
  202. */
  203. EventEmitter.prototype.on = function on(event, fn, context) {
  204. return addListener(this, event, fn, context, false);
  205. };
  206. /**
  207. * Add a one-time listener for a given event.
  208. *
  209. * @param {(String|Symbol)} event The event name.
  210. * @param {Function} fn The listener function.
  211. * @param {*} [context=this] The context to invoke the listener with.
  212. * @returns {EventEmitter} `this`.
  213. * @public
  214. */
  215. EventEmitter.prototype.once = function once(event, fn, context) {
  216. return addListener(this, event, fn, context, true);
  217. };
  218. /**
  219. * Remove the listeners of a given event.
  220. *
  221. * @param {(String|Symbol)} event The event name.
  222. * @param {Function} fn Only remove the listeners that match this function.
  223. * @param {*} context Only remove the listeners that have this context.
  224. * @param {Boolean} once Only remove one-time listeners.
  225. * @returns {EventEmitter} `this`.
  226. * @public
  227. */
  228. EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
  229. var evt = prefix ? prefix + event : event;
  230. if (!this._events[evt]) return this;
  231. if (!fn) {
  232. clearEvent(this, evt);
  233. return this;
  234. }
  235. var listeners = this._events[evt];
  236. if (listeners.fn) {
  237. if (
  238. listeners.fn === fn &&
  239. (!once || listeners.once) &&
  240. (!context || listeners.context === context)
  241. ) {
  242. clearEvent(this, evt);
  243. }
  244. } else {
  245. for (var i = 0, events = [], length = listeners.length; i < length; i++) {
  246. if (
  247. listeners[i].fn !== fn ||
  248. (once && !listeners[i].once) ||
  249. (context && listeners[i].context !== context)
  250. ) {
  251. events.push(listeners[i]);
  252. }
  253. }
  254. //
  255. // Reset the array, or remove it completely if we have no more listeners.
  256. //
  257. if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
  258. else clearEvent(this, evt);
  259. }
  260. return this;
  261. };
  262. /**
  263. * Remove all listeners, or those of the specified event.
  264. *
  265. * @param {(String|Symbol)} [event] The event name.
  266. * @returns {EventEmitter} `this`.
  267. * @public
  268. */
  269. EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
  270. var evt;
  271. if (event) {
  272. evt = prefix ? prefix + event : event;
  273. if (this._events[evt]) clearEvent(this, evt);
  274. } else {
  275. this._events = new Events();
  276. this._eventsCount = 0;
  277. }
  278. return this;
  279. };
  280. //
  281. // Alias methods names because people roll like that.
  282. //
  283. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  284. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  285. //
  286. // Expose the prefix.
  287. //
  288. EventEmitter.prefixed = prefix;
  289. //
  290. // Allow `EventEmitter` to be imported as module namespace.
  291. //
  292. EventEmitter.EventEmitter = EventEmitter;
  293. //
  294. // Expose the module.
  295. //
  296. {
  297. module.exports = EventEmitter;
  298. }
  299. } (eventemitter3$1));
  300. var eventemitter3 = eventemitter3Exports;
  301. return eventemitter3;
  302. }));