Skip to content

Commit 60a7928

Browse files
committed
Add usesyncqueries hook from expo dev tools
1 parent b130ae2 commit 60a7928

File tree

6 files changed

+439
-18
lines changed

6 files changed

+439
-18
lines changed

package-lock.json

Lines changed: 13 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,23 @@
1717
"url": "https://github.com/LovesWorking/react-query-external-sync"
1818
},
1919
"keywords": [
20-
"react-query",
20+
"expo",
21+
"devtools",
2122
"tanstack",
22-
"state management",
23-
"dev tools",
24-
"react native",
25-
"QA",
26-
"debugging"
23+
"query",
24+
"react-query",
25+
"react-native",
26+
"expo-react-native",
27+
"expo-react-native-tanstack-query-devtools",
28+
"tanstack-query",
29+
"tanstack-query-devtools",
30+
"tanstack-query-devtools-expo",
31+
"tanstack-query-devtools-expo-react-native",
32+
"tanstack-query-devtools-expo-plugin",
33+
"tanstack-query-devtools-expo-plugin-react-native",
34+
"tanstack-query-devtools-expo-plugin-webui",
35+
"tanstack-query-devtools-expo-plugin-webui-react-native",
36+
"React-Query-Dev-Tools"
2737
],
2838
"author": "LovesWorking (https://github.com/LovesWorking)",
2939
"license": "MIT",
@@ -32,10 +42,11 @@
3242
"socket.io-client": "^4.7.4"
3343
},
3444
"peerDependencies": {
35-
"@tanstack/react-query": "^5.17.19",
45+
"@tanstack/react-query": "^4.0.0 || ^5.0.0",
3646
"react": "^18"
3747
},
3848
"devDependencies": {
49+
"@tanstack/react-query": "^5.66.9",
3950
"@babel/core": "^7.23.9",
4051
"@babel/preset-env": "^7.23.9",
4152
"@babel/preset-react": "^7.23.3",

src/new-sync/hydration.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import type {
2+
DefaultError,
3+
MutationOptions,
4+
QueryOptions,
5+
Query,
6+
QueryClient,
7+
Mutation,
8+
} from "@tanstack/react-query";
9+
10+
import {
11+
DehydratedMutation,
12+
DehydratedQuery,
13+
DehydratedState,
14+
ObserverState,
15+
} from "./types";
16+
type TransformerFn = (data: any) => any;
17+
18+
export function Dehydrate(client: QueryClient): DehydratedState {
19+
const mutations = client
20+
.getMutationCache()
21+
.getAll()
22+
.flatMap((mutation) => [dehydrateMutation(mutation)]);
23+
24+
const queries = client
25+
.getQueryCache()
26+
.getAll()
27+
.flatMap((query) => [dehydrateQuery(query)]);
28+
29+
return { mutations, queries };
30+
}
31+
export interface DehydrateOptions {
32+
serializeData?: TransformerFn;
33+
shouldDehydrateMutation?: (mutation: Mutation) => boolean;
34+
shouldDehydrateQuery?: (query: Query) => boolean;
35+
shouldRedactErrors?: (error: unknown) => boolean;
36+
}
37+
38+
export interface HydrateOptions {
39+
defaultOptions?: {
40+
deserializeData?: TransformerFn;
41+
queries?: QueryOptions;
42+
mutations?: MutationOptions<unknown, DefaultError, unknown, unknown>;
43+
};
44+
}
45+
46+
function dehydrateMutation(mutation: Mutation): DehydratedMutation {
47+
return {
48+
mutationId: mutation.mutationId,
49+
mutationKey: mutation.options.mutationKey,
50+
state: mutation.state,
51+
...(mutation.options.scope && { scope: mutation.options.scope }),
52+
...(mutation.meta && { meta: mutation.meta }),
53+
};
54+
}
55+
56+
function dehydrateQuery(query: Query): DehydratedQuery {
57+
// Extract observer states
58+
const observerStates: ObserverState[] = query.observers.map((observer) => ({
59+
queryHash: query.queryHash,
60+
options: observer.options,
61+
// Remove queryFn from observer options to prevent not being able to capture fetch action
62+
queryFn: undefined,
63+
}));
64+
65+
return {
66+
state: {
67+
...query.state,
68+
...(query.state.data !== undefined && {
69+
data: query.state.data,
70+
}),
71+
},
72+
queryKey: query.queryKey,
73+
queryHash: query.queryHash,
74+
...(query.meta && { meta: query.meta }),
75+
observers: observerStates,
76+
};
77+
}

src/new-sync/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export let useSyncQueries: typeof import("./useSyncQueries").useSyncQueries;
2+
// @ts-ignore process.env.NODE_ENV is defined by metro transform plugins
3+
if (process.env.NODE_ENV !== "production") {
4+
useSyncQueries = require("./useSyncQueries").useSyncQueries;
5+
} else {
6+
useSyncQueries = () => ({
7+
isConnected: false,
8+
});
9+
}

src/new-sync/types.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {
2+
DefaultError,
3+
MutationMeta,
4+
MutationScope,
5+
MutationState,
6+
QueryKey,
7+
QueryMeta,
8+
QueryObserverOptions,
9+
QueryState,
10+
MutationKey,
11+
} from "@tanstack/react-query";
12+
// Define a simplified version of DehydratedState that both versions can work with
13+
export interface SimpleDehydratedState {
14+
mutations: any[];
15+
queries: any[];
16+
}
17+
18+
export interface SyncMessage {
19+
type: "dehydrated-state";
20+
state: DehydratedState;
21+
isOnlineManagerOnline: boolean;
22+
}
23+
24+
export interface DehydratedState {
25+
mutations: DehydratedMutation[];
26+
queries: DehydratedQuery[];
27+
}
28+
29+
export interface DehydratedMutation {
30+
mutationId: number;
31+
mutationKey?: MutationKey;
32+
state: MutationState;
33+
meta?: MutationMeta;
34+
scope?: MutationScope;
35+
}
36+
export interface DehydratedQuery {
37+
queryHash: string;
38+
queryKey: QueryKey;
39+
state: QueryState;
40+
promise?: Promise<unknown>;
41+
meta?: QueryMeta;
42+
observers: ObserverState[];
43+
}
44+
export interface ObserverState<
45+
TQueryFnData = unknown,
46+
TError = DefaultError,
47+
TData = TQueryFnData,
48+
TQueryData = TQueryFnData,
49+
TQueryKey extends QueryKey = QueryKey
50+
> {
51+
queryHash: string;
52+
options: QueryObserverOptions<
53+
TQueryFnData,
54+
TError,
55+
TData,
56+
TQueryData,
57+
TQueryKey
58+
>;
59+
}

0 commit comments

Comments
 (0)