Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/react-query-external-sync/useSyncQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@ export function useSyncQueriesExternal({
);

// ==========================================================
// Subscribe to query changes and sync to dashboard
// Reusable function to sync state changes to dashboard
// ==========================================================
const unsubscribe = queryClient.getQueryCache().subscribe(() => {
const syncStateToDesktop = () => {
if (!deviceId) {
log(`${logPrefix} No persistent device ID found`, enableLogs, "warn");
return;
Expand All @@ -433,7 +433,17 @@ export function useSyncQueriesExternal({

// Send message to dashboard
socket.emit("query-sync", syncMessage);
});
};

// ==========================================================
// Subscribe to query cache changes and sync to dashboard
// ==========================================================
const queryCacheSubscription = queryClient.getQueryCache().subscribe(syncStateToDesktop);

// ==========================================================
// Subscribe to mutation cache changes and sync to dashboard
// ==========================================================
const mutationCacheSubscription = queryClient.getMutationCache().subscribe(syncStateToDesktop);

// ==========================================================
// Cleanup function to unsubscribe from all events
Expand All @@ -443,7 +453,8 @@ export function useSyncQueriesExternal({
queryActionSubscription?.off();
initialStateSubscription?.off();
onlineManagerSubscription?.off();
unsubscribe();
queryCacheSubscription();
mutationCacheSubscription();
};
}, [queryClient, socket, deviceName, isConnected, deviceId, enableLogs]);

Expand Down