feat: add minimal reactive store linkage for ArkTS shell
This commit is contained in:
1 parent
0b20b5dd43
commit
7fb4e6117a
8 files changed
+147
-6
No files matched your search
@@ -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 页面路由占位草案
|
||||||
@@ -9,18 +9,33 @@ import { AppListView } from '../components/appList/AppListView'
|
|||||||
@Entry
|
@Entry
|
||||||
@Component
|
@Component
|
||||||
struct AppListPage {
|
struct AppListPage {
|
||||||
|
@State refreshTick: number = 0
|
||||||
private store: AppCatalogStore = new AppCatalogStore()
|
private store: AppCatalogStore = new AppCatalogStore()
|
||||||
|
private unsubscribe?: () => void
|
||||||
|
|
||||||
aboutToAppear(): void {
|
aboutToAppear(): void {
|
||||||
|
if (!this.unsubscribe) {
|
||||||
|
this.unsubscribe = this.store.subscribe(() => {
|
||||||
|
this.refreshTick += 1
|
||||||
|
})
|
||||||
|
}
|
||||||
this.store.loadApps()
|
this.store.loadApps()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
aboutToDisappear(): void {
|
||||||
|
if (this.unsubscribe) {
|
||||||
|
this.unsubscribe()
|
||||||
|
this.unsubscribe = undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private openImportPage(): void {
|
private openImportPage(): void {
|
||||||
console.info('TODO: open ImportPage')
|
console.info('TODO: open ImportPage')
|
||||||
}
|
}
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
Column() {
|
Column() {
|
||||||
|
Text(`${this.refreshTick}`).fontSize(1).opacity(0)
|
||||||
PageHeader({ title: '应用列表' })
|
PageHeader({ title: '应用列表' })
|
||||||
if (this.store.error) {
|
if (this.store.error) {
|
||||||
StatusBanner({ text: this.store.error.message })
|
StatusBanner({ text: this.store.error.message })
|
||||||
|
|||||||
@@ -8,15 +8,37 @@ import { ImportResultPanel } from '../components/import/ImportResultPanel'
|
|||||||
|
|
||||||
@Component
|
@Component
|
||||||
export struct ImportPage {
|
export struct ImportPage {
|
||||||
|
@State refreshTick: number = 0
|
||||||
private store: ImportStore = new ImportStore()
|
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<void> {
|
private async onPickFile(): Promise<void> {
|
||||||
await this.store.pickFile()
|
await this.store.pickFile()
|
||||||
await this.store.analyzeSelectedFile()
|
await this.store.analyzeSelectedFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async onImport(): Promise<void> {
|
||||||
|
await this.store.startImport()
|
||||||
|
}
|
||||||
|
|
||||||
build() {
|
build() {
|
||||||
Column() {
|
Column() {
|
||||||
|
Text(`${this.refreshTick}`).fontSize(1).opacity(0)
|
||||||
PageHeader({ title: '导入应用' })
|
PageHeader({ title: '导入应用' })
|
||||||
if (this.store.error) {
|
if (this.store.error) {
|
||||||
StatusBanner({ text: this.store.error.message })
|
StatusBanner({ text: this.store.error.message })
|
||||||
@@ -24,6 +46,9 @@ export struct ImportPage {
|
|||||||
ImportSourceSelector({ onPick: () => this.onPickFile() })
|
ImportSourceSelector({ onPick: () => this.onPickFile() })
|
||||||
if (this.store.previewInfo) {
|
if (this.store.previewInfo) {
|
||||||
PackagePreviewCard({ name: this.store.previewInfo.name })
|
PackagePreviewCard({ name: this.store.previewInfo.name })
|
||||||
|
Button('开始导入').onClick(() => {
|
||||||
|
this.onImport()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
if (this.store.phase === 'importing') {
|
if (this.store.phase === 'importing') {
|
||||||
ImportProgressPanel({ text: '导入中' })
|
ImportProgressPanel({ text: '导入中' })
|
||||||
|
|||||||
@@ -8,7 +8,24 @@ import { RuntimeStatusOverlay } from '../components/runtime/RuntimeStatusOverlay
|
|||||||
|
|
||||||
@Component
|
@Component
|
||||||
export struct RunPage {
|
export struct RunPage {
|
||||||
|
@State refreshTick: number = 0
|
||||||
private store: RuntimeSessionStore = new RuntimeSessionStore()
|
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 {
|
private onBack(): void {
|
||||||
console.info('TODO: back to AppListPage')
|
console.info('TODO: back to AppListPage')
|
||||||
@@ -24,6 +41,7 @@ export struct RunPage {
|
|||||||
|
|
||||||
build() {
|
build() {
|
||||||
Column() {
|
Column() {
|
||||||
|
Text(`${this.refreshTick}`).fontSize(1).opacity(0)
|
||||||
PageHeader({ title: '运行页' })
|
PageHeader({ title: '运行页' })
|
||||||
if (this.store.error) {
|
if (this.store.error) {
|
||||||
StatusBanner({ text: this.store.error.message })
|
StatusBanner({ text: this.store.error.message })
|
||||||
|
|||||||
@@ -1,23 +1,28 @@
|
|||||||
import { AppService } from '../services/AppService'
|
import { AppService } from '../services/AppService'
|
||||||
import type { AppItemVM } from '../models/AppItemVM'
|
import type { AppItemVM } from '../models/AppItemVM'
|
||||||
import type { UiError } from '../models/UiError'
|
import type { UiError } from '../models/UiError'
|
||||||
|
import { StoreObserver } from './StoreObserver'
|
||||||
|
|
||||||
export class AppCatalogStore {
|
export class AppCatalogStore extends StoreObserver {
|
||||||
items: AppItemVM[] = []
|
items: AppItemVM[] = []
|
||||||
loading: boolean = false
|
loading: boolean = false
|
||||||
error: UiError | null = null
|
error: UiError | null = null
|
||||||
|
|
||||||
constructor(private readonly service: AppService = new AppService()) {}
|
constructor(private readonly service: AppService = new AppService()) {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
async loadApps(): Promise<void> {
|
async loadApps(): Promise<void> {
|
||||||
this.loading = true
|
this.loading = true
|
||||||
this.error = null
|
this.error = null
|
||||||
|
this.notify()
|
||||||
try {
|
try {
|
||||||
this.items = await this.service.listApps()
|
this.items = await this.service.listApps()
|
||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
this.error = { message: '应用列表加载失败', retryable: true }
|
this.error = { message: '应用列表加载失败', retryable: true }
|
||||||
} finally {
|
} finally {
|
||||||
this.loading = false
|
this.loading = false
|
||||||
|
this.notify()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,5 +32,6 @@ export class AppCatalogStore {
|
|||||||
|
|
||||||
clearError(): void {
|
clearError(): void {
|
||||||
this.error = null
|
this.error = null
|
||||||
|
this.notify()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,21 +1,26 @@
|
|||||||
import { ImportService } from '../services/ImportService'
|
import { ImportService } from '../services/ImportService'
|
||||||
import type { PackagePreviewVM } from '../models/PackagePreviewVM'
|
import type { PackagePreviewVM } from '../models/PackagePreviewVM'
|
||||||
import type { UiError } from '../models/UiError'
|
import type { UiError } from '../models/UiError'
|
||||||
|
import { StoreObserver } from './StoreObserver'
|
||||||
|
|
||||||
export type ImportPhase = 'idle' | 'picking' | 'analyzing' | 'importing' | 'success' | 'failed'
|
export type ImportPhase = 'idle' | 'picking' | 'analyzing' | 'importing' | 'success' | 'failed'
|
||||||
|
|
||||||
export class ImportStore {
|
export class ImportStore extends StoreObserver {
|
||||||
phase: ImportPhase = 'idle'
|
phase: ImportPhase = 'idle'
|
||||||
selectedFile: string | null = null
|
selectedFile: string | null = null
|
||||||
previewInfo: PackagePreviewVM | null = null
|
previewInfo: PackagePreviewVM | null = null
|
||||||
error: UiError | null = null
|
error: UiError | null = null
|
||||||
|
|
||||||
constructor(private readonly service: ImportService = new ImportService()) {}
|
constructor(private readonly service: ImportService = new ImportService()) {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
async pickFile(): Promise<void> {
|
async pickFile(): Promise<void> {
|
||||||
this.phase = 'picking'
|
this.phase = 'picking'
|
||||||
|
this.notify()
|
||||||
this.selectedFile = await this.service.pickMrpFile()
|
this.selectedFile = await this.service.pickMrpFile()
|
||||||
this.phase = this.selectedFile ? 'analyzing' : 'idle'
|
this.phase = this.selectedFile ? 'analyzing' : 'idle'
|
||||||
|
this.notify()
|
||||||
}
|
}
|
||||||
|
|
||||||
async analyzeSelectedFile(): Promise<void> {
|
async analyzeSelectedFile(): Promise<void> {
|
||||||
@@ -29,6 +34,7 @@ export class ImportStore {
|
|||||||
this.phase = 'failed'
|
this.phase = 'failed'
|
||||||
this.error = { message: '包解析失败', retryable: true }
|
this.error = { message: '包解析失败', retryable: true }
|
||||||
}
|
}
|
||||||
|
this.notify()
|
||||||
}
|
}
|
||||||
|
|
||||||
async startImport(): Promise<void> {
|
async startImport(): Promise<void> {
|
||||||
@@ -37,6 +43,7 @@ export class ImportStore {
|
|||||||
}
|
}
|
||||||
this.phase = 'importing'
|
this.phase = 'importing'
|
||||||
this.error = null
|
this.error = null
|
||||||
|
this.notify()
|
||||||
try {
|
try {
|
||||||
await this.service.importPackage(this.selectedFile)
|
await this.service.importPackage(this.selectedFile)
|
||||||
this.phase = 'success'
|
this.phase = 'success'
|
||||||
@@ -44,6 +51,7 @@ export class ImportStore {
|
|||||||
this.phase = 'failed'
|
this.phase = 'failed'
|
||||||
this.error = { message: '导入失败', retryable: true }
|
this.error = { message: '导入失败', retryable: true }
|
||||||
}
|
}
|
||||||
|
this.notify()
|
||||||
}
|
}
|
||||||
|
|
||||||
reset(): void {
|
reset(): void {
|
||||||
@@ -51,5 +59,6 @@ export class ImportStore {
|
|||||||
this.selectedFile = null
|
this.selectedFile = null
|
||||||
this.previewInfo = null
|
this.previewInfo = null
|
||||||
this.error = null
|
this.error = null
|
||||||
|
this.notify()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,20 +1,24 @@
|
|||||||
import { RuntimeService } from '../services/RuntimeService'
|
import { RuntimeService } from '../services/RuntimeService'
|
||||||
import type { UiError } from '../models/UiError'
|
import type { UiError } from '../models/UiError'
|
||||||
import type { RuntimePhase } from '../models/RuntimeSessionState'
|
import type { RuntimePhase } from '../models/RuntimeSessionState'
|
||||||
|
import { StoreObserver } from './StoreObserver'
|
||||||
|
|
||||||
export class RuntimeSessionStore {
|
export class RuntimeSessionStore extends StoreObserver {
|
||||||
currentAppId: string | null = null
|
currentAppId: string | null = null
|
||||||
phase: RuntimePhase = 'idle'
|
phase: RuntimePhase = 'idle'
|
||||||
error: UiError | null = null
|
error: UiError | null = null
|
||||||
canRestart: boolean = false
|
canRestart: boolean = false
|
||||||
canExit: 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<void> {
|
async startSession(appId: string): Promise<void> {
|
||||||
this.currentAppId = appId
|
this.currentAppId = appId
|
||||||
this.phase = 'starting'
|
this.phase = 'starting'
|
||||||
this.error = null
|
this.error = null
|
||||||
|
this.notify()
|
||||||
try {
|
try {
|
||||||
await this.service.start(appId)
|
await this.service.start(appId)
|
||||||
this.phase = 'running'
|
this.phase = 'running'
|
||||||
@@ -24,6 +28,7 @@ export class RuntimeSessionStore {
|
|||||||
this.phase = 'error'
|
this.phase = 'error'
|
||||||
this.error = { message: '启动失败', retryable: true }
|
this.error = { message: '启动失败', retryable: true }
|
||||||
}
|
}
|
||||||
|
this.notify()
|
||||||
}
|
}
|
||||||
|
|
||||||
async restartSession(): Promise<void> {
|
async restartSession(): Promise<void> {
|
||||||
@@ -37,6 +42,7 @@ export class RuntimeSessionStore {
|
|||||||
this.phase = 'error'
|
this.phase = 'error'
|
||||||
this.error = { message: '重启失败', retryable: true }
|
this.error = { message: '重启失败', retryable: true }
|
||||||
}
|
}
|
||||||
|
this.notify()
|
||||||
}
|
}
|
||||||
|
|
||||||
async stopSession(): Promise<void> {
|
async stopSession(): Promise<void> {
|
||||||
@@ -44,9 +50,11 @@ export class RuntimeSessionStore {
|
|||||||
this.phase = 'stopped'
|
this.phase = 'stopped'
|
||||||
this.canExit = false
|
this.canExit = false
|
||||||
this.canRestart = false
|
this.canRestart = false
|
||||||
|
this.notify()
|
||||||
}
|
}
|
||||||
|
|
||||||
clearError(): void {
|
clearError(): void {
|
||||||
this.error = null
|
this.error = null
|
||||||
|
this.notify()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
export type StoreListener = () => void
|
||||||
|
|
||||||
|
export class StoreObserver {
|
||||||
|
private listeners: Set<StoreListener> = new Set()
|
||||||
|
|
||||||
|
subscribe(listener: StoreListener): () => void {
|
||||||
|
this.listeners.add(listener)
|
||||||
|
return () => {
|
||||||
|
this.listeners.delete(listener)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected notify(): void {
|
||||||
|
this.listeners.forEach((listener) => listener())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user