From 66dfca1f09d71ae74ef6be6f34850277f012c11e Mon Sep 17 00:00:00 2001 From: amit-s19 Date: Tue, 18 Jun 2024 16:25:42 +0530 Subject: [PATCH] added ref for state management --- src/index.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/index.tsx b/src/index.tsx index e11ef50..3803a7f 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -6,6 +6,7 @@ import React, { useContext, useEffect, useState, + useRef } from 'react'; import { DataSyncContext } from './data-sync-context'; import axios from 'axios'; @@ -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>({}); + const isSyncing = useRef(); const [isOnline, setIsOnline] = useState( window?.navigator?.onLine ?? true ); @@ -57,7 +59,6 @@ export const OfflineSyncProvider: FC<{ console.log('Network status:', isConnected ? 'Online' : 'Offline'); if (isConnected) { handleOnline(); - } else { handleOffline(); @@ -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; @@ -192,6 +197,7 @@ export const OfflineSyncProvider: FC<{ } } } + isSyncing.current = false; }; return (