/** * features/chat/shipping/useShippingProcessingWorkItem.ts * 用途:按 work_item_type + work_item_no 轮询统一 ShippingProcessingProjection。 * 依赖:shipping.api getWorkspaceShippingProcessingWorkItem、normalizeShippingProcessing。 * 逻辑:复核台 / 任务中心双读接入;404 或权威 OT 缺失时保持 null,不打断旧进度 UI。 */ import { getCurrentInstance, onBeforeUnmount, ref, watch, type MaybeRefOrGetter, type Ref, toValue, } from 'vue'; import { BusinessError } from '@/shared/api/client'; import { getWorkspaceShippingProcessingWorkItem } from '../api/shipping.api'; import type { ShippingProcessingProjection } from '../types/shipping.api.types'; import { normalizeShippingProcessingProjection } from './normalizeShippingProcessing'; const DEFAULT_POLL_MS = 3000; const TERMINAL_STATUSES = new Set([ 'completed', 'failed', 'cancelled', ]); export type ShippingProcessingWorkItemTypeArg = 'order_intake' | 'order_task'; /** * 轮询单个统一处理工作项投影。 * * @param workItemType - order_intake | order_task * @param workItemNo - Intake 编号或 OT 编号(MaybeRef) * @param options.enabled - 是否启用;options.pollMs - 轮询间隔 * @returns projection / loading / error / refresh / stop */ export function useShippingProcessingWorkItem( workItemType: ShippingProcessingWorkItemTypeArg, workItemNo: MaybeRefOrGetter, options: { enabled?: MaybeRefOrGetter; pollMs?: number; } = {}, ): { projection: Ref; loading: Ref; error: Ref; refresh: () => Promise; stop: () => void; } { const projection = ref(null); const loading = ref(false); const error = ref(null); let timer: ReturnType | null = null; let generation = 0; let stopped = false; const pollMs = options.pollMs ?? DEFAULT_POLL_MS; /** * 清除轮询定时器。 */ function clearTimer() { if (timer != null) { window.clearTimeout(timer); timer = null; } } /** * 停止轮询并递增代际,丢弃在途响应写回。 */ function stop() { stopped = true; generation += 1; clearTimer(); } /** * 拉取一次投影;失败保留上一帧。 */ async function refresh(): Promise { const no = String(toValue(workItemNo) || '').trim(); const enabled = options.enabled == null ? true : Boolean(toValue(options.enabled)); if (!enabled || !no) { projection.value = null; error.value = null; clearTimer(); return; } const gen = generation; loading.value = true; try { const raw = await getWorkspaceShippingProcessingWorkItem(workItemType, no); if (stopped || gen !== generation) return; const normalized = normalizeShippingProcessingProjection(raw); projection.value = normalized; error.value = null; clearTimer(); if (normalized && !TERMINAL_STATUSES.has(normalized.status) && !stopped) { timer = window.setTimeout(() => { void refresh(); }, pollMs); } } catch (err) { if (stopped || gen !== generation) return; // 404 / 业务不存在:清空投影但不抛到页面主流程。 if (err instanceof BusinessError && (err.status === 404 || err.code?.includes('NOT_FOUND'))) { projection.value = null; error.value = null; clearTimer(); return; } error.value = err instanceof BusinessError ? err.message : 'network_recoverable'; clearTimer(); if (!stopped) { timer = window.setTimeout(() => { void refresh(); }, pollMs); } } finally { if (gen === generation) loading.value = false; } } watch( () => [String(toValue(workItemNo) || ''), Boolean(toValue(options.enabled ?? true))] as const, ([no, enabled], _prev, onCleanup) => { stopped = false; generation += 1; clearTimer(); onCleanup(() => { clearTimer(); }); if (!enabled || !no) { projection.value = null; error.value = null; return; } void refresh(); }, { immediate: true }, ); if (getCurrentInstance()) { onBeforeUnmount(() => { stop(); }); } return { projection, loading, error, refresh, stop, }; }