class EventBus {
	constructor() {
		this.events = {}
	}

	// 注册事件监听
	on(eventName, callback) {
		if (!this.events[eventName]) {
			this.events[eventName] = []
		}
		this.events[eventName].push(callback)
	}

	// 移除事件监听
	off(eventName, callback) {
		if (this.events[eventName]) {
			this.events[eventName] = this.events[eventName].filter((cb) => cb !== callback)
		}
	}

	// 触发事件，并传入发送者标识
	emit(eventName, payload, sender) {
		if (this.events[eventName]) {
			this.events[eventName].forEach((callback) => callback(payload, sender))
		}
	}
}

// 创建全局实例
const eventBus = new EventBus()

export default eventBus
