feat: connect run page to native smoke startup flow
This commit is contained in:
1 parent
06f4a95020
commit
a14b10d62f
4 files changed
+51
-13
No files matched your search
@@ -1,23 +1,43 @@
|
||||
const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms))
|
||||
import nativeMrp from 'libmrp_napi.so'
|
||||
|
||||
export class NativeRuntimeAdapter {
|
||||
async startSession(_appId: string): Promise<void> {
|
||||
await sleep(600)
|
||||
function assertOk(result: { ok: boolean; errorCode?: number; errorMessage?: string }, action: string): void {
|
||||
if (!result.ok) {
|
||||
throw new Error(`${action} failed: ${result.errorCode ?? -1} ${result.errorMessage ?? ''}`.trim())
|
||||
}
|
||||
}
|
||||
|
||||
async restartSession(_appId: string): Promise<void> {
|
||||
await sleep(400)
|
||||
export class NativeRuntimeAdapter {
|
||||
async startSession(appId: string): Promise<void> {
|
||||
assertOk(nativeMrp.init({
|
||||
workDir: '/data/storage/el2/base/files/mrp',
|
||||
width: 240,
|
||||
height: 320,
|
||||
debug: true,
|
||||
}), 'init')
|
||||
assertOk(nativeMrp.loadPackage(appId), 'loadPackage')
|
||||
assertOk(nativeMrp.start(), 'start')
|
||||
}
|
||||
|
||||
async restartSession(appId: string): Promise<void> {
|
||||
await this.stopSession()
|
||||
await this.startSession(appId)
|
||||
}
|
||||
|
||||
async stopSession(): Promise<void> {
|
||||
return
|
||||
assertOk(nativeMrp.release(), 'release')
|
||||
}
|
||||
|
||||
async notifyPageShow(): Promise<void> {
|
||||
const result = nativeMrp.resume()
|
||||
if (!result.ok) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
async notifyPageHide(): Promise<void> {
|
||||
const result = nativeMrp.pause()
|
||||
if (!result.ok) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,19 @@
|
||||
@Component
|
||||
export struct RuntimeContainer {
|
||||
@Prop hint: string = 'Runtime Surface Placeholder'
|
||||
|
||||
build() {
|
||||
Column() {
|
||||
Text('Runtime Surface Placeholder')
|
||||
Column({ space: 12 }) {
|
||||
Text('MRP Runtime Surface Placeholder')
|
||||
.fontSize(18)
|
||||
.fontWeight(FontWeight.Medium)
|
||||
Text(this.hint)
|
||||
.fontSize(14)
|
||||
.fontColor('#666666')
|
||||
}
|
||||
.justifyContent(FlexAlign.Center)
|
||||
.alignItems(HorizontalAlign.Center)
|
||||
.width('100%')
|
||||
.height('100%')
|
||||
.layoutWeight(1)
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,7 @@ export struct RunPage {
|
||||
if (this.appId) {
|
||||
StatusBanner({ text: `当前应用:${this.appId}` })
|
||||
}
|
||||
StatusBanner({ text: this.store.runtimeHint })
|
||||
if (this.store.error) {
|
||||
StatusBanner({ text: this.store.error.message })
|
||||
}
|
||||
@@ -67,7 +68,7 @@ export struct RunPage {
|
||||
onRestart: () => this.onRestart(),
|
||||
onExit: () => this.onExit()
|
||||
})
|
||||
RuntimeContainer()
|
||||
RuntimeContainer({ hint: this.store.runtimeHint })
|
||||
if (this.store.phase !== 'running') {
|
||||
RuntimeStatusOverlay({ text: `当前状态:${this.store.phase}` })
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ export class RuntimeSessionStore extends StoreObserver {
|
||||
error: UiError | null = null
|
||||
canRestart: boolean = false
|
||||
canExit: boolean = false
|
||||
runtimeHint: string = '等待启动'
|
||||
|
||||
constructor(private readonly service: RuntimeService = new RuntimeService()) {
|
||||
super()
|
||||
@@ -20,15 +21,18 @@ export class RuntimeSessionStore extends StoreObserver {
|
||||
this.error = null
|
||||
this.canRestart = false
|
||||
this.canExit = false
|
||||
this.runtimeHint = '正在初始化 Native/NAPI...'
|
||||
this.notify()
|
||||
try {
|
||||
await this.service.start(appId)
|
||||
this.phase = 'running'
|
||||
this.canRestart = true
|
||||
this.canExit = true
|
||||
} catch (_error) {
|
||||
this.runtimeHint = 'Native/NAPI 已连通,可进入后续拉帧联调'
|
||||
} catch (error) {
|
||||
this.phase = 'error'
|
||||
this.error = { message: '启动失败', retryable: true }
|
||||
this.runtimeHint = error instanceof Error ? error.message : 'Native/NAPI 启动失败'
|
||||
}
|
||||
this.notify()
|
||||
}
|
||||
@@ -40,15 +44,18 @@ export class RuntimeSessionStore extends StoreObserver {
|
||||
this.phase = 'starting'
|
||||
this.error = null
|
||||
this.canRestart = false
|
||||
this.runtimeHint = '正在重启 Native/NAPI...'
|
||||
this.notify()
|
||||
try {
|
||||
await this.service.restart(this.currentAppId)
|
||||
this.phase = 'running'
|
||||
this.canRestart = true
|
||||
this.canExit = true
|
||||
} catch (_error) {
|
||||
this.runtimeHint = '重启完成,Native/NAPI 仍可用'
|
||||
} catch (error) {
|
||||
this.phase = 'error'
|
||||
this.error = { message: '重启失败', retryable: true }
|
||||
this.runtimeHint = error instanceof Error ? error.message : '重启失败'
|
||||
}
|
||||
this.notify()
|
||||
}
|
||||
@@ -58,6 +65,7 @@ export class RuntimeSessionStore extends StoreObserver {
|
||||
this.phase = 'stopped'
|
||||
this.canExit = false
|
||||
this.canRestart = false
|
||||
this.runtimeHint = '运行会话已释放'
|
||||
this.notify()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user