You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
8.9 KiB

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