eventemitter3.js 9.8 KB

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