feat(rohs-check): 新增 RoHS 检核工具进度查询功能及相关接口
This commit is contained in:
1 parent
fee33f69d9
commit
a18a07ef1a
19 files changed
+654
-58
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import { computed, onBeforeUnmount, ref, type Ref, watch } from 'vue';
|
||||
import { getToolRunProgress, type ToolRunProgress } from '@/api/toolRunProgress';
|
||||
|
||||
export interface ToolRunProgressConfig {
|
||||
type?: string;
|
||||
title?: string;
|
||||
poll_interval_seconds?: number;
|
||||
active_url?: string;
|
||||
detail_url_template?: string;
|
||||
lock_input_when_running?: boolean;
|
||||
lock_message?: string;
|
||||
}
|
||||
|
||||
const TERMINAL_STATUSES = new Set(['success', 'partial_failed', 'failed', 'cancelled', 'canceled']);
|
||||
|
||||
const clampPollMs = (seconds: unknown) => {
|
||||
const raw = Number(seconds);
|
||||
const safe = Number.isFinite(raw) ? raw : 2;
|
||||
return Math.max(1, Math.min(safe, 30)) * 1000;
|
||||
};
|
||||
|
||||
export function useToolRunProgress(configRef: Ref<ToolRunProgressConfig | null>) {
|
||||
const run = ref<ToolRunProgress | null>(null);
|
||||
const loading = ref(false);
|
||||
const error = ref('');
|
||||
let timer: number | undefined;
|
||||
|
||||
const enabled = computed(() => !!configRef.value?.active_url);
|
||||
const isRunning = computed(() => run.value?.status === 'running');
|
||||
const shouldLockInput = computed(() => Boolean(configRef.value?.lock_input_when_running) && isRunning.value);
|
||||
const lockMessage = computed(() => configRef.value?.lock_message || '当前任务执行中,请等待完成');
|
||||
|
||||
const clearTimer = () => {
|
||||
if (timer !== undefined) {
|
||||
window.clearTimeout(timer);
|
||||
timer = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
const scheduleNext = () => {
|
||||
clearTimer();
|
||||
if (!enabled.value) return;
|
||||
const interval = clampPollMs(configRef.value?.poll_interval_seconds);
|
||||
timer = window.setTimeout(() => {
|
||||
void refresh();
|
||||
}, interval);
|
||||
};
|
||||
|
||||
const refresh = async () => {
|
||||
const cfg = configRef.value;
|
||||
if (!cfg?.active_url) {
|
||||
run.value = null;
|
||||
error.value = '';
|
||||
clearTimer();
|
||||
return;
|
||||
}
|
||||
loading.value = true;
|
||||
try {
|
||||
const payload = await getToolRunProgress(cfg.active_url);
|
||||
run.value = payload.run;
|
||||
error.value = '';
|
||||
} catch (e: any) {
|
||||
error.value = e?.message || '任务进度查询失败';
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
|
||||
if (!run.value || run.value.status === 'running' || !TERMINAL_STATUSES.has(run.value.status)) {
|
||||
scheduleNext();
|
||||
} else {
|
||||
clearTimer();
|
||||
}
|
||||
};
|
||||
|
||||
watch(
|
||||
() => JSON.stringify(configRef.value || {}),
|
||||
() => {
|
||||
run.value = null;
|
||||
error.value = '';
|
||||
clearTimer();
|
||||
if (enabled.value) void refresh();
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
onBeforeUnmount(clearTimer);
|
||||
|
||||
return {
|
||||
run,
|
||||
loading,
|
||||
error,
|
||||
isRunning,
|
||||
shouldLockInput,
|
||||
lockMessage,
|
||||
refresh,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user