Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions src/components/VirtualTable/VirtualTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const VirtualTable = <T,>({

const [error, setError] = useState<IResponseError>();

const [pendingRequests, setPendingRequests] = useState<Record<string, NodeJS.Timeout>>({});
const pendingRequests = useRef<Record<string, ReturnType<typeof setTimeout>>>({});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to cancel all pendingRequest when component unmounts.


const fetchChunkData = useCallback(
async (id: string) => {
Expand Down Expand Up @@ -105,10 +105,13 @@ export const VirtualTable = <T,>({
}
}, DEFAULT_REQUEST_TIMEOUT);

setPendingRequests((reqs) => {
reqs[id] = timer;
return reqs;
});
// Chunk data load could be triggered by different events
// Cancel previous chunk request, while it is pending (instead of concurrentId)
if (pendingRequests.current[id]) {
const oldTimer = pendingRequests.current[id];
window.clearTimeout(oldTimer);
}
pendingRequests.current[id] = timer;
},
[fetchData, limit, sortParams],
);
Expand All @@ -117,20 +120,27 @@ export const VirtualTable = <T,>({
dispatch(initChunk(id));
}, []);

const onLeave = useCallback<OnLeave>(
(id) => {
dispatch(removeChunk(id));
const onLeave = useCallback<OnLeave>((id) => {
dispatch(removeChunk(id));

// If there is a pending request for the removed chunk, cancel it
// It made to prevent excessive requests on fast scroll
if (pendingRequests.current[id]) {
const timer = pendingRequests.current[id];
window.clearTimeout(timer);
delete pendingRequests.current[id];
}
}, []);

// If there is a pending request for the removed chunk, cancel it
// It made to prevent excessive requests on fast scroll
if (pendingRequests[id]) {
const timer = pendingRequests[id];
// Cancel all pending requests on component unmount
useEffect(() => {
return () => {
Object.values(pendingRequests.current).forEach((timer) => {
window.clearTimeout(timer);
delete pendingRequests[id];
}
},
[pendingRequests],
);
});
pendingRequests.current = {};
};
}, []);

// Load chunks if they become active
// This mecanism helps to set chunk active state from different sources, but load data only once
Expand Down