Няма описание
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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. module.exports = EventEmitter;
  3. /**
  4. * Constructs a new event emitter instance.
  5. * @classdesc A minimal event emitter.
  6. * @memberof util
  7. * @constructor
  8. */
  9. function EventEmitter() {
  10. /**
  11. * Registered listeners.
  12. * @type {Object.<string,*>}
  13. * @private
  14. */
  15. this._listeners = {};
  16. }
  17. /**
  18. * Registers an event listener.
  19. * @param {string} evt Event name
  20. * @param {function} fn Listener
  21. * @param {*} [ctx] Listener context
  22. * @returns {util.EventEmitter} `this`
  23. */
  24. EventEmitter.prototype.on = function on(evt, fn, ctx) {
  25. (this._listeners[evt] || (this._listeners[evt] = [])).push({
  26. fn : fn,
  27. ctx : ctx || this
  28. });
  29. return this;
  30. };
  31. /**
  32. * Removes an event listener or any matching listeners if arguments are omitted.
  33. * @param {string} [evt] Event name. Removes all listeners if omitted.
  34. * @param {function} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
  35. * @returns {util.EventEmitter} `this`
  36. */
  37. EventEmitter.prototype.off = function off(evt, fn) {
  38. if (evt === undefined)
  39. this._listeners = {};
  40. else {
  41. if (fn === undefined)
  42. this._listeners[evt] = [];
  43. else {
  44. var listeners = this._listeners[evt];
  45. for (var i = 0; i < listeners.length;)
  46. if (listeners[i].fn === fn)
  47. listeners.splice(i, 1);
  48. else
  49. ++i;
  50. }
  51. }
  52. return this;
  53. };
  54. /**
  55. * Emits an event by calling its listeners with the specified arguments.
  56. * @param {string} evt Event name
  57. * @param {...*} args Arguments
  58. * @returns {util.EventEmitter} `this`
  59. */
  60. EventEmitter.prototype.emit = function emit(evt) {
  61. var listeners = this._listeners[evt];
  62. if (listeners) {
  63. var args = [],
  64. i = 1;
  65. for (; i < arguments.length;)
  66. args.push(arguments[i++]);
  67. for (i = 0; i < listeners.length;)
  68. listeners[i].fn.apply(listeners[i++].ctx, args);
  69. }
  70. return this;
  71. };