-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Labels
Description
As of the now, the number of API fetches happening inside the hook for all the methods i.e. userInfo, getRepositories and their respective sub-methods - are really high.
I have some ideas-as-solution to this issue, which will help reduce the API calls and improve overall dependency for all methods.
- One function to rule them all: Let's create a single function for all our API calls.
const githubRequest = async (endpoint, options = {}) => {
// Code for all the API calls
};- We're calling the API a lot. Let's cache some responses - Caching can be a good option for solve this issue.
const cachedData = {};
const getCachedOrFetch = async (key, fetchFn) => {
if (cachedData[key]) return cachedData[key];
const data = await fetchFn();
cachedData[key] = data;
return data;
};- Sometimes GitHub might be slow - We can have timeouts to avoid delayed responses and UI renders.
const timeoutPromise = (ms) => new Promise((_, reject) => {
setTimeout(() => reject(new Error('Request timed out')), ms);
});
const githubRequest = async (endpoint, options = {}) => {
return Promise.race([
actualRequest(endpoint, options),
timeoutPromise(5000) // 5 second timeout
]);
};