默认不增强事件,避免事件性能开销

This commit is contained in:
YHH
2025-09-26 10:28:00 +08:00
parent cf9ea495d0
commit 5e052a7e7d
2 changed files with 30 additions and 31 deletions

View File

@@ -41,36 +41,36 @@ export class EventBus implements IEventBus {
* 发射事件 * 发射事件
* @param eventType 事件类型 * @param eventType 事件类型
* @param data 事件数据 * @param data 事件数据
* @param enhance 是否增强事件数据添加timestamp、eventId等默认false提升性能
*/ */
public emit<T>(eventType: string, data: T): void { public emit<T>(eventType: string, data: T, enhance: boolean = false): void {
this.validateEventType(eventType); this.validateEventType(eventType);
// 增强事件数据 const finalData = enhance ? this.enhanceEventData(eventType, data) : data;
const enhancedData = this.enhanceEventData(eventType, data);
if (this.isDebugMode) { if (this.isDebugMode) {
EventBus._logger.info(`发射事件: ${eventType}`, enhancedData); EventBus._logger.info(`发射事件: ${eventType}`, finalData);
} }
this.eventSystem.emitSync(eventType, enhancedData); this.eventSystem.emitSync(eventType, finalData);
} }
/** /**
* 异步发射事件 * 异步发射事件
* @param eventType 事件类型 * @param eventType 事件类型
* @param data 事件数据 * @param data 事件数据
* @param enhance 是否增强事件数据添加timestamp、eventId等默认false提升性能
*/ */
public async emitAsync<T>(eventType: string, data: T): Promise<void> { public async emitAsync<T>(eventType: string, data: T, enhance: boolean = false): Promise<void> {
this.validateEventType(eventType); this.validateEventType(eventType);
// 增强事件数据 const finalData = enhance ? this.enhanceEventData(eventType, data) : data;
const enhancedData = this.enhanceEventData(eventType, data);
if (this.isDebugMode) { if (this.isDebugMode) {
EventBus._logger.info(`发射异步事件: ${eventType}`, enhancedData); EventBus._logger.info(`发射异步事件: ${eventType}`, finalData);
} }
await this.eventSystem.emit(eventType, enhancedData); await this.eventSystem.emit(eventType, finalData);
} }
/** /**
@@ -265,7 +265,7 @@ export class EventBus implements IEventBus {
public emitEntityCreated(entityData: IEntityEventData): void { public emitEntityCreated(entityData: IEntityEventData): void {
this.emit(ECSEventType.ENTITY_CREATED, entityData); this.emit(ECSEventType.ENTITY_CREATED, entityData);
} }
/** /**
* 发射实体销毁事件 * 发射实体销毁事件
* @param entityData 实体事件数据 * @param entityData 实体事件数据
@@ -273,7 +273,7 @@ export class EventBus implements IEventBus {
public emitEntityDestroyed(entityData: IEntityEventData): void { public emitEntityDestroyed(entityData: IEntityEventData): void {
this.emit(ECSEventType.ENTITY_DESTROYED, entityData); this.emit(ECSEventType.ENTITY_DESTROYED, entityData);
} }
/** /**
* 发射组件添加事件 * 发射组件添加事件
* @param componentData 组件事件数据 * @param componentData 组件事件数据
@@ -281,7 +281,7 @@ export class EventBus implements IEventBus {
public emitComponentAdded(componentData: IComponentEventData): void { public emitComponentAdded(componentData: IComponentEventData): void {
this.emit(ECSEventType.COMPONENT_ADDED, componentData); this.emit(ECSEventType.COMPONENT_ADDED, componentData);
} }
/** /**
* 发射组件移除事件 * 发射组件移除事件
* @param componentData 组件事件数据 * @param componentData 组件事件数据
@@ -289,7 +289,7 @@ export class EventBus implements IEventBus {
public emitComponentRemoved(componentData: IComponentEventData): void { public emitComponentRemoved(componentData: IComponentEventData): void {
this.emit(ECSEventType.COMPONENT_REMOVED, componentData); this.emit(ECSEventType.COMPONENT_REMOVED, componentData);
} }
/** /**
* 发射系统添加事件 * 发射系统添加事件
* @param systemData 系统事件数据 * @param systemData 系统事件数据
@@ -297,7 +297,7 @@ export class EventBus implements IEventBus {
public emitSystemAdded(systemData: ISystemEventData): void { public emitSystemAdded(systemData: ISystemEventData): void {
this.emit(ECSEventType.SYSTEM_ADDED, systemData); this.emit(ECSEventType.SYSTEM_ADDED, systemData);
} }
/** /**
* 发射系统移除事件 * 发射系统移除事件
* @param systemData 系统事件数据 * @param systemData 系统事件数据
@@ -305,7 +305,7 @@ export class EventBus implements IEventBus {
public emitSystemRemoved(systemData: ISystemEventData): void { public emitSystemRemoved(systemData: ISystemEventData): void {
this.emit(ECSEventType.SYSTEM_REMOVED, systemData); this.emit(ECSEventType.SYSTEM_REMOVED, systemData);
} }
/** /**
* 发射场景变化事件 * 发射场景变化事件
* @param sceneData 场景事件数据 * @param sceneData 场景事件数据
@@ -313,7 +313,7 @@ export class EventBus implements IEventBus {
public emitSceneChanged(sceneData: ISceneEventData): void { public emitSceneChanged(sceneData: ISceneEventData): void {
this.emit(ECSEventType.SCENE_ACTIVATED, sceneData); this.emit(ECSEventType.SCENE_ACTIVATED, sceneData);
} }
/** /**
* 发射性能警告事件 * 发射性能警告事件
* @param performanceData 性能事件数据 * @param performanceData 性能事件数据
@@ -375,16 +375,15 @@ export class EventBus implements IEventBus {
// 私有方法 // 私有方法
/** /**
* 验证事件类型 * 验证事件类型仅在debug模式下执行提升性能
* @param eventType 事件类型 * @param eventType 事件类型
*/ */
private validateEventType(eventType: string): void { private validateEventType(eventType: string): void {
if (!EventTypeValidator.isValid(eventType)) { // 只在debug模式下进行验证提升生产环境性能
if (this.isDebugMode) { if (this.isDebugMode) {
if (!EventTypeValidator.isValid(eventType)) {
EventBus._logger.warn(`未知事件类型: ${eventType}`); EventBus._logger.warn(`未知事件类型: ${eventType}`);
} // 在调试模式下添加自定义事件类型
// 在调试模式下添加自定义事件类型
if (this.isDebugMode) {
EventTypeValidator.addCustomType(eventType); EventTypeValidator.addCustomType(eventType);
} }
} }

View File

@@ -273,7 +273,7 @@ describe('EventBus - 事件总线测试', () => {
timestamp: Date.now() timestamp: Date.now()
}; };
eventBus.emitEntityCreated(entityData); eventBus.emit(ECSEventType.ENTITY_CREATED, entityData, true);
expect(receivedData).not.toBeNull(); expect(receivedData).not.toBeNull();
expect(receivedData!.entityId).toBe(1); expect(receivedData!.entityId).toBe(1);
@@ -370,7 +370,7 @@ describe('EventBus - 事件总线测试', () => {
}); });
const originalData = { message: 'test' }; const originalData = { message: 'test' };
eventBus.emit('enhanced:event', originalData); eventBus.emit('enhanced:event', originalData, true);
expect(receivedData.message).toBe('test'); expect(receivedData.message).toBe('test');
expect(receivedData.timestamp).toBeDefined(); expect(receivedData.timestamp).toBeDefined();