Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions apps/roadrunner-view/src/hooks/useSimulationSessionData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,37 @@ export const useSimulationSessionData = () => {
}, [isDataLoaded, bufferNum]);

useEffect(() => {
fetchBatch();
const doFetch = () => {
if (!document.hidden) {
fetchBatch();
}
};

doFetch();

const fetchTimer = window.setInterval(fetchBatch, 2500);
return () => window.clearInterval(fetchTimer);
const fetchTimer = window.setInterval(doFetch, 2500);

const handleVisibilityChange = () => {
if (!document.hidden) {
doFetch();
}
};

document.addEventListener('visibilitychange', handleVisibilityChange);

return () => {
window.clearInterval(fetchTimer);
document.removeEventListener('visibilitychange', handleVisibilityChange);
};
}, [fetchBatch]);

// Clear map cache when data source toggles
useEffect(() => {
simulationSessionMapRef.current.clear();
setIsDataLoaded(false);
fetchBatch();
if (!document.hidden) {
fetchBatch();
}
}, [dataSource, fetchBatch]);

return {
Expand Down
26 changes: 21 additions & 5 deletions apps/roadrunner-view/src/hooks/useVehicleData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,28 @@ export const useVehicleData = ({
// If in playback mode, fetch pages every 2.5s. Live is fetched every 250 ms.
var fetchInterval = playbackOffset === 0 ? 250 : 2500;

fetchBatchRef.current();
const doFetch = () => {
if (!document.hidden) {
fetchBatchRef.current();
}
};

doFetch();

const fetchTimer = window.setInterval(doFetch, fetchInterval);

const handleVisibilityChange = () => {
if (!document.hidden) {
doFetch();
}
};

document.addEventListener('visibilitychange', handleVisibilityChange);

const fetchTimer = window.setInterval(() => {
fetchBatchRef.current();
}, fetchInterval);
return () => window.clearInterval(fetchTimer);
return () => {
window.clearInterval(fetchTimer);
document.removeEventListener('visibilitychange', handleVisibilityChange);
};
}, [playbackOffset]);

// Run the Playback Engine (High Frequency)
Expand Down
Loading