This commit is contained in:
2025-09-25 15:31:11 +08:00
parent d18a3d5279
commit 972e76e655
11 changed files with 766 additions and 453 deletions

View File

@@ -1,10 +1,10 @@
import { reactive, ref } from 'vue'
import { reactive } from 'vue'
import type { WindowService } from './WindowService'
import type { ResourceService } from './ResourceService'
import type { EventCommunicationService } from './EventCommunicationService'
import type { ApplicationSandboxEngine } from './ApplicationSandboxEngine'
import { v4 as uuidv4 } from 'uuid'
import { externalAppDiscovery, type ExternalApp } from './ExternalAppDiscovery'
import { externalAppDiscovery } from './ExternalAppDiscovery'
/**
* 应用状态枚举
@@ -271,12 +271,29 @@ export class ApplicationLifecycleManager {
let app = this.installedApps.get(appId)
// 如果应用未安装,检查是否为外置应用
let isExternalApp = false
if (!app) {
const externalApp = externalAppDiscovery.getApp(appId)
if (externalApp) {
console.log(`[LifecycleManager] 发现外置应用 ${appId},自动注册`)
await this.registerExternalApp(externalApp)
app = this.installedApps.get(appId)
console.log(`[LifecycleManager] 发现外置应用 ${appId}`)
isExternalApp = true
// 为外部应用创建临时实例
const now = new Date()
app = {
id: externalApp.manifest.id,
manifest: externalApp.manifest,
state: AppLifecycleState.INSTALLED,
processId: '',
installedAt: now,
errorCount: 0,
crashCount: 0,
memoryUsage: 0,
cpuUsage: 0,
version: externalApp.manifest.version,
autoStart: false,
persistent: false,
}
}
}
@@ -296,7 +313,6 @@ export class ApplicationLifecycleManager {
// 检查是否为内置应用
let isBuiltInApp = false
let isExternalApp = false
try {
const { AppRegistry } = await import('../apps/AppRegistry')
@@ -306,8 +322,9 @@ export class ApplicationLifecycleManager {
console.warn('无法导入 AppRegistry')
}
// 检查是否为外置应用
if (!isBuiltInApp) {
// 检查是否为外置应用(仅当不是内置应用时)
// 修复移除重复的变量声明使用已声明的isExternalApp变量
if (!isBuiltInApp && !isExternalApp) {
isExternalApp = externalAppDiscovery.hasApp(appId)
}
@@ -398,9 +415,21 @@ export class ApplicationLifecycleManager {
* 停止应用
*/
async stopApp(appId: string): Promise<boolean> {
const app = this.installedApps.get(appId)
// 首先从已安装应用中查找
let app = this.installedApps.get(appId)
// 如果未找到,从运行进程列表中查找(可能是外部应用的临时实例)
if (!app) {
throw new Error(`应用 ${appId} 未安装`)
for (const runningApp of this.runningProcesses.values()) {
if (runningApp.id === appId) {
app = runningApp
break
}
}
}
if (!app) {
throw new Error(`应用 ${appId} 未安装或未运行`)
}
if (app.state !== AppLifecycleState.RUNNING && app.state !== AppLifecycleState.SUSPENDED) {
@@ -449,7 +478,19 @@ export class ApplicationLifecycleManager {
* 暂停应用
*/
async suspendApp(appId: string): Promise<boolean> {
const app = this.installedApps.get(appId)
// 首先从已安装应用中查找
let app = this.installedApps.get(appId)
// 如果未找到,从运行进程列表中查找(可能是外部应用的临时实例)
if (!app) {
for (const runningApp of this.runningProcesses.values()) {
if (runningApp.id === appId) {
app = runningApp
break
}
}
}
if (!app || app.state !== AppLifecycleState.RUNNING) {
return false
}
@@ -481,7 +522,19 @@ export class ApplicationLifecycleManager {
* 恢复应用
*/
async resumeApp(appId: string): Promise<boolean> {
const app = this.installedApps.get(appId)
// 首先从已安装应用中查找
let app = this.installedApps.get(appId)
// 如果未找到,从运行进程列表中查找(可能是外部应用的临时实例)
if (!app) {
for (const runningApp of this.runningProcesses.values()) {
if (runningApp.id === appId) {
app = runningApp
break
}
}
}
if (!app || app.state !== AppLifecycleState.SUSPENDED) {
return false
}
@@ -545,7 +598,19 @@ export class ApplicationLifecycleManager {
* 检查应用是否正在运行
*/
isAppRunning(appId: string): boolean {
const app = this.installedApps.get(appId)
// 首先从已安装应用中查找
let app = this.installedApps.get(appId)
// 如果未找到,从运行进程列表中查找(可能是外部应用的临时实例)
if (!app) {
for (const runningApp of this.runningProcesses.values()) {
if (runningApp.id === appId) {
app = runningApp
break
}
}
}
return app?.state === AppLifecycleState.RUNNING || app?.state === AppLifecycleState.SUSPENDED
}
@@ -786,44 +851,8 @@ export class ApplicationLifecycleManager {
}
}
/**
* 注册外置应用
*/
private async registerExternalApp(externalApp: ExternalApp): Promise<void> {
try {
const { manifest } = externalApp
const now = new Date()
const appInstance: AppInstance = {
id: manifest.id,
manifest,
state: AppLifecycleState.INSTALLED,
processId: '',
installedAt: now,
errorCount: 0,
crashCount: 0,
memoryUsage: 0,
cpuUsage: 0,
version: manifest.version,
autoStart: false,
persistent: false,
}
this.installedApps.set(manifest.id, appInstance)
console.log(`[LifecycleManager] 外置应用 ${manifest.name} (${manifest.id}) 已自动注册`)
// 发送应用注册事件
this.eventService.sendMessage('system', 'app-lifecycle', {
type: 'external-app-registered',
appId: manifest.id,
manifest,
})
} catch (error) {
console.error(`[LifecycleManager] 注册外置应用失败:`, error)
throw error
}
}
// 已移除:外部应用不再需要注册到 installedApps 中
// 外部应用在启动时会创建临时实例
/**
* 为外置应用加载代码到沙箱