/**
 * @author elliothu@tencent.com
 * @date 2019-05-07
 * @desc 事件系统，用于全局通信
 */
export class EventProxy {
    constructor() {
        this.proxies = {};
        this.onceProxies = {};
        this.on = (eventName, handler) => {
            if (this.proxies[eventName]) {
                return this.proxies[eventName].push(handler);
            }
            this.proxies[eventName] = [handler];
        };
        this.only = (eventName, handler) => {
            this.proxies[eventName] = [handler];
        };
        this.once = (eventName, handler) => {
            if (this.onceProxies[eventName]) {
                return this.onceProxies[eventName].push(handler);
            }
            this.onceProxies[eventName] = [handler];
        };
        this.emit = (eventName, ...args) => {
            if (this.proxies[eventName]) {
                this.proxies[eventName].forEach((handler) => handler(...args));
            }
            if (this.onceProxies[eventName]) {
                this.onceProxies[eventName].forEach((handler) => handler(...args));
                this.onceProxies[eventName] = [];
            }
        };
        this.cancel = (eventName, handler) => {
            if (!this.proxies[eventName]) {
                return;
            }
            this.proxies[eventName] = this.proxies[eventName].filter((h) => h !== handler);
        };
        this.cancelOnce = (eventName, handler) => {
            if (!this.onceProxies[eventName]) {
                return;
            }
            this.onceProxies[eventName] = this.onceProxies[eventName].filter((h) => h !== handler);
        };
        this.cancelAll = (eventName) => {
            if (!this.proxies[eventName]) {
                return;
            }
            this.proxies[eventName] = [];
        };
        this.cancelAllOnce = (eventName) => {
            if (!this.onceProxies[eventName]) {
                return;
            }
            this.onceProxies[eventName] = [];
        };
        this.clear = () => {
            this.proxies = {};
            this.onceProxies = {};
        };
    }
}
