EventEmitter3.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. (function() {
  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. * @api 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 {Mixed} 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. * @api private
  36. */
  37. function EE(fn, context, once) {
  38. this.fn = fn;
  39. this.context = context;
  40. this.once = once || false;
  41. }
  42. /**
  43. * Minimal `EventEmitter` interface that is molded against the Node.js
  44. * `EventEmitter` interface.
  45. *
  46. * @constructor
  47. * @api public
  48. */
  49. function EventEmitter() {
  50. this._events = new Events();
  51. this._eventsCount = 0;
  52. }
  53. /**
  54. * Return an array listing the events for which the emitter has registered
  55. * listeners.
  56. *
  57. * @returns {Array}
  58. * @api public
  59. */
  60. EventEmitter.prototype.eventNames = function eventNames() {
  61. var names = [],
  62. events,
  63. name;
  64. if (this._eventsCount === 0) return names;
  65. for (name in (events = this._events)) {
  66. if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
  67. }
  68. if (Object.getOwnPropertySymbols) {
  69. return names.concat(Object.getOwnPropertySymbols(events));
  70. }
  71. return names;
  72. };
  73. /**
  74. * Return the listeners registered for a given event.
  75. *
  76. * @param {String|Symbol} event The event name.
  77. * @param {Boolean} exists Only check if there are listeners.
  78. * @returns {Array|Boolean}
  79. * @api public
  80. */
  81. EventEmitter.prototype.listeners = function listeners(event, exists) {
  82. var evt = prefix ? prefix + event : event,
  83. available = this._events[evt];
  84. if (exists) return !!available;
  85. if (!available) return [];
  86. if (available.fn) return [available.fn];
  87. for (var i = 0, l = available.length, ee = new Array(l); i < l; i++) {
  88. ee[i] = available[i].fn;
  89. }
  90. return ee;
  91. };
  92. /**
  93. * Calls each of the listeners registered for a given event.
  94. *
  95. * @param {String|Symbol} event The event name.
  96. * @returns {Boolean} `true` if the event had listeners, else `false`.
  97. * @api public
  98. */
  99. EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
  100. var evt = prefix ? prefix + event : event;
  101. if (!this._events[evt]) return false;
  102. var listeners = this._events[evt],
  103. len = arguments.length,
  104. args,
  105. i;
  106. if (listeners.fn) {
  107. if (listeners.once)
  108. this.removeListener(event, listeners.fn, undefined, true);
  109. switch (len) {
  110. case 1:
  111. return listeners.fn.call(listeners.context), true;
  112. case 2:
  113. return listeners.fn.call(listeners.context, a1), true;
  114. case 3:
  115. return listeners.fn.call(listeners.context, a1, a2), true;
  116. case 4:
  117. return listeners.fn.call(listeners.context, a1, a2, a3), true;
  118. case 5:
  119. return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
  120. case 6:
  121. return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
  122. }
  123. for (i = 1, args = new Array(len - 1); i < len; i++) {
  124. args[i - 1] = arguments[i];
  125. }
  126. listeners.fn.apply(listeners.context, args);
  127. } else {
  128. var length = listeners.length,
  129. j;
  130. for (i = 0; i < length; i++) {
  131. if (listeners[i].once)
  132. this.removeListener(event, listeners[i].fn, undefined, true);
  133. switch (len) {
  134. case 1:
  135. listeners[i].fn.call(listeners[i].context);
  136. break;
  137. case 2:
  138. listeners[i].fn.call(listeners[i].context, a1);
  139. break;
  140. case 3:
  141. listeners[i].fn.call(listeners[i].context, a1, a2);
  142. break;
  143. case 4:
  144. listeners[i].fn.call(listeners[i].context, a1, a2, a3);
  145. break;
  146. default:
  147. if (!args)
  148. for (j = 1, args = new Array(len - 1); j < len; j++) {
  149. args[j - 1] = arguments[j];
  150. }
  151. listeners[i].fn.apply(listeners[i].context, args);
  152. }
  153. }
  154. }
  155. return true;
  156. };
  157. /**
  158. * Add a listener for a given event.
  159. *
  160. * @param {String|Symbol} event The event name.
  161. * @param {Function} fn The listener function.
  162. * @param {Mixed} [context=this] The context to invoke the listener with.
  163. * @returns {EventEmitter} `this`.
  164. * @api public
  165. */
  166. EventEmitter.prototype.on = function on(event, fn, context) {
  167. var listener = new EE(fn, context || this),
  168. evt = prefix ? prefix + event : event;
  169. if (!this._events[evt]) (this._events[evt] = listener), this._eventsCount++;
  170. else if (!this._events[evt].fn) this._events[evt].push(listener);
  171. else this._events[evt] = [this._events[evt], listener];
  172. return this;
  173. };
  174. /**
  175. * Add a one-time listener for a given event.
  176. *
  177. * @param {String|Symbol} event The event name.
  178. * @param {Function} fn The listener function.
  179. * @param {Mixed} [context=this] The context to invoke the listener with.
  180. * @returns {EventEmitter} `this`.
  181. * @api public
  182. */
  183. EventEmitter.prototype.once = function once(event, fn, context) {
  184. var listener = new EE(fn, context || this, true),
  185. evt = prefix ? prefix + event : event;
  186. if (!this._events[evt]) (this._events[evt] = listener), this._eventsCount++;
  187. else if (!this._events[evt].fn) this._events[evt].push(listener);
  188. else this._events[evt] = [this._events[evt], listener];
  189. return this;
  190. };
  191. /**
  192. * Remove the listeners of a given event.
  193. *
  194. * @param {String|Symbol} event The event name.
  195. * @param {Function} fn Only remove the listeners that match this function.
  196. * @param {Mixed} context Only remove the listeners that have this context.
  197. * @param {Boolean} once Only remove one-time listeners.
  198. * @returns {EventEmitter} `this`.
  199. * @api public
  200. */
  201. EventEmitter.prototype.removeListener = function removeListener(
  202. event,
  203. fn,
  204. context,
  205. once
  206. ) {
  207. var evt = prefix ? prefix + event : event;
  208. if (!this._events[evt]) return this;
  209. if (!fn) {
  210. if (--this._eventsCount === 0) this._events = new Events();
  211. else delete this._events[evt];
  212. return this;
  213. }
  214. var listeners = this._events[evt];
  215. if (listeners.fn) {
  216. if (
  217. listeners.fn === fn &&
  218. (!once || listeners.once) &&
  219. (!context || listeners.context === context)
  220. ) {
  221. if (--this._eventsCount === 0) this._events = new Events();
  222. else delete this._events[evt];
  223. }
  224. } else {
  225. for (var i = 0, events = [], length = listeners.length; i < length; i++) {
  226. if (
  227. listeners[i].fn !== fn ||
  228. (once && !listeners[i].once) ||
  229. (context && listeners[i].context !== context)
  230. ) {
  231. events.push(listeners[i]);
  232. }
  233. }
  234. //
  235. // Reset the array, or remove it completely if we have no more listeners.
  236. //
  237. if (events.length)
  238. this._events[evt] = events.length === 1 ? events[0] : events;
  239. else if (--this._eventsCount === 0) this._events = new Events();
  240. else delete this._events[evt];
  241. }
  242. return this;
  243. };
  244. /**
  245. * Remove all listeners, or those of the specified event.
  246. *
  247. * @param {String|Symbol} [event] The event name.
  248. * @returns {EventEmitter} `this`.
  249. * @api public
  250. */
  251. EventEmitter.prototype.removeAllListeners = function removeAllListeners(
  252. event
  253. ) {
  254. var evt;
  255. if (event) {
  256. evt = prefix ? prefix + event : event;
  257. if (this._events[evt]) {
  258. if (--this._eventsCount === 0) this._events = new Events();
  259. else delete this._events[evt];
  260. }
  261. } else {
  262. this._events = new Events();
  263. this._eventsCount = 0;
  264. }
  265. return this;
  266. };
  267. //
  268. // Alias methods names because people roll like that.
  269. //
  270. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  271. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  272. //
  273. // This function doesn't apply anymore.
  274. //
  275. EventEmitter.prototype.setMaxListeners = function setMaxListeners() {
  276. return this;
  277. };
  278. //
  279. // Expose the prefix.
  280. //
  281. EventEmitter.prefixed = prefix;
  282. //
  283. // Allow `EventEmitter` to be imported as module namespace.
  284. //
  285. EventEmitter.EventEmitter = EventEmitter;
  286. //
  287. // Expose the module.
  288. //
  289. if ('undefined' !== typeof module) {
  290. module.exports = EventEmitter;
  291. }
  292. window.EventEmitter = EventEmitter;
  293. })();