fix: add worker message trigger

This commit is contained in:
jialin
2026-01-20 10:36:38 +08:00
parent b4f69a4e2e
commit 9d13b27c9e
5 changed files with 30 additions and 23 deletions
+1 -9
View File
@@ -61,7 +61,6 @@ const useSetChunkRequest = () => {
const timer = useRef<any>(null);
const loadedSize = useRef(0);
const workerRef = useRef<any>(null);
const startLoadingRef = useRef(false);
const reset = () => {
loaded.current = 0;
@@ -170,7 +169,6 @@ const useSetChunkRequest = () => {
const setChunkRequest = (config: RequestConfig) => {
requestConfig.current = { ...particalConfig, ...config };
retryCount.current = totalCount;
startLoadingRef.current = false;
clearTimeout(timer.current);
axiosChunkRequest(requestConfig.current);
return axiosToken;
@@ -203,17 +201,11 @@ const useSetChunkRequest = () => {
2 ** (totalCount - retryCount.current) * 1000
);
}
if (requestReadyState === 3) {
setTimeout(() => {
startLoadingRef.current = true;
}, 5000);
}
}, [requestReadyState]);
return {
setChunkRequest,
createAxiosToken,
startLoadingRef
createAxiosToken
};
};
+16 -3
View File
@@ -14,6 +14,7 @@ type EventsType = 'CREATE' | 'UPDATE' | 'DELETE' | 'INSERT';
export function useUpdateChunkedList(options: {
events?: EventsType[];
dataList?: any[];
triggerAt?: React.MutableRefObject<number>;
limit?: number;
onCreate?: (args: any) => void;
onUpdate?: (args: any) => void;
@@ -24,7 +25,8 @@ export function useUpdateChunkedList(options: {
mapFun?: (args: any) => any;
computedID?: (d: object) => string;
}) {
const { events = ['CREATE', 'DELETE', 'UPDATE', 'INSERT'] } = options;
const { events = ['CREATE', 'DELETE', 'UPDATE', 'INSERT'], triggerAt } =
options;
const deletedIdsRef = useRef<Set<number | string>>(new Set());
const cacheDataListRef = useRef<any[]>(options.dataList || []);
const timerRef = useRef<any>(null);
@@ -63,6 +65,7 @@ export function useUpdateChunkedList(options: {
// CREATE
if (data?.type === WatchEventType.CREATE && events.includes('CREATE')) {
const latestCreateList: any[] = [];
const newDataList = collections.reduce((acc: any[], item: any) => {
const updateIndex = cacheDataListRef.current?.findIndex(
(sItem: any) => sItem.id === item.id
@@ -73,6 +76,12 @@ export function useUpdateChunkedList(options: {
} else {
cacheDataListRef.current[updateIndex] = updateItem;
}
if (
triggerAt?.current &&
Date.parse(item.created_at) >= triggerAt.current
) {
latestCreateList.push(updateItem);
}
return acc;
}, []);
@@ -82,7 +91,7 @@ export function useUpdateChunkedList(options: {
...cacheDataListRef.current
].slice(0, limit);
options.onCreate?.(newDataList);
options.onCreate?.(latestCreateList);
}
// DELETE
@@ -112,7 +121,11 @@ export function useUpdateChunkedList(options: {
updateItem,
...cacheDataListRef.current.slice(0, limit - 1)
];
options.onCreate?.([updateItem]);
if (options.onCreate && triggerAt?.current) {
if (Date.parse(item.created_at) >= triggerAt.current) {
options.onCreate?.([updateItem]);
}
}
}
});
}