* 注册组件
(component: BaseComponent)
| 115 | * 注册组件 |
| 116 | */ |
| 117 | public async registerComponent(component: BaseComponent): Promise<void> { |
| 118 | const id = component.getId(); |
| 119 | |
| 120 | if (this.components.has(id)) { |
| 121 | throw new Error(`组件 "${id}" 已存在`); |
| 122 | } |
| 123 | |
| 124 | this.components.set(id, component); |
| 125 | this.log(`组件 "${id}" 已注册`); |
| 126 | this.emit('componentRegistered', { id, component }); |
| 127 | |
| 128 | // 如果管理器已初始化且启用自动初始化,立即初始化新组件 |
| 129 | if (this.isInitialized && this.config.autoInit) { |
| 130 | try { |
| 131 | await component.init(); |
| 132 | this.log(`组件 "${id}" 已自动初始化`); |
| 133 | this.emit('componentInitialized', { id, component }); |
| 134 | } catch (error) { |
| 135 | this.log(`组件 "${id}" 自动初始化失败: ${error}`); |
| 136 | this.emit('componentInitializationFailed', { id, component, error: error as Error }); |
| 137 | throw error; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * 获取组件 |
no test coverage detected