Files
vue-desktop/src/apps/AppRegistry.ts
2025-09-28 14:32:07 +08:00

95 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { AppRegistration } from './types/AppManifest'
import { reactive } from 'vue'
/**
* 应用注册中心
* 管理所有内置应用的注册和获取
*/
export class AppRegistry {
private static instance: AppRegistry | null = null
private apps = reactive(new Map<string, AppRegistration>())
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()