pref(logBar): 优化运行数据处理逻辑

master
钟良源 2 months ago
parent 701e3524ae
commit 20b10715ed

@ -164,20 +164,24 @@ const LogBar: React.FC<LogBarProps> = () => {
}; };
}, [dispatch, currentAppData]); }, [dispatch, currentAppData]);
// 实现轮询获取运行数据 // 获取当前应用的运行状态
const currentAppKey = getCurrentAppKey();
const isRunning = currentAppKey && appRuntimeData[currentAppKey]?.isRunning;
// 实现轮询获取运行数据 - 只在应用运行时轮询
useEffect(() => { useEffect(() => {
let intervalId: NodeJS.Timeout | null = null; let intervalId: NodeJS.Timeout | null = null;
const currentAppKey = getCurrentAppKey(); const appKey = getCurrentAppKey();
// 只有在当前tab是运行数据且有当前应用时才开始轮询 // 只有在应用正在运行且有 runId 时才开始轮询
if (activeTab === '3' && currentAppKey && logBarStatus && appRuntimeData[currentAppKey]?.runId) { if (appKey && appRuntimeData[appKey]?.isRunning && appRuntimeData[appKey]?.runId) {
const fetchRuntimeData = async () => { const fetchRuntimeData = async () => {
try { try {
setLoading(true); setLoading(true);
const response = await getNodeData(appRuntimeData[currentAppKey].runId); const response = await getNodeData(appRuntimeData[appKey].runId);
setRuntimeData(prev => ({ setRuntimeData(prev => ({
...prev, ...prev,
[currentAppKey]: response.data [appKey]: response.data
})); }));
} catch (error) { } catch (error) {
console.error('获取运行数据失败:', error); console.error('获取运行数据失败:', error);
@ -189,17 +193,30 @@ const LogBar: React.FC<LogBarProps> = () => {
// 立即获取一次数据 // 立即获取一次数据
fetchRuntimeData(); fetchRuntimeData();
// 设置轮询,每5秒获取一次数据 // 设置轮询,每3秒获取一次数据
intervalId = setInterval(fetchRuntimeData, 3000); intervalId = setInterval(fetchRuntimeData, 3000);
} }
// 清理函数,组件卸载或条件不满足时清除定时器 // 清理函数,组件卸载或应用停止运行时清除定时器
return () => { return () => {
if (intervalId) { if (intervalId) {
clearInterval(intervalId); clearInterval(intervalId);
} }
}; };
}, [activeTab, currentAppData, logBarStatus]); }, [currentAppData, appRuntimeData]);
// 当应用停止运行时,清除运行数据
useEffect(() => {
const appKey = getCurrentAppKey();
if (appKey && !appRuntimeData[appKey]?.isRunning) {
// 清除当前应用的运行数据
setRuntimeData(prev => {
const newData = { ...prev };
delete newData[appKey];
return newData;
});
}
}, [isRunning, currentAppData]);
// 渲染校验日志内容 // 渲染校验日志内容
const renderValidationLogs = () => { const renderValidationLogs = () => {

Loading…
Cancel
Save