移除eventhandler装饰器

This commit is contained in:
YHH
2025-09-29 09:35:02 +08:00
parent 2947ddeb64
commit 61fcd52c65
9 changed files with 7 additions and 1192 deletions

View File

@@ -467,116 +467,4 @@ export class GlobalEventBus {
}
}
/**
* 事件装饰器工厂
* 用于自动注册事件监听器,支持自动清理
*/
export function EventHandler(eventType: string, config: IEventListenerConfig = {}) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
// 存储装饰器信息
if (!target.constructor._eventHandlers) {
target.constructor._eventHandlers = [];
}
target.constructor._eventHandlers.push({
eventType,
methodName: propertyKey,
config
});
// 在类实例化时自动注册监听器
const initMethod = target.constructor.prototype.initEventListeners || function() {};
target.constructor.prototype.initEventListeners = function() {
initMethod.call(this);
// 初始化监听器追踪数组
if (!this._decoratorEventListeners) {
this._decoratorEventListeners = [];
}
const eventBus = GlobalEventBus.getInstance();
const listenerId = eventBus.on(eventType, originalMethod.bind(this), config);
// 保存监听器ID用于后续清理
this._decoratorEventListeners.push({
eventType,
methodName: propertyKey,
listenerId
});
};
// 添加清理方法
const cleanupMethod = target.constructor.prototype.cleanupEventListeners || function() {};
target.constructor.prototype.cleanupEventListeners = function() {
cleanupMethod.call(this);
if (this._decoratorEventListeners) {
const eventBus = GlobalEventBus.getInstance();
for (const listener of this._decoratorEventListeners) {
eventBus.off(listener.eventType, listener.listenerId);
}
this._decoratorEventListeners.length = 0;
}
};
return descriptor;
};
}
/**
* 异步事件装饰器工厂
* 用于自动注册异步事件监听器,支持自动清理
*/
export function AsyncEventHandler(eventType: string, config: IEventListenerConfig = {}) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
// 存储装饰器信息
if (!target.constructor._eventHandlers) {
target.constructor._eventHandlers = [];
}
target.constructor._eventHandlers.push({
eventType,
methodName: propertyKey,
config,
async: true
});
const initMethod = target.constructor.prototype.initEventListeners || function() {};
target.constructor.prototype.initEventListeners = function() {
initMethod.call(this);
// 初始化监听器追踪数组
if (!this._decoratorEventListeners) {
this._decoratorEventListeners = [];
}
const eventBus = GlobalEventBus.getInstance();
const listenerId = eventBus.onAsync(eventType, originalMethod.bind(this), config);
// 保存监听器ID用于后续清理
this._decoratorEventListeners.push({
eventType,
methodName: propertyKey,
listenerId
});
};
// 添加清理方法
const cleanupMethod = target.constructor.prototype.cleanupEventListeners || function() {};
target.constructor.prototype.cleanupEventListeners = function() {
cleanupMethod.call(this);
if (this._decoratorEventListeners) {
const eventBus = GlobalEventBus.getInstance();
for (const listener of this._decoratorEventListeners) {
eventBus.off(listener.eventType, listener.listenerId);
}
this._decoratorEventListeners.length = 0;
}
};
return descriptor;
};
}

View File

@@ -578,37 +578,4 @@ export class TypeSafeEventSystem {
*/
export const GlobalEventSystem = new TypeSafeEventSystem();
/**
* 事件装饰器 - 用于自动注册事件监听器
* @param eventType 事件类型
* @param config 监听器配置
*/
export function EventListener(eventType: string, config: EventListenerConfig = {}) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
// 在类实例化时自动注册监听器
const initMethod = target.constructor.prototype.initEventListeners || function () { };
target.constructor.prototype.initEventListeners = function () {
initMethod.call(this);
GlobalEventSystem.on(eventType, originalMethod.bind(this), config);
};
};
}
/**
* 异步事件装饰器
* @param eventType 事件类型
* @param config 监听器配置
*/
export function AsyncEventListener(eventType: string, config: EventListenerConfig = {}) {
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value;
const initMethod = target.constructor.prototype.initEventListeners || function () { };
target.constructor.prototype.initEventListeners = function () {
initMethod.call(this);
GlobalEventSystem.onAsync(eventType, originalMethod.bind(this), config);
};
};
}

View File

@@ -1,2 +1,2 @@
export { EventBus, GlobalEventBus, EventHandler, AsyncEventHandler } from '../EventBus';
export { EventBus, GlobalEventBus } from '../EventBus';
export { TypeSafeEventSystem, EventListenerConfig, EventStats } from '../EventSystem';

View File

@@ -53,9 +53,6 @@ export abstract class EntitySystem implements ISystemBase {
private _scene: Scene | null;
protected logger = createLogger('EntitySystem');
// 装饰器动态添加的方法(可选)
protected initEventListeners?: () => void;
protected cleanupEventListeners?: () => void;
/**
* 实体ID映射缓存
@@ -139,8 +136,6 @@ export abstract class EntitySystem implements ISystemBase {
this._entityIdMapVersion = -1;
this._entityIdMapSize = 0;
// 初始化装饰器事件监听器
this.initDecoratorEventListeners();
this._entityCache = {
frame: null,
@@ -752,25 +747,6 @@ export abstract class EntitySystem implements ISystemBase {
}
}
/**
* 初始化装饰器事件监听器
*/
protected initDecoratorEventListeners(): void {
if (this.initEventListeners) {
this.initEventListeners();
}
}
/**
* 清理装饰器事件监听器
*/
protected cleanupDecoratorEventListeners(): void {
if (this.cleanupEventListeners) {
this.cleanupEventListeners();
}
}
/**
* 清理手动添加的事件监听器
*/
@@ -792,13 +768,8 @@ export abstract class EntitySystem implements ISystemBase {
* 由框架调用,处理系统的完整销毁流程
*/
public destroy(): void {
// 1. 清理手动添加的事件监听器
this.cleanupManualEventListeners();
// 2. 清理装饰器事件监听器
this.cleanupDecoratorEventListeners();
// 3. 调用用户的销毁回调
this.onDestroy();
}