Skip to content

Commit 66dfca1

Browse files
committed
added ref for state management
1 parent fbe7cc3 commit 66dfca1

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/index.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import React, {
66
useContext,
77
useEffect,
88
useState,
9+
useRef
910
} from 'react';
1011
import { DataSyncContext } from './data-sync-context';
1112
import axios from 'axios';
@@ -45,6 +46,7 @@ export const OfflineSyncProvider: FC<{
4546
}> = ({ children, render, onStatusChange, onCallback }) => {
4647
// Manage state for data, offline status, and online status
4748
const [data, setData] = useState<Record<string, any>>({});
49+
const isSyncing = useRef<boolean>();
4850
const [isOnline, setIsOnline] = useState<boolean>(
4951
window?.navigator?.onLine ?? true
5052
);
@@ -57,7 +59,6 @@ export const OfflineSyncProvider: FC<{
5759
console.log('Network status:', isConnected ? 'Online' : 'Offline');
5860
if (isConnected) {
5961
handleOnline();
60-
6162
} else {
6263
handleOffline();
6364

@@ -168,6 +169,10 @@ export const OfflineSyncProvider: FC<{
168169
};
169170

170171
const syncOfflineRequests = async () => {
172+
if (isSyncing.current) {
173+
return;
174+
}
175+
isSyncing.current = true;
171176
const storedRequests: any = await getStoredRequests();
172177
if (!storedRequests || storedRequests.length === 0) {
173178
return;
@@ -192,6 +197,7 @@ export const OfflineSyncProvider: FC<{
192197
}
193198
}
194199
}
200+
isSyncing.current = false;
195201
};
196202

197203
return (

0 commit comments

Comments
 (0)