From 39d1344883862bfc385128be88820a63e4745e62 Mon Sep 17 00:00:00 2001 From: amit-s19 Date: Thu, 12 Sep 2024 16:58:50 +0530 Subject: [PATCH] Reverting Tirth's changes --- package.json | 2 +- src/data-sync-context.tsx | 2 +- src/hooks/useNetworkStatus.ts | 2 +- src/index.tsx | 199 +++++----------------------------- 4 files changed, 33 insertions(+), 172 deletions(-) diff --git a/package.json b/package.json index f123486..465aa34 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "0.1.90", + "version": "0.1.92", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", diff --git a/src/data-sync-context.tsx b/src/data-sync-context.tsx index d4bf26b..b677107 100644 --- a/src/data-sync-context.tsx +++ b/src/data-sync-context.tsx @@ -2,4 +2,4 @@ import { createContext } from "react"; type DataSyncContextType = any; -export const DataSyncContext = createContext(null); +export const DataSyncContext = createContext(null); \ No newline at end of file diff --git a/src/hooks/useNetworkStatus.ts b/src/hooks/useNetworkStatus.ts index f8e6dbe..964799d 100644 --- a/src/hooks/useNetworkStatus.ts +++ b/src/hooks/useNetworkStatus.ts @@ -26,4 +26,4 @@ const useNetworkStatus = () => { return isConnected; }; -export default useNetworkStatus; +export default useNetworkStatus; \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index 10e3915..1d47dd4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -6,19 +6,17 @@ import React, { useContext, useEffect, useState, - useRef, + useRef } from 'react'; import { DataSyncContext } from './data-sync-context'; import axios from 'axios'; import localForage from 'localforage'; -import { omit } from 'underscore'; -//@ts-ignore -const api = axios?.create(); +import { omit } from 'underscore'; +const api = axios.create(); const RETRY_LIMIT = 0; const RETRY_DELAY_MS = 1000; const API_REQUESTS_STORAGE_KEY = 'apiRequests'; -const OFFLINE_RESPONSES_STORAGE_KEY = 'offlineResponses'; import { generateUuid, @@ -34,24 +32,10 @@ import useNetworkStatus from './hooks/useNetworkStatus'; // const hasWindow = () => { // return window && typeof window !== 'undefined'; // }; -interface ApiRequest { - id: string; - type?: string; - url: string; - method: string; - data?: any | ((previousResponses: any[]) => Promise); - isFormdata?: boolean; - retryCount?: number; - dependsOn?: string; -} - type ConfigType = { isFormdata?: boolean; maxRetry?: number; - executionOrder?: string[]; - sequentialProcessing?: boolean; }; - export const OfflineSyncProvider: FC<{ children: ReactElement; render?: (status: { isOffline?: boolean; isOnline: boolean }) => ReactNode; @@ -59,10 +43,10 @@ export const OfflineSyncProvider: FC<{ onCallback?: (data: any) => void; toastConfig?: any; config?: ConfigType; -}> = ({ children, render, onStatusChange, onCallback, config }) => { +}> = ({ children, render, onStatusChange, onCallback }) => { // Manage state for data, offline status, and online status const [data, setData] = useState>({}); - const isSyncing = useRef(false); + const isSyncing = useRef(); const [isOnline, setIsOnline] = useState( window?.navigator?.onLine ?? true ); @@ -77,6 +61,7 @@ export const OfflineSyncProvider: FC<{ handleOnline(); } else { handleOffline(); + } } }, [isConnected]); @@ -116,7 +101,7 @@ export const OfflineSyncProvider: FC<{ const saveRequestToOfflineStorage = async (apiConfig: any) => { try { - const storedRequests: any = + const storedRequests: Array = (await localForage.getItem(API_REQUESTS_STORAGE_KEY)) || []; console.log('perform stored', { req: storedRequests, @@ -125,17 +110,10 @@ export const OfflineSyncProvider: FC<{ if (apiConfig?.isFormdata && apiConfig?.data instanceof FormData) { // console.log({ apiConfig }) const newData = await _formDataToObject(apiConfig.data); - storedRequests.push( - omit( - { ...apiConfig, data: newData, type: apiConfig.type }, - 'onSuccess' - ) - ); + storedRequests.push(omit({ ...apiConfig, data: newData }, 'onSuccess')); } else { console.log('Saving request normally'); - storedRequests.push( - omit({ ...apiConfig, type: apiConfig.type }, 'onSuccess') - ); + storedRequests.push(omit({ ...apiConfig }, 'onSuccess')); } console.log('perform forage after:', { storedRequests }); const result = await localForage.setItem( @@ -148,74 +126,30 @@ export const OfflineSyncProvider: FC<{ } }; - const saveResponseToOfflineStorage = async (type: string, response: any) => { - try { - const storedResponses: Record = - (await localForage.getItem(OFFLINE_RESPONSES_STORAGE_KEY)) || {}; - if (!storedResponses[type]) { - storedResponses[type] = []; - } - storedResponses[type].push(response); - await localForage.setItem(OFFLINE_RESPONSES_STORAGE_KEY, storedResponses); - } catch (error) { - console.error('Error saving response to offline storage:', error); - } - }; - - const getOfflineResponses = async (type: string) => { - try { - const storedResponses: Record = - (await localForage.getItem(OFFLINE_RESPONSES_STORAGE_KEY)) || {}; - return storedResponses[type] || []; - } catch (error) { - console.error('Error getting offline responses:', error); - return []; - } - }; - // Function to perform the actual API request and handle retries const performRequest = async (config: any): Promise => { - console.log('Inside performRequest'); + console.log("Inside performRequest") try { - let requestData = config.data; - if (typeof requestData === 'function') { - const dependencyResponses = config.dependsOn - ? await getOfflineResponses(config.dependsOn) - : []; - requestData = await requestData(dependencyResponses); - } - let response; - if (config?.isFormdata && !(requestData instanceof FormData)) { - const updateConfig = { ...config, data: objectToFormData(requestData) }; + if (config?.isFormdata && !(config?.data instanceof FormData)) { + const updateConfig = { ...config, data: objectToFormData(config.data) }; response = await api.request(updateConfig); } else { - response = await api.request({ ...config, data: requestData }); - } - - if (config.type) { - await saveResponseToOfflineStorage(config.type, response.data); + response = await api.request(config); } onCallback && onCallback({ config, data: response, sendRequest }); return response.data; } catch (error) { console.log('packageError', { error }); - console.log('Inside performRequest error: ', { - rc: config.retryCount, - RETRY_LIMIT, - }); - if ((config.retryCount ?? 0) < RETRY_LIMIT) { + console.log("Inside performRequest error: ", { rc: config.retryCount, RETRY_LIMIT }) + if (config.retryCount < RETRY_LIMIT) { await new Promise(resolve => setTimeout(resolve, RETRY_DELAY_MS)); - if (config.retryCount === undefined) { - config.retryCount = 1; - } else { - config.retryCount++; - } + config.retryCount++; return performRequest(config); } else { // Retry limit reached, save the request to offline storage - console.log('Saving request to offline storage'); + console.log("Saving request to offline storage"); await saveRequestToOfflineStorage(config); return error; // throw new Error('Exceeded retry limit, request saved for offline sync.'); @@ -234,64 +168,6 @@ export const OfflineSyncProvider: FC<{ } }; - const processRequestsSequentially = async ( - requests: ApiRequest[], - executionOrder?: string[] - ) => { - const results = []; - - if (executionOrder && executionOrder.length > 0) { - const requestsByType: Record = {}; - - for (const request of requests) { - const type = request.type || 'default'; - if (!requestsByType[type]) { - requestsByType[type] = []; - } - requestsByType[type].push(request); - } - - for (const type of executionOrder) { - const typeRequests = requestsByType[type] || []; - for (const request of typeRequests) { - try { - const result = await performRequest(request); - results.push({ request, result }); - } catch (error) { - console.error(`Error processing ${type} request:`, error); - results.push({ request, error }); - } - } - } - - for (const type in requestsByType) { - if (!executionOrder.includes(type)) { - for (const request of requestsByType[type]) { - try { - const result = await performRequest(request); - results.push({ request, result }); - } catch (error) { - console.error(`Error processing ${type} request:`, error); - results.push({ request, error }); - } - } - } - } - } else { - for (const request of requests) { - try { - const result = await performRequest(request); - results.push({ request, result }); - } catch (error) { - console.error(`Error processing request:`, error); - results.push({ request, error }); - } - } - } - - return results; - }; - const syncOfflineRequests = async () => { if (isSyncing.current) { return; @@ -299,44 +175,29 @@ export const OfflineSyncProvider: FC<{ isSyncing.current = true; const storedRequests: any = await getStoredRequests(); if (!storedRequests || storedRequests.length === 0) { - isSyncing.current = false; return; } - console.log('Inside syncOfflineRequests', storedRequests); + console.log("Inside syncOfflineRequests", storedRequests) const requestClone = [...storedRequests]; - - try { - let results; - if (config?.executionOrder) { - results = await processRequestsSequentially( - requestClone, - config.executionOrder - ); - } else if (config?.sequentialProcessing) { - results = await processRequestsSequentially(requestClone); - } else { - results = await Promise.all(requestClone.map(performRequest)); - } - - for (const result of results) { - const request = result.request || result; - const error = result.error; - if (!error) { + for (const request of storedRequests) { + console.log("Inside syncOfflineRequests loop, ", storedRequests) + if (request) { + try { + await performRequest(request); + // Remove the request with a matching id from requestClone const updatedRequests = requestClone.filter( sr => sr.id !== request.id ); requestClone.splice(0, requestClone.length, ...updatedRequests); - } else { - console.error('Failed to process request:', request, error); + } catch (error) { + console.log({ error }); + } finally { + await localForage.setItem(API_REQUESTS_STORAGE_KEY, requestClone); } } - } catch (error) { - console.error('Error in syncOfflineRequests:', error); - } finally { - await localForage.setItem(API_REQUESTS_STORAGE_KEY, requestClone); - isSyncing.current = false; } + isSyncing.current = false; }; return ( @@ -361,4 +222,4 @@ export const OfflineSyncProvider: FC<{ // Custom hook to access offline sync context export const useOfflineSyncContext = () => { return useContext(DataSyncContext); -}; +}; \ No newline at end of file