import type { AppRegistration } from './types/AppManifest' import { reactive, markRaw } from 'vue' /** * 应用注册中心 * 管理所有内置应用的注册和获取 */ export class AppRegistry { private static instance: AppRegistry | null = null private apps = reactive(new Map()) private constructor() {} static getInstance(): AppRegistry { if (!AppRegistry.instance) { AppRegistry.instance = new AppRegistry() } return AppRegistry.instance } /** * 注册内置应用 */ registerApp(registration: AppRegistration): void { // 使用 markRaw 标记组件,避免被设为响应式 // 注意:对于异步组件,我们不立即标记为raw,而是在实际加载时处理 const safeRegistration = { ...registration, component: registration.component, } this.apps.set(registration.manifest.id, safeRegistration) console.log(`已注册内置应用: ${registration.manifest.name}`) } /** * 获取应用 */ getApp(appId: string): AppRegistration | undefined { return this.apps.get(appId) } /** * 获取所有应用 */ getAllApps(): AppRegistration[] { return Array.from(this.apps.values()) } /** * 获取所有内置应用 */ getBuiltInApps(): AppRegistration[] { return Array.from(this.apps.values()).filter((app) => app.isBuiltIn) } /** * 检查应用是否存在 */ hasApp(appId: string): boolean { return this.apps.has(appId) } /** * 按类别获取应用 */ getAppsByCategory(category: string): AppRegistration[] { return Array.from(this.apps.values()).filter((app) => app.manifest.category === category) } /** * 搜索应用 */ searchApps(query: string): AppRegistration[] { const lowercaseQuery = query.toLowerCase() return Array.from(this.apps.values()).filter((app) => { const manifest = app.manifest return ( manifest.name.toLowerCase().includes(lowercaseQuery) || manifest.description.toLowerCase().includes(lowercaseQuery) || manifest.keywords?.some((keyword) => keyword.toLowerCase().includes(lowercaseQuery)) ) }) } /** * 清空所有应用 */ clear(): void { this.apps.clear() } } // 导出单例实例 export const appRegistry = AppRegistry.getInstance()