UNPKG

react-native-devtools-sync

Version:

A tool for syncing React Query state to an external Dev Tools

99 lines 3.87 kB
/** * Platform and storage utilities that work across different environments (React Native, web, etc.) */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; let customStorageImplementation = null; // Try to detect if we're in a React Native environment export const isReactNative = () => { try { return (typeof navigator !== "undefined" && navigator.product === "ReactNative"); } catch (e) { return false; } }; /** * Get platform-specific URL for socket connection * On Android emulator, we need to replace localhost with 10.0.2.2 */ export const getPlatformSpecificURL = (baseUrl, platform) => { try { const url = new URL(baseUrl); // For Android emulator, replace hostname with 10.0.2.2 if (platform === "android" && (url.hostname === "localhost" || url.hostname === "127.0.0.1")) { url.hostname = "10.0.2.2"; return url.toString(); } // For other platforms, use as provided return baseUrl; } catch (e) { console.warn("Error getting platform-specific URL:", e); return baseUrl; } }; // Storage implementation export const getStorage = () => { // Return user-defined storage if available if (customStorageImplementation) { return customStorageImplementation; } // Try to use React Native AsyncStorage if available if (isReactNative()) { try { // Dynamic import to avoid bundling issues const AsyncStorage = // eslint-disable-next-line @typescript-eslint/no-var-requires require("@react-native-async-storage/async-storage").default; return AsyncStorage; } catch (e) { console.warn("Failed to import AsyncStorage from react-native:", e); } } // Fallback to browser localStorage with an async wrapper if (typeof localStorage !== "undefined") { return { getItem: (key) => __awaiter(void 0, void 0, void 0, function* () { return localStorage.getItem(key); }), setItem: (key, value) => __awaiter(void 0, void 0, void 0, function* () { localStorage.setItem(key, value); }), removeItem: (key) => __awaiter(void 0, void 0, void 0, function* () { localStorage.removeItem(key); }), }; } // Memory fallback if nothing else is available console.warn("No persistent storage available, using in-memory storage"); const memoryStorage = {}; return { getItem: (key) => __awaiter(void 0, void 0, void 0, function* () { return memoryStorage[key] || null; }), setItem: (key, value) => __awaiter(void 0, void 0, void 0, function* () { memoryStorage[key] = value; }), removeItem: (key) => __awaiter(void 0, void 0, void 0, function* () { delete memoryStorage[key]; }), }; }; /** * Set a custom storage implementation * Use this if you need to provide your own storage solution */ export const setCustomStorage = (storage) => { customStorageImplementation = storage; }; //# sourceMappingURL=platformUtils.js.map