Skip to content

Commit

Permalink
Fix rate limit issue and add interval for fetching
Browse files Browse the repository at this point in the history
latest data
  • Loading branch information
codad5 committed Nov 29, 2023
1 parent cd42f31 commit 9077b31
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
42 changes: 37 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,52 @@ import { Box, Center, Heading, Image, Link, Text, Flex, IconButton, Highlight }
import { useEffect } from 'react';
import { useRecoilState } from 'recoil';
import { FaGithub } from "react-icons/fa";
import { platformLatestData } from './states';
import { platformLatestData, requestCount } from './states';
import { getLatestVersionDataFOrThisPlatform } from './libs/helper';
import DownloadButton from './components/DownloadButton';

function App() {
const [platformLatestDataInfo, setPlatformLatestData] = useRecoilState(platformLatestData);
const [rateLimit, setRateLimit] = useRecoilState(requestCount);

useEffect(() => {
if(platformLatestDataInfo) return;
getLatestVersionDataFOrThisPlatform().then((data) => {
setPlatformLatestData(data);
})
console.log('platformLatestDataInfo', platformLatestDataInfo);
const interval = setInterval(() => {
if (platformLatestDataInfo || rateLimit > 5) {
clearInterval(interval);
return;
}
getLatestVersionDataFOrThisPlatform().then((data) => {
setPlatformLatestData(data);
}).catch((err) => {
console.log(err);
setPlatformLatestData(null);
}).finally(() => {
setRateLimit((prev) => prev + 1);
console.log('rateLimit', rateLimit);
})
}, 3000);
if(platformLatestDataInfo || rateLimit > 5) clearInterval(interval);
}, [platformLatestDataInfo])


useEffect(() => {
console.log('rateLimit changed', rateLimit);
if (rateLimit <= 5 || platformLatestDataInfo) return;
setPlatformLatestData({
platform: 'other',
version: '0.0.0',
url: 'https://github.com/coad5/google-task-tauri/releases',
date: new Date().toISOString(),
})
setTimeout(() => {
setRateLimit(0);
}, 1000 * 60 * 60 * 24);
}, [rateLimit])




return (
<div className="">
<Box w="100%" p={2} h="10vh">
Expand Down
2 changes: 1 addition & 1 deletion src/libs/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export async function getLatestJson(): Promise<githubLatestReleaseData|null> {
console.log("json from local storage ", latestJson);
return fetch(API_URL).then((response) => {
// check response status
console.log(response);
if (response.status !== 200) return null;
const res = response.json()
console.log(res);
localStorage.setItem("latestJson", JSON.stringify(res));
return res
});
Expand Down
13 changes: 13 additions & 0 deletions src/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export const platformLatestData = atom<PlatformData | null>({
default: null,
});

export const requestCount = atom<number>({
key: 'requestCount',
default: 0,
});


export const platformLatestDataSelector = selector<PlatformData | null>({
key: 'platformLatestDataSelector',
Expand All @@ -21,4 +26,12 @@ export const lastestVersion = selector<string | null>({
const platformData = get(platformLatestData);
return platformData?.version ?? null;
},
});

export const requestCountSelector = selector<number>({
key: 'requestCountSelector',
get: ({ get }) => {
const count = get(requestCount);
return count;
},
});

0 comments on commit 9077b31

Please sign in to comment.