44 lines
1.6 KiB
TypeScript
44 lines
1.6 KiB
TypeScript
import { reactive } from 'vue'
|
|
import { errorMessage } from '../../lib/http'
|
|
|
|
/** 当前浏览器会话中的操作状态;切换路由不会丢失长请求。 */
|
|
export interface Operation {
|
|
pending: boolean
|
|
label: string
|
|
error: string
|
|
notice: string
|
|
recovery: OperationRecovery | null
|
|
}
|
|
|
|
/** 失败后的可执行恢复位置;仅保留当前浏览器会话内的真实上下文。 */
|
|
export type OperationRecovery = {
|
|
target: 'keyframe'
|
|
shotId: string
|
|
episodeNo?: number
|
|
}
|
|
|
|
/** 按项目而非 graph 上锁,防止改写剧本与拆解同时提交。 */
|
|
const operations = reactive<Record<string, Operation>>({})
|
|
|
|
/** 取得项目操作状态,不向浏览器持久化虚假的后台运行状态。 */
|
|
export function getOperation(projectId: string): Operation {
|
|
return (operations[projectId] ??= { pending: false, label: '', error: '', notice: '', recovery: null })
|
|
}
|
|
|
|
/** 只提交一次;断网不自动重试,后台是否已执行由刷新后的 checkpoint 确认。 */
|
|
export async function runOperation(projectId: string, label: string, action: () => Promise<unknown>) {
|
|
const operation = getOperation(projectId)
|
|
if (operation.pending) return false
|
|
Object.assign(operation, { pending: true, label, error: '', notice: '', recovery: null })
|
|
try {
|
|
await action()
|
|
operation.notice = '请求已返回,正在重新读取后端状态;最终结果以下方记录为准。'
|
|
return true
|
|
} catch (error) {
|
|
operation.error = errorMessage(error)
|
|
return false
|
|
} finally {
|
|
operation.pending = false
|
|
}
|
|
}
|