fix: update gpus state by polling

This commit is contained in:
jialin
2025-06-12 17:26:34 +08:00
parent 1b6cfdbdc4
commit 81833c4938
2 changed files with 42 additions and 8 deletions
+41 -7
View File
@@ -8,8 +8,9 @@ import qs from 'query-string';
import { useEffect, useRef, useState } from 'react'; import { useEffect, useRef, useState } from 'react';
type WatchConfig = type WatchConfig =
| { watch?: false | undefined; API?: string } | { watch?: false | undefined; API?: string; polling?: boolean }
| { watch: true; API: string }; | { watch: true; API: string; polling?: false | undefined }
| { polling: true; watch: false | undefined; API?: string };
export default function useTableFetch<ListItem>( export default function useTableFetch<ListItem>(
options: { options: {
@@ -24,9 +25,11 @@ export default function useTableFetch<ListItem>(
deleteAPI, deleteAPI,
contentForDelete, contentForDelete,
API, API,
polling = false,
watch, watch,
defaultData = [] defaultData = []
} = options; } = options;
const pollingRef = useRef<any>(null);
const chunkRequedtRef = useRef<any>(null); const chunkRequedtRef = useRef<any>(null);
const modalRef = useRef<any>(null); const modalRef = useRef<any>(null);
const rowSelection = useTableRowSelection(); const rowSelection = useTableRowSelection();
@@ -92,12 +95,14 @@ export default function useTableFetch<ListItem>(
} }
}; };
const fetchData = async (params?: { query: any }) => { const fetchData = async (params?: { query: any }, polling = false) => {
if (!polling) {
setDataSource((pre) => {
pre.loading = true;
return { ...pre };
});
}
const { query } = params || {}; const { query } = params || {};
setDataSource((pre) => {
pre.loading = true;
return { ...pre };
});
try { try {
const params = { const params = {
..._.pickBy(query || queryParams, (val: any) => !!val) ..._.pickBy(query || queryParams, (val: any) => !!val)
@@ -143,6 +148,24 @@ export default function useTableFetch<ListItem>(
} }
}; };
const fetchAPIWithPolling = async (params: any) => {
if (!polling || watch || !fetchAPI) return;
if (pollingRef.current) {
clearInterval(pollingRef.current);
}
// fetch data with polling, 1s interval
pollingRef.current = setInterval(async () => {
fetchData(
{
query: params
},
true
);
}, 5000);
};
const handleQueryChange = (params: any) => { const handleQueryChange = (params: any) => {
setQueryParams({ setQueryParams({
...queryParams, ...queryParams,
@@ -220,6 +243,17 @@ export default function useTableFetch<ListItem>(
}); });
}; };
useEffect(() => {
if (dataSource.loadend) {
fetchAPIWithPolling(queryParams);
}
return () => {
if (pollingRef.current) {
clearInterval(pollingRef.current);
}
};
}, [dataSource.loadend, queryParams]);
useEffect(() => { useEffect(() => {
const init = async () => { const init = async () => {
await fetchData(); await fetchData();
+1 -1
View File
@@ -49,7 +49,7 @@ const GPUList: React.FC = () => {
handleNameChange handleNameChange
} = useTableFetch<GPUDeviceItem>({ } = useTableFetch<GPUDeviceItem>({
fetchAPI: queryGpuDevicesList, fetchAPI: queryGpuDevicesList,
watch: true, polling: true,
API: GPU_DEVICES_API API: GPU_DEVICES_API
}); });