UNPKG

@shaaz1000/rn-storage

Version:

A comprehensive storage solution for React Native with encryption, caching, and offline sync

112 lines 3.89 kB
"use strict"; // src/core/offlineSync.ts var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const netinfo_1 = __importDefault(require("@react-native-community/netinfo")); const storageManager_1 = __importDefault(require("./storageManager")); class OfflineSync { constructor() { this.syncQueue = []; this.syncInterval = null; this.networkStatus = { isConnected: false }; this.storage = storageManager_1.default.getInstance(); this.syncConfig = { syncInterval: 5000, retryAttempts: 3 }; this.initializeNetworkListener(); } static getInstance() { if (!OfflineSync.instance) { OfflineSync.instance = new OfflineSync(); } return OfflineSync.instance; } configure(config) { this.syncConfig = { ...this.syncConfig, ...config }; this.startSyncInterval(); } async initializeNetworkListener() { netinfo_1.default.addEventListener(state => { var _a; const wasOffline = !this.networkStatus.isConnected; this.networkStatus.isConnected = (_a = state.isConnected) !== null && _a !== void 0 ? _a : false; if (wasOffline && state.isConnected) { this.syncQueuedItems(); } }); } startSyncInterval() { if (this.syncInterval) { clearInterval(this.syncInterval); } this.syncInterval = setInterval(() => { if (this.networkStatus.isConnected) { this.syncQueuedItems(); } }, this.syncConfig.syncInterval); } async queueOperation(operation) { this.syncQueue.push(operation); await this.storage.setItem('__syncQueue', this.syncQueue); if (this.networkStatus.isConnected) { await this.syncQueuedItems(); } } async syncQueuedItems() { if (this.syncQueue.length === 0) return; let retryCount = 0; let success = false; while (retryCount < (this.syncConfig.retryAttempts || 3) && !success) { try { // Process each queued item for (const item of this.syncQueue) { await this.processSyncItem(item); } // Clear queue after successful sync this.syncQueue = []; await this.storage.setItem('__syncQueue', this.syncQueue); success = true; this.networkStatus.lastSyncTime = Date.now(); if (this.syncConfig.onSyncComplete) { this.syncConfig.onSyncComplete(true); } } catch (error) { retryCount++; if (retryCount === this.syncConfig.retryAttempts && this.syncConfig.onSyncError) { this.syncConfig.onSyncError(error); } } } } async processSyncItem(item) { // Implement your sync logic here // This is where you'd make API calls to your backend switch (item.operation) { case 'set': // await yourApi.set(item.key, item.value); break; case 'remove': // await yourApi.remove(item.key); break; } } async getLastSyncTime() { return this.networkStatus.lastSyncTime; } async getPendingOperations() { return [...this.syncQueue]; } stopSync() { if (this.syncInterval) { clearInterval(this.syncInterval); this.syncInterval = null; } } } exports.default = OfflineSync; //# sourceMappingURL=offlineSync.js.map