// 发布订阅模式
var Observer = {
  events: {},

  /*
  events: {
  eventName1: [fn1, fn2],
  eventName2: [fn1, fn2]
  }
  */
  on: function on(eventName, fn) {
    if (!this.events[eventName]) {
      this.events[eventName] = [];
    }

    this.events[eventName].push(fn);
  },
  emit: function emit(eventName) {
    var fns = this.events[eventName];
    if (!Array.isArray(fns) || !fns.length) return false; // eslint-disable-next-line

    for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
      args[_key - 1] = arguments[_key];
    }

    for (var i = 0, fn; fn = fns[i++];) {
      var _fn2;

      (_fn2 = fn).apply.apply(_fn2, [this].concat(args));
    }
  },
  off: function off(eventName, fn) {
    var fns = this.events[eventName];
    if (!Array.isArray(fns) || !fns.length) return false;

    for (var i = 0; i < fns.length; i++) {
      var _fn = fns[i];

      if (_fn === fn) {
        fns.splice(i, 1);
      }
    }
  },
  destroy: function destroy(eventName) {
    if (!eventName) {
      this.events = {};
    }

    delete this.events[eventName];
  }
};
export default Observer;