eventemitter3.esm.js 9.4 KB

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