Skip to content

Commit

Permalink
improve tarkov tracker api update
Browse files Browse the repository at this point in the history
  • Loading branch information
Razzmatazzz committed Aug 29, 2024
1 parent 1201f17 commit 1bf9660
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -87,7 +87,6 @@ const socketServer = 'wss://socket.tarkov.dev';
//const socketServer = 'ws://localhost:8080';

let socket = false;
let tarkovTrackerProgressInterval = false;

loadPolyfills();

Expand Down Expand Up @@ -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());
Expand All @@ -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) => {
Expand Down

0 comments on commit 1bf9660

Please sign in to comment.