From 1bf96608092ff2997ffac677875e9c20ead3abbc Mon Sep 17 00:00:00 2001 From: Razzmatazz Date: Thu, 29 Aug 2024 14:10:56 -0500 Subject: [PATCH] improve tarkov tracker api update --- src/App.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/App.js b/src/App.js index 47e0c27a..cf10f9ad 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,5 @@ /* eslint-disable no-restricted-globals */ -import React, { useEffect, useCallback, Suspense } from 'react'; +import React, { useEffect, useCallback, useRef, Suspense } from 'react'; import { Routes, Route, useNavigate, Navigate } from 'react-router-dom'; import { Helmet } from 'react-helmet'; import { useDispatch, useSelector } from 'react-redux'; @@ -87,7 +87,6 @@ const socketServer = 'wss://socket.tarkov.dev'; //const socketServer = 'ws://localhost:8080'; let socket = false; -let tarkovTrackerProgressInterval = false; loadPolyfills(); @@ -122,6 +121,8 @@ function App() { const controlId = useSelector((state) => state.sockets.controlId); let navigate = useNavigate(); const dispatch = useDispatch(); + const retrievedTarkovTrackerToken = useRef(false); + const tarkovTrackerProgressInterval = useRef(false); if (connectToId) { dispatch(enableConnection()); @@ -140,26 +141,28 @@ function App() { ); useEffect(() => { - if (useTarkovTracker && progressStatus !== 'loading' && !tarkovTrackerProgressInterval) { - dispatch(fetchTarkovTrackerProgress(tarkovTrackerAPIKey)); - } - - if (!tarkovTrackerProgressInterval && useTarkovTracker) { - tarkovTrackerProgressInterval = setInterval(() => { + if (!tarkovTrackerProgressInterval.current && useTarkovTracker) { + tarkovTrackerProgressInterval.current = setInterval(() => { + retrievedTarkovTrackerToken.current = tarkovTrackerAPIKey; dispatch(fetchTarkovTrackerProgress(tarkovTrackerAPIKey)); }, 1000 * 60 * 5); } - if (tarkovTrackerProgressInterval && !useTarkovTracker) { - clearInterval(tarkovTrackerProgressInterval); - tarkovTrackerProgressInterval = false; + if (useTarkovTracker && progressStatus !== 'loading' && retrievedTarkovTrackerToken.current !== tarkovTrackerAPIKey) { + retrievedTarkovTrackerToken.current = tarkovTrackerAPIKey; + dispatch(fetchTarkovTrackerProgress(tarkovTrackerAPIKey)); + } + + if (tarkovTrackerProgressInterval.current && !useTarkovTracker) { + clearInterval(tarkovTrackerProgressInterval.current); + tarkovTrackerProgressInterval.current = false; } return () => { - clearInterval(tarkovTrackerProgressInterval); - tarkovTrackerProgressInterval = false; + clearInterval(tarkovTrackerProgressInterval.current); + tarkovTrackerProgressInterval.current = false; }; - }, [progressStatus, dispatch, tarkovTrackerAPIKey, useTarkovTracker]); + }, [progressStatus, dispatch, tarkovTrackerAPIKey, useTarkovTracker, tarkovTrackerProgressInterval, retrievedTarkovTrackerToken]); useEffect(() => { const handleDisplayMessage = (rawMessage) => {