Skip to content

Commit 768ef94

Browse files
committed
add clear mutation and query cache
1 parent bea8146 commit 768ef94

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

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)