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
@@ -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 })
|
||||
|
||||
@@ -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<void> {
|
||||
await this.store.pickFile()
|
||||
await this.store.analyzeSelectedFile()
|
||||
}
|
||||
|
||||
private async onImport(): Promise<void> {
|
||||
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: '导入中' })
|
||||
|
||||
@@ -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 })
|
||||
|
||||
@@ -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<void> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -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<void> {
|
||||
this.phase = 'picking'
|
||||
this.notify()
|
||||
this.selectedFile = await this.service.pickMrpFile()
|
||||
this.phase = this.selectedFile ? 'analyzing' : 'idle'
|
||||
this.notify()
|
||||
}
|
||||
|
||||
async analyzeSelectedFile(): Promise<void> {
|
||||
@@ -29,6 +34,7 @@ export class ImportStore {
|
||||
this.phase = 'failed'
|
||||
this.error = { message: '包解析失败', retryable: true }
|
||||
}
|
||||
this.notify()
|
||||
}
|
||||
|
||||
async startImport(): Promise<void> {
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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<void> {
|
||||
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<void> {
|
||||
@@ -37,6 +42,7 @@ export class RuntimeSessionStore {
|
||||
this.phase = 'error'
|
||||
this.error = { message: '重启失败', retryable: true }
|
||||
}
|
||||
this.notify()
|
||||
}
|
||||
|
||||
async stopSession(): Promise<void> {
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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