From 7fb4e6117a71baec16b207887953765a6a0e5acc Mon Sep 17 00:00:00 2001 From: OpenClaw Date: Mon, 9 Mar 2026 15:00:32 +0800 Subject: [PATCH] feat: add minimal reactive store linkage for ArkTS shell --- docs/product/ARKTS_STORE_REACTIVE_LINK_V0.md | 44 +++++++++++++++++++ entry/src/main/ets/pages/AppListPage.ets | 15 +++++++ entry/src/main/ets/pages/ImportPage.ets | 25 +++++++++++ entry/src/main/ets/pages/RunPage.ets | 18 ++++++++ entry/src/main/ets/store/AppCatalogStore.ts | 10 ++++- entry/src/main/ets/store/ImportStore.ts | 13 +++++- .../src/main/ets/store/RuntimeSessionStore.ts | 12 ++++- entry/src/main/ets/store/StoreObserver.ts | 16 +++++++ 8 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 docs/product/ARKTS_STORE_REACTIVE_LINK_V0.md create mode 100644 entry/src/main/ets/store/StoreObserver.ts diff --git a/docs/product/ARKTS_STORE_REACTIVE_LINK_V0.md b/docs/product/ARKTS_STORE_REACTIVE_LINK_V0.md new file mode 100644 index 0000000..aef3a9b --- /dev/null +++ b/docs/product/ARKTS_STORE_REACTIVE_LINK_V0.md @@ -0,0 +1,44 @@ +# ArkTS Store 最小响应式联动方案 v0 + +项目:MRP 模拟器鸿蒙化 +角色:RD Frontend + +## 本轮完成 + +### 1. 新增 StoreObserver +文件:`entry/src/main/ets/store/StoreObserver.ts` + +职责: +- 提供 `subscribe()` +- 提供 `notify()` +- 作为 3 个 Store 的最小观察者基类 + +### 2. 3 个 Store 已接通知机制 +- `AppCatalogStore` +- `ImportStore` +- `RuntimeSessionStore` + +做法: +- 状态变更后调用 `notify()` +- 页面不直接轮询 Store +- 页面通过订阅回调触发局部刷新 + +### 3. 3 个页面已接最小刷新联动 +- `AppListPage` +- `ImportPage` +- `RunPage` + +做法: +- 页面新增 `@State refreshTick` +- `aboutToAppear()` 中订阅 Store +- `aboutToDisappear()` 中取消订阅 +- Store 状态变化时递增 `refreshTick`,驱动页面重绘 + +## 当前结论 +- 已从“Store 可调用”推进到“Store 可驱动页面刷新”的最小版 +- 当前方案是 POC 响应式占位实现,不替代后续正式状态框架 + +## 下一步 +1. 把 AppListPage 的列表项点击接到 `RunPage` 跳转占位 +2. 为 Adapter 增加 mock 数据,形成可演示的页面闭环 +3. 补一份 ArkTS 页面路由占位草案 diff --git a/entry/src/main/ets/pages/AppListPage.ets b/entry/src/main/ets/pages/AppListPage.ets index 7db8ebd..66415a9 100644 --- a/entry/src/main/ets/pages/AppListPage.ets +++ b/entry/src/main/ets/pages/AppListPage.ets @@ -9,18 +9,33 @@ import { AppListView } from '../components/appList/AppListView' @Entry @Component struct AppListPage { + @State refreshTick: number = 0 private store: AppCatalogStore = new AppCatalogStore() + private unsubscribe?: () => void aboutToAppear(): void { + if (!this.unsubscribe) { + this.unsubscribe = this.store.subscribe(() => { + this.refreshTick += 1 + }) + } this.store.loadApps() } + aboutToDisappear(): void { + if (this.unsubscribe) { + this.unsubscribe() + this.unsubscribe = undefined + } + } + private openImportPage(): void { console.info('TODO: open ImportPage') } build() { Column() { + Text(`${this.refreshTick}`).fontSize(1).opacity(0) PageHeader({ title: '应用列表' }) if (this.store.error) { StatusBanner({ text: this.store.error.message }) diff --git a/entry/src/main/ets/pages/ImportPage.ets b/entry/src/main/ets/pages/ImportPage.ets index d64987b..02a5c39 100644 --- a/entry/src/main/ets/pages/ImportPage.ets +++ b/entry/src/main/ets/pages/ImportPage.ets @@ -8,15 +8,37 @@ import { ImportResultPanel } from '../components/import/ImportResultPanel' @Component export struct ImportPage { + @State refreshTick: number = 0 private store: ImportStore = new ImportStore() + private unsubscribe?: () => void + + aboutToAppear(): void { + if (!this.unsubscribe) { + this.unsubscribe = this.store.subscribe(() => { + this.refreshTick += 1 + }) + } + } + + aboutToDisappear(): void { + if (this.unsubscribe) { + this.unsubscribe() + this.unsubscribe = undefined + } + } private async onPickFile(): Promise { await this.store.pickFile() await this.store.analyzeSelectedFile() } + private async onImport(): Promise { + await this.store.startImport() + } + build() { Column() { + Text(`${this.refreshTick}`).fontSize(1).opacity(0) PageHeader({ title: '导入应用' }) if (this.store.error) { StatusBanner({ text: this.store.error.message }) @@ -24,6 +46,9 @@ export struct ImportPage { ImportSourceSelector({ onPick: () => this.onPickFile() }) if (this.store.previewInfo) { PackagePreviewCard({ name: this.store.previewInfo.name }) + Button('开始导入').onClick(() => { + this.onImport() + }) } if (this.store.phase === 'importing') { ImportProgressPanel({ text: '导入中' }) diff --git a/entry/src/main/ets/pages/RunPage.ets b/entry/src/main/ets/pages/RunPage.ets index c36a586..0bcafa4 100644 --- a/entry/src/main/ets/pages/RunPage.ets +++ b/entry/src/main/ets/pages/RunPage.ets @@ -8,7 +8,24 @@ import { RuntimeStatusOverlay } from '../components/runtime/RuntimeStatusOverlay @Component export struct RunPage { + @State refreshTick: number = 0 private store: RuntimeSessionStore = new RuntimeSessionStore() + private unsubscribe?: () => void + + aboutToAppear(): void { + if (!this.unsubscribe) { + this.unsubscribe = this.store.subscribe(() => { + this.refreshTick += 1 + }) + } + } + + aboutToDisappear(): void { + if (this.unsubscribe) { + this.unsubscribe() + this.unsubscribe = undefined + } + } private onBack(): void { console.info('TODO: back to AppListPage') @@ -24,6 +41,7 @@ export struct RunPage { build() { Column() { + Text(`${this.refreshTick}`).fontSize(1).opacity(0) PageHeader({ title: '运行页' }) if (this.store.error) { StatusBanner({ text: this.store.error.message }) diff --git a/entry/src/main/ets/store/AppCatalogStore.ts b/entry/src/main/ets/store/AppCatalogStore.ts index 3c89483..68aaeb3 100644 --- a/entry/src/main/ets/store/AppCatalogStore.ts +++ b/entry/src/main/ets/store/AppCatalogStore.ts @@ -1,23 +1,28 @@ import { AppService } from '../services/AppService' import type { AppItemVM } from '../models/AppItemVM' import type { UiError } from '../models/UiError' +import { StoreObserver } from './StoreObserver' -export class AppCatalogStore { +export class AppCatalogStore extends StoreObserver { items: AppItemVM[] = [] loading: boolean = false error: UiError | null = null - constructor(private readonly service: AppService = new AppService()) {} + constructor(private readonly service: AppService = new AppService()) { + super() + } async loadApps(): Promise { this.loading = true this.error = null + this.notify() try { this.items = await this.service.listApps() } catch (_error) { this.error = { message: '应用列表加载失败', retryable: true } } finally { this.loading = false + this.notify() } } @@ -27,5 +32,6 @@ export class AppCatalogStore { clearError(): void { this.error = null + this.notify() } } diff --git a/entry/src/main/ets/store/ImportStore.ts b/entry/src/main/ets/store/ImportStore.ts index 458c3ee..2cd6589 100644 --- a/entry/src/main/ets/store/ImportStore.ts +++ b/entry/src/main/ets/store/ImportStore.ts @@ -1,21 +1,26 @@ import { ImportService } from '../services/ImportService' import type { PackagePreviewVM } from '../models/PackagePreviewVM' import type { UiError } from '../models/UiError' +import { StoreObserver } from './StoreObserver' export type ImportPhase = 'idle' | 'picking' | 'analyzing' | 'importing' | 'success' | 'failed' -export class ImportStore { +export class ImportStore extends StoreObserver { phase: ImportPhase = 'idle' selectedFile: string | null = null previewInfo: PackagePreviewVM | null = null error: UiError | null = null - constructor(private readonly service: ImportService = new ImportService()) {} + constructor(private readonly service: ImportService = new ImportService()) { + super() + } async pickFile(): Promise { this.phase = 'picking' + this.notify() this.selectedFile = await this.service.pickMrpFile() this.phase = this.selectedFile ? 'analyzing' : 'idle' + this.notify() } async analyzeSelectedFile(): Promise { @@ -29,6 +34,7 @@ export class ImportStore { this.phase = 'failed' this.error = { message: '包解析失败', retryable: true } } + this.notify() } async startImport(): Promise { @@ -37,6 +43,7 @@ export class ImportStore { } this.phase = 'importing' this.error = null + this.notify() try { await this.service.importPackage(this.selectedFile) this.phase = 'success' @@ -44,6 +51,7 @@ export class ImportStore { this.phase = 'failed' this.error = { message: '导入失败', retryable: true } } + this.notify() } reset(): void { @@ -51,5 +59,6 @@ export class ImportStore { this.selectedFile = null this.previewInfo = null this.error = null + this.notify() } } diff --git a/entry/src/main/ets/store/RuntimeSessionStore.ts b/entry/src/main/ets/store/RuntimeSessionStore.ts index c8e9215..0690be1 100644 --- a/entry/src/main/ets/store/RuntimeSessionStore.ts +++ b/entry/src/main/ets/store/RuntimeSessionStore.ts @@ -1,20 +1,24 @@ import { RuntimeService } from '../services/RuntimeService' import type { UiError } from '../models/UiError' import type { RuntimePhase } from '../models/RuntimeSessionState' +import { StoreObserver } from './StoreObserver' -export class RuntimeSessionStore { +export class RuntimeSessionStore extends StoreObserver { currentAppId: string | null = null phase: RuntimePhase = 'idle' error: UiError | null = null canRestart: boolean = false canExit: boolean = false - constructor(private readonly service: RuntimeService = new RuntimeService()) {} + constructor(private readonly service: RuntimeService = new RuntimeService()) { + super() + } async startSession(appId: string): Promise { this.currentAppId = appId this.phase = 'starting' this.error = null + this.notify() try { await this.service.start(appId) this.phase = 'running' @@ -24,6 +28,7 @@ export class RuntimeSessionStore { this.phase = 'error' this.error = { message: '启动失败', retryable: true } } + this.notify() } async restartSession(): Promise { @@ -37,6 +42,7 @@ export class RuntimeSessionStore { this.phase = 'error' this.error = { message: '重启失败', retryable: true } } + this.notify() } async stopSession(): Promise { @@ -44,9 +50,11 @@ export class RuntimeSessionStore { this.phase = 'stopped' this.canExit = false this.canRestart = false + this.notify() } clearError(): void { this.error = null + this.notify() } } diff --git a/entry/src/main/ets/store/StoreObserver.ts b/entry/src/main/ets/store/StoreObserver.ts new file mode 100644 index 0000000..436b276 --- /dev/null +++ b/entry/src/main/ets/store/StoreObserver.ts @@ -0,0 +1,16 @@ +export type StoreListener = () => void + +export class StoreObserver { + private listeners: Set = new Set() + + subscribe(listener: StoreListener): () => void { + this.listeners.add(listener) + return () => { + this.listeners.delete(listener) + } + } + + protected notify(): void { + this.listeners.forEach((listener) => listener()) + } +}