Files
short-drama-agent-front/src/composables/useQuery.ts
T
GouJ 7a60db3ced feat: 同步动作与首帧编辑能力并统一工作区反馈
接入动作时间轴、密度审计、人工视线目标和后台首帧编辑;独立读取已保存提示词,并在首帧晋升前重新核对持久化质量状态。
统一 NMessage 结果反馈、局部 loading 和业务说明间距,保留真实长任务进度与部分失败语义。
验证:pnpm check(49 个文件、393 项测试)、pnpm build;当前环境无法完成 localhost 浏览器验收及真实后端联调。
2026-10-12 20:05:03 +08:00

55 lines
1.9 KiB
TypeScript

import { useFeedbackMessage } from './useFeedbackMessage'
import { onScopeDispose, ref, watch, type Ref } from 'vue'
import { errorMessage } from '../lib/http'
/** 异步查询仅在首次加载、查询目标变化或显式刷新时请求,切换目标会取消过期响应。 */
export function useQuery<T>(key: Ref<string>, loader: (key: string, signal: AbortSignal) => Promise<T>) {
const data = ref<T | null>(null) as Ref<T | null>
const loading = ref(false)
const error = ref('')
/** 查询失败统一进入消息层,保留错误状态用于禁用写操作。 */
useFeedbackMessage(error)
const updatedAt = ref('')
let generation = 0
let controller: AbortController | undefined
let disposed = false
/** 取消上一次查询并读取当前目标,失败时保留已有数据。 */
async function refresh() {
if (disposed) return
const current = ++generation
controller?.abort()
controller = new AbortController()
const signal = controller.signal
loading.value = true
try {
const result = await loader(key.value, signal)
if (current !== generation || disposed) return
data.value = result
error.value = ''
updatedAt.value = new Date().toISOString()
} catch (cause) {
if (current === generation && !signal.aborted && !disposed) error.value = errorMessage(cause)
} finally {
if (current === generation && !disposed) loading.value = false
}
}
watch(
key,
() => {
data.value = null
error.value = ''
updatedAt.value = ''
void refresh()
},
{ immediate: true }
)
onScopeDispose(() => {
disposed = true
generation++
controller?.abort()
})
return { data, loading, error, updatedAt, refresh }
}