EventEmitter

function EventEmitter() {
  this._events = {};
}

EventEmitter.prototype.on = function (eventName, cb) {
  if (!this._events) {
    this._events = {};
  }
  if (eventName !== "newListener") {
    this._events["newListener"] ? this._events["newListener"].forEach(fn=>fn(eventName)) : void 0;
  }
  if (this._events[eventName]) {
    this._events[eventName].push(cb);
  } else {
    this._events[eventName] = [cb];
  }
};

EventEmitter.prototype.once =  function(eventName,cb){
    let onceFn = function () {
        cb.call(this, ...arguments);
        this.off(eventName, onceFn);
    };
    onceFn.fn = cb;
    this.on(eventName, onceFn);
}

EventEmitter.prototype.off = function (eventName, cb) {
    if (this._events[eventName]) {
        this._events[eventName] = this._events[eventName].filter(
          (fn) => fn !== cb && cb.fn === cb
        );
  }
};

EventEmitter.prototype.emit = function (eventName, ...args) {
  if (this._events[eventName]) {
    this._events[eventName].forEach((fn) => {
      fn.call(this, ...args);
    });
  }
};

module.exports = EventEmitter;