diff --git a/AutoRobot/Windows/Robot/Web/src/DockLayout/eventBus.js b/AutoRobot/Windows/Robot/Web/src/DockLayout/eventBus.js index ab137be..6277804 100644 --- a/AutoRobot/Windows/Robot/Web/src/DockLayout/eventBus.js +++ b/AutoRobot/Windows/Robot/Web/src/DockLayout/eventBus.js @@ -436,6 +436,120 @@ export const triggerDragEvent = { } } +// 事件处理器注册和管理 +class EventHandlerRegistry { + constructor() { + this.handlers = new Map() + this.lifecycleHooks = { + init: new Set(), + destroy: new Set() + } + } + + /** + * 注册事件处理器 + */ + registerHandler(handlerName, eventTypes, handlerInstance) { + const subscription = new Set() + + // 注册事件监听器 + eventTypes.forEach(eventType => { + const unsubscribe = onEvent(eventType, (data, event) => { + if (handlerInstance[eventType] && typeof handlerInstance[eventType] === 'function') { + try { + handlerInstance[eventType](data, event) + } catch (error) { + console.error(`[EventHandlerRegistry] Error in handler ${handlerName} for ${eventType}:`, error) + } + } + }) + + subscription.add({ eventType, unsubscribe }) + }) + + this.handlers.set(handlerName, { + instance: handlerInstance, + subscriptions: subscription, + registeredAt: Date.now() + }) + + console.log(`[EventHandlerRegistry] Handler "${handlerName}" registered for events:`, eventTypes) + return handlerName + } + + /** + * 注销事件处理器 + */ + unregisterHandler(handlerName) { + const handler = this.handlers.get(handlerName) + if (!handler) return false + + // 取消所有订阅 + handler.subscriptions.forEach(({ unsubscribe }) => { + try { + unsubscribe() + } catch (error) { + console.warn(`[EventHandlerRegistry] Error unsubscribing handler ${handlerName}:`, error) + } + }) + + // 调用销毁钩子 + if (handler.instance.onDestroy) { + try { + handler.instance.onDestroy() + } catch (error) { + console.error(`[EventHandlerRegistry] Error in onDestroy for handler ${handlerName}:`, error) + } + } + + this.handlers.delete(handlerName) + console.log(`[EventHandlerRegistry] Handler "${handlerName}" unregistered`) + return true + } + + /** + * 获取所有处理器 + */ + getHandlers() { + return Array.from(this.handlers.keys()) + } + + /** + * 获取处理器信息 + */ + getHandlerInfo(handlerName) { + return this.handlers.get(handlerName) + } + + /** + * 销毁所有处理器 + */ + destroyAll() { + for (const handlerName of this.handlers.keys()) { + this.unregisterHandler(handlerName) + } + console.log(`[EventHandlerRegistry] All handlers destroyed`) + } + + /** + * 获取状态快照 + */ + getSnapshot() { + const snapshot = {} + for (const [name, handler] of this.handlers.entries()) { + snapshot[name] = { + registeredAt: handler.registeredAt, + subscriptionCount: handler.subscriptions.size, + hasDestroyMethod: typeof handler.instance.onDestroy === 'function' + } + } + return snapshot + } +} + +// 创建事件处理器注册表 +export const handlerRegistry = new EventHandlerRegistry() + // 导出便捷触发函数 export const emitEvent = (eventType, data, options = {}) => { return eventBus.emit(eventType, data, options) @@ -451,4 +565,17 @@ export const onceEvent = (eventType, callback, options = {}) => { export const offEvent = (eventType, callback) => { return eventBus.off(eventType, callback) +} + +// 导出事件处理器注册函数 +export const registerHandler = (handlerName, eventTypes, handlerInstance) => { + return handlerRegistry.registerHandler(handlerName, eventTypes, handlerInstance) +} + +export const unregisterHandler = (handlerName) => { + return handlerRegistry.unregisterHandler(handlerName) +} + +export const getHandlerSnapshot = () => { + return handlerRegistry.getSnapshot() } \ No newline at end of file