Skip to content

Commit 3b0e094

Browse files
authored
Merge pull request #14 from LovesWorking/fix-external-device-info-crash
Fix external device info crash
2 parents acd14f0 + 193be16 commit 3b0e094

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function dehydrateMutation(mutation: Mutation): DehydratedMutation {
4949
mutationId: mutation.mutationId,
5050
mutationKey: mutation.options.mutationKey,
5151
state: mutation.state,
52+
gcTime: mutation.gcTime,
5253
...(mutation.options.scope && { scope: mutation.options.scope }),
5354
...(mutation.meta && { meta: mutation.meta }),
5455
};
@@ -72,6 +73,7 @@ function dehydrateQuery(query: Query): DehydratedQuery {
7273
},
7374
queryKey: query.queryKey,
7475
queryHash: query.queryHash,
76+
gcTime: query.gcTime,
7577
...(query.meta && { meta: query.meta }),
7678
observers: observerStates,
7779
};

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface DehydratedMutation {
3333
state: MutationState;
3434
meta?: MutationMeta;
3535
scope?: MutationScope;
36+
gcTime?: number;
3637
}
3738
export interface DehydratedQuery {
3839
queryHash: string;
@@ -41,6 +42,7 @@ export interface DehydratedQuery {
4142
promise?: Promise<unknown>;
4243
meta?: QueryMeta;
4344
observers: ObserverState[];
45+
gcTime?: number;
4446
}
4547
export interface ObserverState<
4648
TQueryFnData = unknown,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ export function useMySocket({
109109
deviceName,
110110
deviceId: persistentDeviceId,
111111
platform,
112-
extraDeviceInfo: JSON.stringify(extraDeviceInfo),
112+
...(extraDeviceInfo && Object.keys(extraDeviceInfo).length > 0
113+
? { extraDeviceInfo: JSON.stringify(extraDeviceInfo) }
114+
: {}),
113115
},
114116
reconnection: false,
115117
transports: ["websocket"], // Prefer websocket transport for React Native

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { useEffect, useRef } from "react";
22
import type { QueryKey } from "@tanstack/query-core";
33
import { onlineManager, QueryClient } from "@tanstack/react-query";
44

5+
import { log } from "./utils/logger";
56
import { Dehydrate } from "./hydration";
7+
import { PlatformOS } from "./platformUtils";
68
import { SyncMessage } from "./types";
79
import { useMySocket } from "./useMySocket";
8-
import { PlatformOS } from "./platformUtils";
9-
import { log } from "./utils/logger";
1010

1111
/**
1212
* Query actions that can be performed on a query.
@@ -29,7 +29,9 @@ type QueryActions =
2929
| "ACTION-ONLINE-MANAGER-ONLINE" // Set online manager to online
3030
| "ACTION-ONLINE-MANAGER-OFFLINE" // Set online manager to offline
3131
// Internal action
32-
| "success"; // Internal success action
32+
| "success" // Internal success action
33+
| "ACTION-CLEAR-MUTATION-CACHE" // Clear the mutation cache
34+
| "ACTION-CLEAR-QUERY-CACHE"; // Clear the query cache
3335

3436
/**
3537
* Message structure for query actions between dashboard and devices
@@ -270,6 +272,17 @@ export function useSyncQueriesExternal({
270272
`${logPrefix} Received query action: ${action} for query ${queryHash}`,
271273
enableLogs
272274
);
275+
// If action is clear cache do the action here before moving on
276+
if (action === "ACTION-CLEAR-MUTATION-CACHE") {
277+
queryClient.getMutationCache().clear();
278+
log(`${logPrefix} Cleared mutation cache`, enableLogs);
279+
return;
280+
}
281+
if (action === "ACTION-CLEAR-QUERY-CACHE") {
282+
queryClient.getQueryCache().clear();
283+
log(`${logPrefix} Cleared query cache`, enableLogs);
284+
return;
285+
}
273286

274287
const activeQuery = queryClient.getQueryCache().get(queryHash);
275288
if (!activeQuery) {
@@ -440,7 +453,15 @@ export function useSyncQueriesExternal({
440453
onlineManagerSubscription?.off();
441454
unsubscribe();
442455
};
443-
}, [queryClient, socket, deviceName, isConnected, deviceId, enableLogs]);
456+
}, [
457+
queryClient,
458+
socket,
459+
deviceName,
460+
isConnected,
461+
deviceId,
462+
enableLogs,
463+
logPrefix,
464+
]);
444465

445466
return { connect, disconnect, isConnected, socket };
446467
}

0 commit comments

Comments
 (0)