Files
Monopoly/extensions/app/assets/manager/event/EventManager.ts
2026-03-30 09:39:59 +08:00

90 lines
2.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { EventTarget, _decorator } from 'cc';
import BaseManager from '../../base/BaseManager';
const { ccclass } = _decorator;
class Event {
static destroy(event: Event) {
if (!event) return;
event._event = null;
}
// 事件管理器
private _event: EventTarget = new EventTarget();
/**
* 事件分发
*/
public emit(event: string | number, ...data: any[]) {
if (!this._event) {
throw Error('当前event已销毁无法继续调用');
}
this._event.emit(event as any, ...data);
}
/**
* 事件监听
*/
public on(event: string | number, cb: (...any: any[]) => void, target?: any) {
if (!this._event) {
throw Error('当前event已销毁无法继续调用');
}
this._event.on(event as any, cb, target);
}
/**
* 事件监听
*/
public once(event: string | number, cb: (...any: any[]) => void, target?: any) {
if (!this._event) {
throw Error('当前event已销毁无法继续调用');
}
this._event.once(event as any, cb, target);
}
/**
* 事件移除监听
*/
public off(event: string | number, cb?: (...any: any[]) => void, target?: any) {
if (!this._event) {
throw Error('当前event已销毁无法继续调用');
}
this._event.off(event as any, cb, target);
}
/**
* 事件移除监听
*/
public targetOff(target: any) {
if (!this._event) {
throw Error('当前event已销毁无法继续调用');
}
this._event.targetOff(target);
}
}
@ccclass('EventManager')
export default class EventManager extends BaseManager {
private events: Map<string | number | Symbol, Event> = new Map();
clear() {
this.events.forEach(event => Event.destroy(event));
return this.events.clear();
}
delete(key: string | number | Symbol) {
Event.destroy(this.events.get(key));
return this.events.delete(key);
}
get(key: string | number | Symbol): Event {
if (this.events.has(key)) {
return this.events.get(key);
}
const event = new Event();
this.events.set(key, event);
return event;
}
}