Skip to content

Commit

Permalink
Merge pull request #977 from the-hideout/better-tt-update
Browse files Browse the repository at this point in the history
Opitmize/reduce Tarkov Tracker API calls
  • Loading branch information
Razzmatazzz authored Sep 2, 2024
2 parents aae1ca3 + ab50567 commit 69e83de
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
55 changes: 48 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ function App() {
const dispatch = useDispatch();
const retrievedTarkovTrackerToken = useRef(false);
const tarkovTrackerProgressInterval = useRef(false);
const tarkovTrackerUpdatePending = useRef(false);
const tabHasFocus = useRef(true);

if (connectToId) {
dispatch(enableConnection());
Expand All @@ -140,17 +142,56 @@ function App() {
(state) => state.settings[state.settings.gameMode].tarkovTrackerAPIKey,
);

const updateTarkovTrackerData = useCallback(() => {
tarkovTrackerUpdatePending.current = false;
retrievedTarkovTrackerToken.current = tarkovTrackerAPIKey;
dispatch(fetchTarkovTrackerProgress(tarkovTrackerAPIKey));
}, [dispatch, tarkovTrackerAPIKey]);

const scheduleTarkovTrackerUpdate = useCallback(() => {
clearInterval(tarkovTrackerProgressInterval.current);
tarkovTrackerProgressInterval.current = setInterval(() => {
if (!tabHasFocus.current) {
// window doesn't have focus, so postpone the update until it does
tarkovTrackerUpdatePending.current = true;
return;
}
updateTarkovTrackerData();
}, 1000 * 60 * 5);
}, [updateTarkovTrackerData]);

// monitor window focus for Tarkov Tracker updates
useEffect(() => {
const handleFocus = () => {
tabHasFocus.current = true;
if (!tarkovTrackerUpdatePending.current) {
return;
}
scheduleTarkovTrackerUpdate();
updateTarkovTrackerData();
};

const handleBlur = () => {
tabHasFocus.current = false;
};

window.addEventListener('focus', handleFocus);
window.addEventListener('blur', handleBlur);

// Clean up
return () => {
window.removeEventListener('focus', handleFocus);
window.removeEventListener('blur', handleBlur);
};
}, [scheduleTarkovTrackerUpdate, updateTarkovTrackerData]);

useEffect(() => {
if (!tarkovTrackerProgressInterval.current && useTarkovTracker) {
tarkovTrackerProgressInterval.current = setInterval(() => {
retrievedTarkovTrackerToken.current = tarkovTrackerAPIKey;
dispatch(fetchTarkovTrackerProgress(tarkovTrackerAPIKey));
}, 1000 * 60 * 5);
scheduleTarkovTrackerUpdate();
}

if (useTarkovTracker && progressStatus !== 'loading' && retrievedTarkovTrackerToken.current !== tarkovTrackerAPIKey) {
retrievedTarkovTrackerToken.current = tarkovTrackerAPIKey;
dispatch(fetchTarkovTrackerProgress(tarkovTrackerAPIKey));
updateTarkovTrackerData();
}

if (tarkovTrackerProgressInterval.current && !useTarkovTracker) {
Expand All @@ -162,7 +203,7 @@ function App() {
clearInterval(tarkovTrackerProgressInterval.current);
tarkovTrackerProgressInterval.current = false;
};
}, [progressStatus, dispatch, tarkovTrackerAPIKey, useTarkovTracker, tarkovTrackerProgressInterval, retrievedTarkovTrackerToken]);
}, [progressStatus, scheduleTarkovTrackerUpdate, updateTarkovTrackerData, tarkovTrackerAPIKey, useTarkovTracker]);

useEffect(() => {
const handleDisplayMessage = (rawMessage) => {
Expand Down
1 change: 1 addition & 0 deletions src/features/settings/settingsSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export const selectAllTraders = createSelector([selectSettings], (settings) => {
mechanic: settings[settings.gameMode].mechanic,
ragman: settings[settings.gameMode].ragman,
jaeger: settings[settings.gameMode].jaeger,
ref: settings[settings.gameMode].ref,
};
});

Expand Down
11 changes: 5 additions & 6 deletions src/pages/settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function Settings() {
const hideDogtagBarters = useSelector((state) => state.settings[state.settings.gameMode].hideDogtagBarters);
const gameMode = useSelector((state) => state.settings.gameMode);

const refs = {
const stationRefs = {
'bitcoin-farm': useRef(null),
'booze-generator': useRef(null),
'christmas-tree': useRef(null),
Expand Down Expand Up @@ -104,11 +104,11 @@ function Settings() {
useEffect(() => {
if (useTarkovTracker) {
for (const stationKey in allStations) {
if (!refs[stationKey]) {
if (!stationRefs[stationKey]) {
continue;
}

refs[stationKey].current?.setValue({
stationRefs[stationKey].current?.setValue({
value: allStations[stationKey],
label: allStations[stationKey]
? allStations[stationKey].toString()
Expand Down Expand Up @@ -230,13 +230,13 @@ function Settings() {
<div className="settings-group-wrapper">
<h2>{t('Traders')}</h2>
{traders.map((trader) => {

return (
<StationSkillTraderSetting
key={trader.normalizedName}
label={trader.name}
type="trader"
stateKey={trader.normalizedName}
ref={refs[trader.normalizedName]}
/>
);
})}
Expand All @@ -250,7 +250,7 @@ function Settings() {
label={station.name}
type="station"
stateKey={station.normalizedName}
ref={refs[station.normalizedName]}
ref={stationRefs[station.normalizedName]}
isDisabled={useTarkovTracker}
image={station.imageLink}
/>
Expand All @@ -265,7 +265,6 @@ function Settings() {
key={skillKey}
type="skill"
stateKey={skillKey}
ref={refs[skillKey]}
/>
);
})}
Expand Down

0 comments on commit 69e83de

Please sign in to comment.