Skip to content

Commit

Permalink
simplifiy updateOnlineStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
gkatrakazas committed Nov 18, 2024
1 parent 03d8a6a commit 74f0e6b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function App() {
useEffect(() => {
setUrl(window.location.href);
checkForUpdates();
updateOnlineStatus({}, false); // Respect the cooldown
updateOnlineStatus(false); // Respect the cooldown
}, [location])

useEffect(() => {
Expand Down
128 changes: 65 additions & 63 deletions src/context/StatusContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ interface Connectivity {
}

interface StatusContextValue {
isOnline: boolean;
isOnline: boolean | null; // Allow null for initial state
updateAvailable: boolean;
Connectivity: Connectivity;
updateOnlineStatus: ({ internetConnection }?: { internetConnection?: { isConnected: boolean; speed: number } | null }) => Promise<void>;
updateOnlineStatus: (forceCheck?: boolean) => Promise<void>; // Only takes `forceCheck` as an optional parameter
}

const StatusContext = createContext<StatusContextValue>({
isOnline: null,
updateAvailable: false,
Connectivity: { navigatorOnline: null, Internet: null, speed: 0 },
updateOnlineStatus: async () => { },
updateOnlineStatus: async () => { }, // Default to a no-op async function
});

// Function to calculate speed based on RTT (lower RTT means higher speed)
Expand Down Expand Up @@ -66,73 +66,79 @@ export const StatusProvider = ({ children }: { children: React.ReactNode }) => {

const lastUpdateCallTime = React.useRef<number>(0);

const updateOnlineStatus = async (
{ internetConnection = null }: { internetConnection?: { isConnected: boolean; speed: number } | null } = {},
forceCheck = true
) => {
const updateOnlineStatus = async (forceCheck = true) => {

const navigatorOnline = getNavigatorOnlineStatus();
console.log('updateOnlineStatus with force:',forceCheck)

console.log('updateOnlineStatus with force:', forceCheck)
// Get the current time
const now = Date.now();

// If not a forced check and last call was within the last 5 seconds, skip the update
if (!forceCheck && now - lastUpdateCallTime.current < 5000) {
console.log('Skipping updateOnlineStatus: Called too recently');
return;
}

// Update the last call time
lastUpdateCallTime.current = now;
console.log('lastUpdateCallTime',Date.now())

if (navigatorOnline) {
if (internetConnection === null) {
internetConnection = await checkInternetConnection();
console.log('lastUpdateCallTime', Date.now())

const internetConnection = await checkInternetConnection();

setConnectivity((prev) => {
if (
prev.navigatorOnline === navigatorOnline &&
prev.Internet === internetConnection.isConnected &&
prev.speed === internetConnection.speed
) {
return prev; // No changes, return previous state to prevent rerender
}

setConnectivity((prev) => {
if (
prev.navigatorOnline === navigatorOnline &&
prev.Internet === internetConnection.isConnected &&
prev.speed === internetConnection.speed
) {
return prev; // No changes, return previous state to prevent rerender
}
return {
...prev,
navigatorOnline,
Internet: internetConnection.isConnected,
speed: internetConnection.speed,
};
});

setIsOnline((prev) => {
if (prev === internetConnection.isConnected) {
return prev; // No change in `isOnline`
}
return internetConnection.isConnected;
});
} else {
setConnectivity((prev) => {
if (
prev.navigatorOnline === false &&
prev.Internet === false &&
prev.speed === 0
) {
return prev; // No changes, return previous state
return {
...prev,
navigatorOnline,
Internet: internetConnection.isConnected,
speed: internetConnection.speed,
};
});

setIsOnline((prev) => {
if (prev === internetConnection.isConnected) {
return prev; // No change in `isOnline`
}
return internetConnection.isConnected;
});
};

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');
}
return {
navigatorOnline: false,
Internet: false,
speed: 0,
};
});

setIsOnline((prev) => (prev === false ? prev : false)); // Update only if different
}, 20000); // Poll every 20 seconds
};

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


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

useEffect(() => {

Expand All @@ -153,12 +159,8 @@ export const StatusProvider = ({ children }: { children: React.ReactNode }) => {
const startPolling = () => {
pollingInterval = setInterval(async () => {
console.log('Polling backend connection...');
const internetConnection = await checkInternetConnection();
if (internetConnection.isConnected) {
clearInterval(pollingInterval!);
updateOnlineStatus({ internetConnection });
}
}, 10000); // Poll every 10 seconds
updateOnlineStatus();
}, 5000); // Poll every 10 seconds
};

if (!isOnline) {
Expand Down

0 comments on commit 74f0e6b

Please sign in to comment.