Skip to content

Commit d35f27d

Browse files
authored
Merge pull request #20 from ElielC/main
feat(platformUtils): add a prop to expecify if it`s a real device
2 parents 350b1c6 + 8db7662 commit d35f27d

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ The `useSyncQueriesExternal` hook accepts the following options:
133133
| `deviceId` | string | Yes | Unique identifier for your device |
134134
| `extraDeviceInfo` | object | No | Additional device metadata to display in DevTools |
135135
| `enableLogs` | boolean | No | Enable console logging for debugging (default: false) |
136+
| `isDevice` | boolean | No | Set to true if this is a device (default: false) |
136137
| `envVariables` | object | No | Environment variables to sync with DevTools |
137138
| `mmkvStorage` | MmkvStorage | No | MMKV storage instance for real-time monitoring |
138139
| `asyncStorage` | AsyncStorage | No | AsyncStorage instance for polling-based monitoring |

src/react-query-external-sync/platformUtils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ export const isReactNative = (): boolean => {
4545
*/
4646
export const getPlatformSpecificURL = (
4747
baseUrl: string,
48-
platform: PlatformOS
48+
platform: PlatformOS,
49+
isDevice: boolean
4950
): string => {
5051
try {
5152
const url = new URL(baseUrl);
5253

5354
// For Android emulator, replace hostname with 10.0.2.2
5455
if (
56+
!isDevice &&
5557
platform === "android" &&
5658
(url.hostname === "localhost" || url.hostname === "127.0.0.1")
5759
) {

src/react-query-external-sync/useMySocket.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ interface Props {
1616
* @default false
1717
*/
1818
enableLogs?: boolean;
19+
/**
20+
* Whether the app is running on a physical device or an emulator/simulator
21+
* This can affect how the socket URL is constructed, especially on Android
22+
* @default false
23+
*/
24+
isDevice?: boolean // Whether the app is running on a physical device
1925
}
2026

2127
/**
@@ -43,6 +49,7 @@ export function useMySocket({
4349
envVariables,
4450
platform,
4551
enableLogs = false,
52+
isDevice = false
4653
}: Props) {
4754
const socketRef = useRef<Socket | null>(null);
4855
const [socket, setSocket] = useState<Socket | null>(null);
@@ -86,7 +93,7 @@ export function useMySocket({
8693
};
8794

8895
// Get the platform-specific URL
89-
const platformUrl = getPlatformSpecificURL(socketURL, platform);
96+
const platformUrl = getPlatformSpecificURL(socketURL, platform, isDevice);
9097
currentSocketURL = platformUrl;
9198

9299
try {
@@ -159,7 +166,7 @@ export function useMySocket({
159166
// Update the socket URL when socketURL changes
160167
useEffect(() => {
161168
// Get platform-specific URL for the new socketURL
162-
const platformUrl = getPlatformSpecificURL(socketURL, platform);
169+
const platformUrl = getPlatformSpecificURL(socketURL, platform, isDevice);
163170

164171
// Compare with last known URL to avoid direct property access
165172
if (socketRef.current && currentSocketURL !== platformUrl && persistentDeviceId) {

src/react-query-external-sync/useSyncQueriesExternal.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ interface useSyncQueriesExternalProps {
146146
* @default false
147147
*/
148148
enableLogs?: boolean;
149+
/**
150+
* Whether the app is running on a physical device or an emulator/simulator
151+
* This can affect how the socket URL is constructed, especially on Android
152+
* @default false
153+
*/
154+
isDevice?: boolean; // Whether the app is running on a physical device
149155

150156
/**
151157
* Storage instances for different storage types
@@ -312,6 +318,7 @@ export function useSyncQueriesExternal({
312318
platform,
313319
deviceId,
314320
enableLogs = false,
321+
isDevice = false,
315322
storage,
316323
mmkvStorage,
317324
asyncStorage,
@@ -439,6 +446,7 @@ export function useSyncQueriesExternal({
439446
envVariables: mergedEnvVariables,
440447
platform,
441448
enableLogs,
449+
isDevice,
442450
});
443451

444452
useEffect(() => {

0 commit comments

Comments
 (0)