Skip to content

Commit

Permalink
fix: adjustment in useNetworkRequest and development mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mguellsegarra committed Jul 29, 2024
1 parent f9681b5 commit 20753b2
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/hooks/useNetworkRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,25 @@ export const useNetworkRequest = (
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const cancelRequest = useCallback((requestId?: string) => {
if (requestId) {
abortControllersRef.current.delete(requestId);
if (process.env.NODE_ENV !== "development") {
abortControllersRef.current.get(requestId)?.abort();
}
} else {
if (process.env.NODE_ENV !== "development") {
abortControllersRef.current.forEach((controller) => controller.abort());
}
abortControllersRef.current.clear();
}
}, []);

const fetchData = useCallback(
async (payload: any, manualRequestId?: string) => {
const requestId = manualRequestId || nanoid();
if (abortControllersRef.current.has(requestId)) {
abortControllersRef.current.get(requestId)?.abort();
cancelRequest(requestId);
}

const abortController = new AbortController();
Expand All @@ -31,18 +45,8 @@ export const useNetworkRequest = (
abortControllersRef.current.delete(requestId);
}
},
[fn],
[cancelRequest, fn],
);

const cancelRequest = useCallback((requestId?: string) => {
if (requestId) {
abortControllersRef.current.get(requestId)?.abort();
abortControllersRef.current.delete(requestId);
} else {
abortControllersRef.current.forEach((controller) => controller.abort());
abortControllersRef.current.clear();
}
}, []);

return [fetchData, cancelRequest];
};

0 comments on commit 20753b2

Please sign in to comment.