perf(short-drama-agent-front): 优化记录加载与工作区体验
This commit is contained in:
1 parent
d0264c08ce
commit
db9aea1694
111 files changed
+1680
-1290
No files matched your search
@@ -1,45 +1,120 @@
|
||||
import { computed, inject, type InjectionKey } from 'vue'
|
||||
import { computed, inject, ref, watch, type InjectionKey, type Ref } from 'vue'
|
||||
import { useQuery } from '../../composables/useQuery'
|
||||
import { projectsApi } from './api'
|
||||
import type { ProjectDetail } from './types'
|
||||
import type { Checkpoint } from '../workflows/types'
|
||||
import type { Ref } from 'vue'
|
||||
|
||||
/** 同一项目的各个 graph 共用一份查询,避免每个面板重复请求。 */
|
||||
/** 页面需要回读完整记录的工作流。 */
|
||||
export type CheckpointWorkflow = 'create-drama' | 'breakdown'
|
||||
|
||||
/** 项目布局共享轻量读取,完整记录只有消费者进入时才加载。 */
|
||||
export function useProjectData(id: Ref<string>) {
|
||||
const query = useQuery(id, async (projectId, signal) => {
|
||||
const [project, checkpoints, stageReadiness] = await Promise.all([
|
||||
projectsApi.detail(projectId, signal),
|
||||
projectsApi.checkpoints(projectId, signal),
|
||||
projectsApi.stageReadiness(projectId, signal)
|
||||
])
|
||||
return { project, checkpoints, stageReadiness }
|
||||
})
|
||||
const project = computed<ProjectDetail | null>(() => query.data.value?.project ?? null)
|
||||
const checkpoints = computed<Checkpoint[]>(() => query.data.value?.checkpoints ?? [])
|
||||
/** 导航只消费当前项目的准入响应。 */
|
||||
const stageReadiness = computed(() => {
|
||||
const readiness = query.data.value?.stageReadiness
|
||||
return readiness?.projectId === id.value ? readiness : null
|
||||
})
|
||||
/** 同项目并发轻量刷新只采用最后一次请求。 */
|
||||
let readinessRefresh = 0
|
||||
/** 子页面完成当前步骤后调用,只刷新轻量项目状态与导航门禁。 */
|
||||
async function refreshStageReadiness() {
|
||||
const projectId = id.value
|
||||
const snapshot = query.data.value
|
||||
if (!projectId || snapshot?.project.id !== projectId) return
|
||||
const request = ++readinessRefresh
|
||||
const nextReadiness = await projectsApi.stageReadiness(projectId)
|
||||
if (
|
||||
request === readinessRefresh &&
|
||||
id.value === projectId &&
|
||||
query.data.value === snapshot &&
|
||||
nextReadiness.projectId === projectId
|
||||
/** 正式项目正文与导航条件分别读取。 */
|
||||
const projectQuery = useQuery(id, (projectId, signal) =>
|
||||
projectId ? projectsApi.detail(projectId, signal) : Promise.resolve(null)
|
||||
)
|
||||
const readinessQuery = useQuery(id, (projectId, signal) =>
|
||||
projectId ? projectsApi.stageReadiness(projectId, signal) : Promise.resolve(null)
|
||||
)
|
||||
/** 摘要失败会阻断写操作,但不阻塞工作区浏览。 */
|
||||
const summaryQuery = useQuery(id, (projectId, signal) =>
|
||||
projectId ? projectsApi.checkpointSummaries(projectId, signal) : Promise.resolve(null)
|
||||
)
|
||||
/** 当前项目已请求过的工作流,页面卸载时保留。 */
|
||||
const requested = ref<Partial<Record<CheckpointWorkflow, string>>>({})
|
||||
/** 页面与布局可能同时观察到同一个终态,只处理一次。 */
|
||||
const handledExecutions = new Map<string, Promise<void>>()
|
||||
watch(
|
||||
id,
|
||||
() => {
|
||||
requested.value = {}
|
||||
handledExecutions.clear()
|
||||
},
|
||||
{ flush: 'sync' }
|
||||
)
|
||||
|
||||
/**
|
||||
* 用项目编号作为查询键;离开项目会取消旧请求并清除旧结果。
|
||||
* @param workflow 当前工作流
|
||||
* @returns 完整记录及其独立查询状态
|
||||
*/
|
||||
function createCheckpointQuery(workflow: CheckpointWorkflow) {
|
||||
const key = computed(() => (requested.value[workflow] === id.value ? id.value : ''))
|
||||
return useQuery(key, (projectId, signal) =>
|
||||
projectId ? projectsApi.checkpoints(projectId, signal, workflow) : Promise.resolve(null)
|
||||
)
|
||||
query.data.value = { ...snapshot, stageReadiness: nextReadiness }
|
||||
}
|
||||
return { ...query, project, checkpoints, stageReadiness, refreshStageReadiness }
|
||||
/** 两个查询在布局生命周期内存活,同项目切页直接复用。 */
|
||||
const checkpointQueries = {
|
||||
'create-drama': createCheckpointQuery('create-drama'),
|
||||
breakdown: createCheckpointQuery('breakdown')
|
||||
}
|
||||
/** 只发布当前项目的正式数据。 */
|
||||
const project = computed(() => (projectQuery.data.value?.id === id.value ? projectQuery.data.value : null))
|
||||
const stageReadiness = computed(() =>
|
||||
project.value && readinessQuery.data.value?.projectId === id.value ? readinessQuery.data.value : null
|
||||
)
|
||||
/** 摘要未取得或刷新失败时禁止依赖其作出写入判断。 */
|
||||
const summaryBlocked = computed(
|
||||
() =>
|
||||
!project.value ||
|
||||
summaryQuery.data.value === null ||
|
||||
summaryQuery.loading.value ||
|
||||
!!summaryQuery.error.value
|
||||
)
|
||||
/** 页面外壳加载与错误不包含按需正文。 */
|
||||
const loading = computed(() => projectQuery.loading.value || readinessQuery.loading.value)
|
||||
const error = computed(() => projectQuery.error.value || readinessQuery.error.value || summaryQuery.error.value)
|
||||
|
||||
/**
|
||||
* 声明页面需要某个工作流的正文;重复声明不会发出新请求。
|
||||
* @param workflow 当前工作流
|
||||
* @returns 完整记录查询
|
||||
*/
|
||||
function useWorkflowCheckpoints(workflow: CheckpointWorkflow) {
|
||||
if (requested.value[workflow] !== id.value) requested.value[workflow] = id.value
|
||||
return checkpointQueries[workflow]
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入或任务终态后只刷新摘要和受影响的已加载正文。
|
||||
* @param workflow 受影响的工作流,没有对应正文时仅刷新摘要
|
||||
* @param projectId 操作开始时绑定的项目
|
||||
* @param executionKey 共享观察器使用的终态标识
|
||||
* @returns 本次刷新结束
|
||||
*/
|
||||
async function refreshWorkflow(workflow: CheckpointWorkflow | null, projectId = id.value, executionKey?: string) {
|
||||
if (!projectId || id.value !== projectId) return
|
||||
const existing = executionKey ? handledExecutions.get(executionKey) : undefined
|
||||
if (existing) return existing
|
||||
const update = Promise.all([
|
||||
summaryQuery.refresh(),
|
||||
workflow && requested.value[workflow] === projectId
|
||||
? checkpointQueries[workflow].refresh()
|
||||
: Promise.resolve()
|
||||
]).then(() => {})
|
||||
if (executionKey) handledExecutions.set(executionKey, update)
|
||||
await update
|
||||
}
|
||||
|
||||
/** 刷新工作区入口数据,不读取完整工作流历史。 */
|
||||
async function refresh() {
|
||||
await Promise.all([projectQuery.refresh(), readinessQuery.refresh(), summaryQuery.refresh()])
|
||||
}
|
||||
|
||||
return {
|
||||
project,
|
||||
projectQuery,
|
||||
stageReadiness,
|
||||
readinessQuery,
|
||||
summaryQuery,
|
||||
summaryBlocked,
|
||||
loading,
|
||||
error,
|
||||
refresh,
|
||||
refreshProject: projectQuery.refresh,
|
||||
refreshStageReadiness: readinessQuery.refresh,
|
||||
useWorkflowCheckpoints,
|
||||
refreshWorkflow
|
||||
}
|
||||
}
|
||||
|
||||
/** 项目布局向子页面提供的类型安全上下文。 */
|
||||
|
||||
Reference in new issue
Block a user