Skip to content

Commit

Permalink
added ref for state management
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-s19 committed Jun 18, 2024
1 parent fbe7cc3 commit 66dfca1
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, {
useContext,
useEffect,
useState,
useRef
} from 'react';
import { DataSyncContext } from './data-sync-context';
import axios from 'axios';
Expand Down Expand Up @@ -45,6 +46,7 @@ export const OfflineSyncProvider: FC<{
}> = ({ children, render, onStatusChange, onCallback }) => {
// Manage state for data, offline status, and online status
const [data, setData] = useState<Record<string, any>>({});
const isSyncing = useRef<boolean>();
const [isOnline, setIsOnline] = useState<boolean>(
window?.navigator?.onLine ?? true
);
Expand All @@ -57,7 +59,6 @@ export const OfflineSyncProvider: FC<{
console.log('Network status:', isConnected ? 'Online' : 'Offline');
if (isConnected) {
handleOnline();

} else {
handleOffline();

Expand Down Expand Up @@ -168,6 +169,10 @@ export const OfflineSyncProvider: FC<{
};

const syncOfflineRequests = async () => {
if (isSyncing.current) {
return;
}
isSyncing.current = true;
const storedRequests: any = await getStoredRequests();
if (!storedRequests || storedRequests.length === 0) {
return;
Expand All @@ -192,6 +197,7 @@ export const OfflineSyncProvider: FC<{
}
}
}
isSyncing.current = false;
};

return (
Expand Down

0 comments on commit 66dfca1

Please sign in to comment.