"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
// 发布订阅模式
var EventEmitter = /*#__PURE__*/function () {
  function EventEmitter() {
    _classCallCheck(this, EventEmitter);
    // 事件对象，存放订阅的名字和事件 如：{'click': [handle, handle2]}
    this.events = {};
  }

  // 订阅事件方法
  _createClass(EventEmitter, [{
    key: "on",
    value: function on(eventName, callback) {
      this.events[eventName] = this.events[eventName] || [];
      this.events[eventName].push(callback);
    }

    // 触发事件方法
  }, {
    key: "emit",
    value: function emit(eventName) {
      var _this = this;
      for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
        rest[_key - 1] = arguments[_key];
      }
      if (this.events[eventName]) {
        this.events[eventName].forEach(function (f) {
          return f.apply(_this, rest);
        });
      }
    }

    // 移除订阅事件
  }, {
    key: "un",
    value: function un(eventName, callback) {
      if (this.events[eventName]) {
        this.events[eventName] = this.events[eventName].filter(function (f) {
          return f !== callback;
        });
      }
    }

    // 只执行一次订阅的事件
  }, {
    key: "once",
    value: function once(eventName, callback) {
      var _this2 = this;
      //创建临时方法
      var fn = function fn() {
        for (var _len2 = arguments.length, rest = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
          rest[_key2] = arguments[_key2];
        }
        // 执行原有的callback
        callback.apply(_this2, rest);

        // 执行移除事件，删除临时方法
        _this2.un(eventName, fn);
      };

      // 执行订阅事件，绑定临时方法
      this.on(eventName, fn);
    }
  }]);
  return EventEmitter;
}();
var _default = exports["default"] = EventEmitter;
//# sourceMappingURL=EventEmitter.js.map
