* 注册组件 * * @param typeName - 组件类型名称(必须以字母开头,只能包含字母和数字) * @param constructor - 组件构造函数 * @param tagName - 可选的自定义元素标签名(用于 Web Components) * @throws {RegistryError} 如果注册表已冻结、类型名称无效或组件已注册
(typeName: string, constructor: ComponentConstructor, _tagName?: string)
| 81 | * @throws {RegistryError} 如果注册表已冻结、类型名称无效或组件已注册 |
| 82 | */ |
| 83 | register(typeName: string, constructor: ComponentConstructor, _tagName?: string): void { |
| 84 | // 检查是否已冻结 |
| 85 | if (this.frozen) { |
| 86 | throw new RegistryError( |
| 87 | 'Cannot register component: registry is frozen in production mode', |
| 88 | RegistryErrorCodes.REGISTRY_FROZEN, |
| 89 | { typeName }, |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | // 验证类型名称 |
| 94 | if (!isValidTypeName(typeName)) { |
| 95 | throw new RegistryError( |
| 96 | `Invalid component type name: "${typeName}". Type name must start with a letter and contain only alphanumeric characters.`, |
| 97 | RegistryErrorCodes.INVALID_TYPE_NAME, |
| 98 | { typeName }, |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | // 检查是否已注册 |
| 103 | if (this.components.has(typeName)) { |
| 104 | // 如果已注册相同的构造函数,静默返回(幂等性) |
| 105 | if (this.components.get(typeName) === constructor) { |
| 106 | return; |
| 107 | } |
| 108 | // 否则记录警告并返回(不覆盖) |
| 109 | console.warn(`Component "${typeName}" is already registered. Skipping registration.`); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | // 注册组件 |
| 114 | this.components.set(typeName, constructor); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * 获取已注册的组件构造函数 |
no test coverage detected