129 lines
5.4 KiB
TypeScript
129 lines
5.4 KiB
TypeScript
import { computed, inject, ref, watch, type InjectionKey, type Ref } from 'vue'
|
|
import { useQuery } from '../../composables/useQuery'
|
|
import { projectsApi } from './api'
|
|
|
|
/** 页面需要回读完整记录的工作流。 */
|
|
export type CheckpointWorkflow = 'create-drama' | 'breakdown'
|
|
|
|
/** 项目布局共享轻量读取,完整记录只有消费者进入时才加载。 */
|
|
export function useProjectData(id: Ref<string>) {
|
|
/** 正式项目正文与导航条件分别读取。 */
|
|
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)
|
|
)
|
|
}
|
|
/** 两个查询在布局生命周期内存活,同项目切页直接复用。 */
|
|
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
|
|
}
|
|
}
|
|
|
|
/** 项目布局向子页面提供的类型安全上下文。 */
|
|
export const projectContextKey: InjectionKey<ReturnType<typeof useProjectData>> = Symbol('project-context')
|
|
|
|
/** 读取项目上下文,错误布局在开发阶段立即暴露。 */
|
|
export function useProjectContext() {
|
|
const context = inject(projectContextKey)
|
|
if (!context) throw new Error('项目页面必须位于 ProjectLayout 中')
|
|
return context
|
|
}
|