"use strict";
/**
 * @author elliothu@tencent.com
 * @date 2019-05-07
 * @desc 事件系统，用于全局通信
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventProxy = void 0;
var tslib_1 = require("tslib");
var EventProxy = /** @class */ (function () {
    function EventProxy() {
        var _this = this;
        this.proxies = {};
        this.onceProxies = {};
        this.on = function (eventName, handler) {
            if (_this.proxies[eventName]) {
                return _this.proxies[eventName].push(handler);
            }
            _this.proxies[eventName] = [handler];
        };
        this.only = function (eventName, handler) {
            _this.proxies[eventName] = [handler];
        };
        this.once = function (eventName, handler) {
            if (_this.onceProxies[eventName]) {
                return _this.onceProxies[eventName].push(handler);
            }
            _this.onceProxies[eventName] = [handler];
        };
        this.emit = function (eventName) {
            var args = [];
            for (var _i = 1; _i < arguments.length; _i++) {
                args[_i - 1] = arguments[_i];
            }
            if (_this.proxies[eventName]) {
                _this.proxies[eventName].forEach(function (handler) { return handler.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(args), false)); });
            }
            if (_this.onceProxies[eventName]) {
                _this.onceProxies[eventName].forEach(function (handler) { return handler.apply(void 0, tslib_1.__spreadArray([], tslib_1.__read(args), false)); });
                _this.onceProxies[eventName] = [];
            }
        };
        this.cancel = function (eventName, handler) {
            if (!_this.proxies[eventName]) {
                return;
            }
            _this.proxies[eventName] = _this.proxies[eventName].filter(function (h) { return h !== handler; });
        };
        this.cancelOnce = function (eventName, handler) {
            if (!_this.onceProxies[eventName]) {
                return;
            }
            _this.onceProxies[eventName] = _this.onceProxies[eventName].filter(function (h) { return h !== handler; });
        };
        this.cancelAll = function (eventName) {
            if (!_this.proxies[eventName]) {
                return;
            }
            _this.proxies[eventName] = [];
        };
        this.cancelAllOnce = function (eventName) {
            if (!_this.onceProxies[eventName]) {
                return;
            }
            _this.onceProxies[eventName] = [];
        };
        this.clear = function () {
            _this.proxies = {};
            _this.onceProxies = {};
        };
    }
    return EventProxy;
}());
exports.EventProxy = EventProxy;
//# sourceMappingURL=eventProxy.js.map