"use strict";

const DiscordieError = require("./DiscordieError");
const Constants = require("../Constants");
const EventTypes = Object.keys(Constants.Events);

const events = require("events");

let lastEvent = null;

function validateEvent(eventType) {
  if (EventTypes.indexOf(eventType) < 0)
    throw new DiscordieError(`Invalid event '${eventType}'`);
}

class DiscordieDispatcher extends events.EventEmitter {
  constructor() {
    super();
    this._anyeventlisteners = [];
  }
  static _getLastEvent() { return lastEvent; }
  on(eventType, listener) {
    validateEvent(eventType);
    return super.on.apply(this, arguments);
  }
  onAny(fn) {
    if (typeof fn !== "function")
      return this;
    this._anyeventlisteners.push(fn);
    return this;
  }
  emit(eventType) {
    validateEvent(eventType);

    lastEvent = [].slice.call(arguments);
    super.emit.apply(this, arguments);
    const _arguments = arguments;
    this._anyeventlisteners.forEach((fn) => {
      fn.apply(null, _arguments);
    });
  }
}

module.exports = DiscordieDispatcher;

"use strict";

const DiscordieError = require("./DiscordieError");
const Constants = require("../Constants");
const EventTypes = Object.keys(Constants.Events);

const events = require("events");

let lastEvent = null;

function validateEvent(eventType) {
  if (EventTypes.indexOf(eventType) < 0)
    throw new DiscordieError(`Invalid event '${eventType}'`);
}

class DiscordieDispatcher extends events.EventEmitter {
  constructor() {
    super();
    this._anyeventlisteners = [];
  }
  static _getLastEvent() { return lastEvent; }
  on(eventType, listener) {
    validateEvent(eventType);
    return super.on.apply(this, arguments);
  }
  onAny(fn) {
    if (typeof fn !== "function")
      return this;
    this._anyeventlisteners.push(fn);
    return this;
  }
  emit(eventType) {
    validateEvent(eventType);

    lastEvent = [].slice.call(arguments);
    super.emit.apply(this, arguments);
    const _arguments = arguments;
    this._anyeventlisteners.forEach((fn) => {
      fn.apply(this, _arguments);
    });
  }
}

module.exports = DiscordieDispatcher;

Call onAny listeners in dispatcher context
Call onAny listeners in dispatcher context
