fix: worker list update by watch

This commit is contained in:
jialin
2026-01-14 19:58:09 +08:00
parent 2ee645b1ee
commit 1230c3bd28
2 changed files with 45 additions and 23 deletions
+3 -3
View File
@@ -12,7 +12,7 @@ export default {
handleWidth: 32 handleWidth: 32
}, },
Tag: { Tag: {
defaultBg: '#1d1d1d' defaultBg: '#272727'
}, },
Steps: { Steps: {
descriptionMaxWidth: 200, descriptionMaxWidth: 200,
@@ -23,8 +23,8 @@ export default {
cellPaddingInline: 16, cellPaddingInline: 16,
cellPaddingBlock: 6, cellPaddingBlock: 6,
cellFontSize: 14, cellFontSize: 14,
rowSelectedHoverBg: '#1d1d1d', rowSelectedHoverBg: '#272727',
rowHoverBg: '#1d1d1d', rowHoverBg: '#272727',
rowSelectedBg: 'transparent', rowSelectedBg: 'transparent',
headerSortActiveBg: 'transparent', headerSortActiveBg: 'transparent',
headerSortHoverBg: 'transparent', headerSortHoverBg: 'transparent',
+42 -20
View File
@@ -60,8 +60,12 @@ export default function useTableFetch<T>(
const modalRef = useRef<any>(null); const modalRef = useRef<any>(null);
const rowSelection = useTableRowSelection(); const rowSelection = useTableRowSelection();
const { sortOrder, handleMultiSortChange } = useTableMultiSort(); const { sortOrder, handleMultiSortChange } = useTableMultiSort();
// ======= to resolve worker upate issue =======
const shouldUpdateRef = useRef(false); const shouldUpdateRef = useRef(false);
const loadendRef = useRef(false); const loadendRef = useRef(false);
const currentWatchParamsRef = useRef<any>(null);
// ============================================
// for skeleton loading // for skeleton loading
const [extraStatus, setExtraStatus] = useState<Record<string, any>>({ const [extraStatus, setExtraStatus] = useState<Record<string, any>>({
@@ -110,7 +114,7 @@ export default function useTableFetch<T>(
const debounceSetExtraStatus = _.debounce(setExtraStatus, 3000); const debounceSetExtraStatus = _.debounce(setExtraStatus, 3000);
const fetchData = async ( const fetchData = async (
params?: { query: Record<string, any>; loadmore?: boolean }, externalParams?: { query: Record<string, any>; loadmore?: boolean },
polling = false polling = false
) => { ) => {
if (!polling) { if (!polling) {
@@ -119,13 +123,14 @@ export default function useTableFetch<T>(
return { ...pre }; return { ...pre };
}); });
} }
const { query, loadmore } = params || {}; const { query, loadmore } = externalParams || {};
try { try {
const params = { const params = {
..._.pickBy(query || queryParams, (val: any) => !!val) ..._.pickBy(query || queryParams, (val: any) => !!val)
}; };
const res = await fetchAPI(params); const res = await fetchAPI(params);
shouldUpdateRef.current = false;
loadendRef.current = true;
if (!dataSource.loadend) { if (!dataSource.loadend) {
// add a delay to avoid flash // add a delay to avoid flash
await new Promise((resolve) => { await new Promise((resolve) => {
@@ -152,7 +157,7 @@ export default function useTableFetch<T>(
total: newRes.pagination.total, total: newRes.pagination.total,
totalPage: newRes.pagination.totalPage totalPage: newRes.pagination.totalPage
}); });
loadendRef.current = true;
if (isInfiniteScroll) { if (isInfiniteScroll) {
setQueryParams(newParams); setQueryParams(newParams);
} }
@@ -168,7 +173,6 @@ export default function useTableFetch<T>(
total: res.pagination.total, total: res.pagination.total,
totalPage: res.pagination.totalPage totalPage: res.pagination.totalPage
}); });
loadendRef.current = true;
if (isInfiniteScroll && query?.page) { if (isInfiniteScroll && query?.page) {
setQueryParams({ setQueryParams({
...queryParams, ...queryParams,
@@ -185,6 +189,7 @@ export default function useTableFetch<T>(
totalPage: dataSource.totalPage totalPage: dataSource.totalPage
}); });
loadendRef.current = true; loadendRef.current = true;
shouldUpdateRef.current = false;
} finally { } finally {
debounceSetExtraStatus({ debounceSetExtraStatus({
firstLoad: false firstLoad: false
@@ -192,7 +197,11 @@ export default function useTableFetch<T>(
} }
}; };
const debounceFetchData = _.debounce(fetchData, 300); // @ts-ignore
const debounceFetchData = _.debounce(
(params: any) => fetchData(params, true),
300
);
const updateHandler = (list: any) => { const updateHandler = (list: any) => {
_.each(list, (data: any) => { _.each(list, (data: any) => {
@@ -204,18 +213,23 @@ export default function useTableFetch<T>(
data.type === WatchEventType.CREATE data.type === WatchEventType.CREATE
); );
// ======= to resolve worker upate issue =======
// when worker list change (create/delete), fetch data again to update the list
if (shouldUpdateRef.current && updateManually && loadendRef.current) { if (shouldUpdateRef.current && updateManually && loadendRef.current) {
shouldUpdateRef.current = false; debounceFetchData(currentWatchParamsRef.current || undefined);
debounceFetchData();
} }
// ============================================
}; };
const createModelsChunkRequest = async (params?: any) => { const createModelsChunkRequest = async (params?: any) => {
if (!API || !watch) return; if (!API || !watch) return;
shouldUpdateRef.current = false; shouldUpdateRef.current = false;
loadendRef.current = false; // loadendRef.current = false;
chunkRequedtRef.current?.current?.cancel?.(); chunkRequedtRef.current?.current?.cancel?.();
try { try {
currentWatchParamsRef.current = {
query: { ...(params || queryParams) }
};
const query = _.omit(params || queryParams, ['page', 'perPage']); const query = _.omit(params || queryParams, ['page', 'perPage']);
chunkRequedtRef.current = setChunkRequest({ chunkRequedtRef.current = setChunkRequest({
@@ -246,18 +260,22 @@ export default function useTableFetch<T>(
}; };
// for filters change // for filters change
const handleQueryChange = (params: any) => { const handleQueryChange = async (params: any) => {
loadendRef.current = false; loadendRef.current = false;
setQueryParams({ setQueryParams((pre: any) => {
...queryParams, return {
...params ...pre,
});
fetchData({ query: { ...queryParams, ...params } });
if (watch) {
createModelsChunkRequest({
...queryParams,
...params ...params
}); };
});
await fetchData({ query: { ...queryParams, ...params } });
if (watch) {
setTimeout(() => {
createModelsChunkRequest({
...queryParams,
...params
});
}, 200);
} }
}; };
@@ -318,7 +336,11 @@ export default function useTableFetch<T>(
await deleteAPI?.(row.id, { await deleteAPI?.(row.id, {
...modalRef.current?.configuration ...modalRef.current?.configuration
}); });
fetchData();
// ======== to avoid fetch data twice, because of debounceFetchData has been run =======
if (!updateManually) {
fetchData();
}
} }
}); });
}; };