Skip to content

Commit

Permalink
Add polling updateOnlineStatus every 20 sec if lastUpdateCallTime did…
Browse files Browse the repository at this point in the history
… before 20 sec
  • Loading branch information
gkatrakazas committed Nov 18, 2024
1 parent b95d17b commit c1d74a4
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/context/StatusContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,38 @@ export const StatusProvider = ({ children }: { children: React.ReactNode }) => {
};
}, [isOnline]);

// Polling logic when online

useEffect(() => {
let pollingInterval: NodeJS.Timeout | null = null;

const startOnlinePolling = () => {
pollingInterval = setInterval(() => {
const now = Date.now();

// Check if it's been more than 20 seconds since the last update call
if (now - lastUpdateCallTime.current > 20000) {
console.log('Polling updateOnlineStatus while online...');
updateOnlineStatus(false); // Pass `false` to indicate this is a periodic check
} else {
console.log('Skipping online polling: Called too recently');
}
}, 20000); // Poll every 20 seconds
};

if (isOnline) {
startOnlinePolling();
} else if (pollingInterval) {
clearInterval(pollingInterval);
}

return () => {
if (pollingInterval) {
clearInterval(pollingInterval);
}
};
}, [isOnline]);

navigator.serviceWorker.addEventListener('message', (event) => {
if (event.data && event.data.type === 'NEW_CONTENT_AVAILABLE') {
const isWindowHidden = document.hidden;
Expand Down

0 comments on commit c1d74a4

Please sign in to comment.