@rozenite/tanstack-query-plugin 1.0.0-alpha.0 → 1.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/CXEL7IU7-BCih8F8R.js +1172 -0
- package/dist/assets/tanstack-query-By949uAm.js +48 -0
- package/dist/react-native.cjs +1 -1
- package/dist/react-native.js +155 -83
- package/dist/tanstack-query.html +1 -1
- package/package.json +2 -2
- package/src/react-native/useHandleDevToolsMessages.ts +120 -0
- package/src/react-native/useHandleInitialData.ts +25 -0
- package/src/react-native/useSyncTanStackCache.ts +33 -0
- package/src/react-native/useTanStackQueryDevTools.ts +14 -208
- package/src/shared/dehydrate.ts +40 -0
- package/src/shared/hydrate.ts +106 -0
- package/src/shared/messaging.ts +19 -0
- package/src/shared/types.ts +43 -0
- package/src/shared/useSyncOnlineStatus.ts +30 -0
- package/src/ui/tanstack-query.tsx +21 -184
- package/src/ui/useHandleSyncMessages.ts +24 -0
- package/src/ui/useSyncDevToolsEvents.ts +55 -0
- package/src/ui/useSyncInitialData.ts +14 -0
- package/dist/assets/tanstack-query-CXp_Psxe.js +0 -48
package/dist/react-native.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@rozenite/plugin-bridge"),i=require("react"),a=require("@tanstack/react-query"),y=e=>{i.useEffect(()=>{if(!e)return;const s=a.onlineManager.subscribe(r=>{}),n=e.onMessage("online-status-changed",({online:r})=>{console.log("changed from ",a.onlineManager.isOnline(),"to",r)});return()=>{s(),n.remove()}},[e])},h=e=>{const s=a.useQueryClient();i.useEffect(()=>{if(!e)return;const n=e.onMessage("devtools-action",({type:r,queryHash:u})=>{const t=s.getQueryCache().get(u);if(!t){console.warn(`No active query found for hash: ${u}`);return}switch(r){case"TRIGGER_ERROR":{const o=t.options,c=new Error("Unknown error from devtools");t.setState({status:"error",error:c,fetchMeta:{...t.state.fetchMeta,__previousQueryOptions:o}});break}case"RESTORE_ERROR":{s.resetQueries(t);break}case"TRIGGER_LOADING":{if(!t)return;const o=t.options;t.fetch({...o,queryFn:()=>new Promise(()=>{}),gcTime:-1}),t.setState({data:void 0,status:"pending",fetchMeta:{...t.state.fetchMeta,__previousQueryOptions:o}});break}case"RESTORE_LOADING":{const o=t.state,c=t.state.fetchMeta?t.state.fetchMeta.__previousQueryOptions:null;t.cancel({silent:!0}),t.setState({...o,fetchStatus:"idle",fetchMeta:null}),c&&t.fetch(c);break}case"RESET":{s.resetQueries(t);break}case"REMOVE":{s.removeQueries(t);break}case"REFETCH":{t.fetch().catch(()=>{});break}case"INVALIDATE":{s.invalidateQueries(t);break}default:console.warn(`Unknown devtools action: ${r}`)}});return()=>{n.remove()}},[e])},f=e=>{const s=e.observers.map(n=>({queryHash:e.queryHash,options:n.options}));return{state:e.state,queryKey:e.queryKey,queryHash:e.queryHash,observers:s}},p=e=>({mutationId:e.mutationId,state:e.state,options:e.options}),d=e=>({queries:e.getQueryCache().getAll().map(f),mutations:e.getMutationCache().getAll().map(p)}),v=e=>{const s=a.useQueryClient();i.useEffect(()=>{if(!e)return;const n=()=>{const t=d(s);e.send("sync-data",{data:t})},r=s.getMutationCache().subscribe(n),u=s.getQueryCache().subscribe(n);return()=>{r(),u()}},[e,s])},g=e=>{const s=a.useQueryClient();i.useEffect(()=>{if(!e)return;const n=e.onMessage("request-initial-data",()=>{const r=d(s);e.send("sync-data",{data:r})});return()=>{n.remove()}},[e,s])},b=()=>{const e=l.useRozeniteDevToolsClient({pluginId:"@rozenite/tanstack-query-plugin"});y(e),h(e),v(e),g(e)};exports.useTanStackQueryDevTools=void 0;process.env.NODE_ENV!=="production"?exports.useTanStackQueryDevTools=b:exports.useTanStackQueryDevTools=()=>({isConnected:!1});
|
package/dist/react-native.js
CHANGED
|
@@ -1,88 +1,160 @@
|
|
|
1
|
-
import { useRozeniteDevToolsClient as
|
|
2
|
-
import { useEffect as
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
});
|
|
14
|
-
});
|
|
15
|
-
return () => n.remove();
|
|
16
|
-
}, [e, u]);
|
|
17
|
-
const o = i(/* @__PURE__ */ new Set()), a = V((n) => {
|
|
18
|
-
const O = (c) => u.getQueryCache().getAll().find((E) => E.queryHash === c);
|
|
19
|
-
((c) => {
|
|
20
|
-
var f;
|
|
21
|
-
const { type: E, queryHash: t, metadata: T, requestId: s } = c;
|
|
22
|
-
if (E === "REFETCH" && t && ((f = O(t)) == null || f.fetch(), s && (o.current.add(s), e == null || e.send("DEVICE_TO_DEVTOOLS_ACK", { requestId: s, success: !0 }), o.current.delete(s))), E === "INVALIDATE" && t) {
|
|
23
|
-
const r = O(t);
|
|
24
|
-
r && u.invalidateQueries({ queryKey: r.queryKey }), s && (e == null || e.send("DEVICE_TO_DEVTOOLS_ACK", { requestId: s, success: !0 }));
|
|
25
|
-
}
|
|
26
|
-
if (E === "RESET" && t) {
|
|
27
|
-
const r = O(t);
|
|
28
|
-
r && u.resetQueries({ queryKey: r.queryKey }), s && (e == null || e.send("DEVICE_TO_DEVTOOLS_ACK", { requestId: s, success: !0 }));
|
|
29
|
-
}
|
|
30
|
-
if (E === "REMOVE" && t) {
|
|
31
|
-
const r = O(t);
|
|
32
|
-
r && u.removeQueries({ queryKey: r.queryKey }), s && (e == null || e.send("DEVICE_TO_DEVTOOLS_ACK", { requestId: s, success: !0 }));
|
|
33
|
-
}
|
|
34
|
-
if (E === "TRIGGER_LOADING" && t) {
|
|
35
|
-
const r = O(t);
|
|
36
|
-
r && (r.setState({
|
|
37
|
-
...r.state,
|
|
38
|
-
fetchStatus: "fetching",
|
|
39
|
-
status: "pending",
|
|
40
|
-
error: null
|
|
41
|
-
}), s && (o.current.add(s), e == null || e.send("DEVICE_TO_DEVTOOLS_ACK", { requestId: s, success: !0 }), o.current.delete(s)));
|
|
1
|
+
import { useRozeniteDevToolsClient as p } from "@rozenite/plugin-bridge";
|
|
2
|
+
import { useEffect as i } from "react";
|
|
3
|
+
import { onlineManager as d, useQueryClient as u } from "@tanstack/react-query";
|
|
4
|
+
const f = (e) => {
|
|
5
|
+
i(() => {
|
|
6
|
+
if (!e)
|
|
7
|
+
return;
|
|
8
|
+
const s = d.subscribe((r) => {
|
|
9
|
+
}), n = e.onMessage(
|
|
10
|
+
"online-status-changed",
|
|
11
|
+
({ online: r }) => {
|
|
12
|
+
console.log("changed from ", d.isOnline(), "to", r);
|
|
42
13
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
14
|
+
);
|
|
15
|
+
return () => {
|
|
16
|
+
s(), n.remove();
|
|
17
|
+
};
|
|
18
|
+
}, [e]);
|
|
19
|
+
}, y = (e) => {
|
|
20
|
+
const s = u();
|
|
21
|
+
i(() => {
|
|
22
|
+
if (!e)
|
|
23
|
+
return;
|
|
24
|
+
const n = e.onMessage(
|
|
25
|
+
"devtools-action",
|
|
26
|
+
({ type: r, queryHash: a }) => {
|
|
27
|
+
const t = s.getQueryCache().get(a);
|
|
28
|
+
if (!t) {
|
|
29
|
+
console.warn(`No active query found for hash: ${a}`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
switch (r) {
|
|
33
|
+
case "TRIGGER_ERROR": {
|
|
34
|
+
const o = t.options, c = new Error("Unknown error from devtools");
|
|
35
|
+
t.setState({
|
|
36
|
+
status: "error",
|
|
37
|
+
error: c,
|
|
38
|
+
fetchMeta: {
|
|
39
|
+
...t.state.fetchMeta,
|
|
40
|
+
// @ts-expect-error This does exist
|
|
41
|
+
__previousQueryOptions: o
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case "RESTORE_ERROR": {
|
|
47
|
+
s.resetQueries(t);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case "TRIGGER_LOADING": {
|
|
51
|
+
if (!t) return;
|
|
52
|
+
const o = t.options;
|
|
53
|
+
t.fetch({
|
|
54
|
+
...o,
|
|
55
|
+
queryFn: () => new Promise(() => {
|
|
56
|
+
}),
|
|
57
|
+
gcTime: -1
|
|
58
|
+
}), t.setState({
|
|
59
|
+
data: void 0,
|
|
60
|
+
status: "pending",
|
|
61
|
+
fetchMeta: {
|
|
62
|
+
...t.state.fetchMeta,
|
|
63
|
+
// @ts-expect-error This does exist
|
|
64
|
+
__previousQueryOptions: o
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
case "RESTORE_LOADING": {
|
|
70
|
+
const o = t.state, c = t.state.fetchMeta ? t.state.fetchMeta.__previousQueryOptions : null;
|
|
71
|
+
t.cancel({ silent: !0 }), t.setState({
|
|
72
|
+
...o,
|
|
73
|
+
fetchStatus: "idle",
|
|
74
|
+
fetchMeta: null
|
|
75
|
+
}), c && t.fetch(c);
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
case "RESET": {
|
|
79
|
+
s.resetQueries(t);
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
case "REMOVE": {
|
|
83
|
+
s.removeQueries(t);
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
case "REFETCH": {
|
|
87
|
+
t.fetch().catch(() => {
|
|
88
|
+
});
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
case "INVALIDATE": {
|
|
92
|
+
s.invalidateQueries(t);
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
default:
|
|
96
|
+
console.warn(`Unknown devtools action: ${r}`);
|
|
97
|
+
}
|
|
49
98
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (e)
|
|
79
|
-
return
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
99
|
+
);
|
|
100
|
+
return () => {
|
|
101
|
+
n.remove();
|
|
102
|
+
};
|
|
103
|
+
}, [e]);
|
|
104
|
+
}, v = (e) => {
|
|
105
|
+
const s = e.observers.map(
|
|
106
|
+
(n) => ({
|
|
107
|
+
queryHash: e.queryHash,
|
|
108
|
+
options: n.options
|
|
109
|
+
})
|
|
110
|
+
);
|
|
111
|
+
return {
|
|
112
|
+
state: e.state,
|
|
113
|
+
queryKey: e.queryKey,
|
|
114
|
+
queryHash: e.queryHash,
|
|
115
|
+
observers: s
|
|
116
|
+
};
|
|
117
|
+
}, b = (e) => ({
|
|
118
|
+
mutationId: e.mutationId,
|
|
119
|
+
state: e.state,
|
|
120
|
+
options: e.options
|
|
121
|
+
}), h = (e) => ({
|
|
122
|
+
queries: e.getQueryCache().getAll().map(v),
|
|
123
|
+
mutations: e.getMutationCache().getAll().map(b)
|
|
124
|
+
}), g = (e) => {
|
|
125
|
+
const s = u();
|
|
126
|
+
i(() => {
|
|
127
|
+
if (!e)
|
|
128
|
+
return;
|
|
129
|
+
const n = () => {
|
|
130
|
+
const t = h(s);
|
|
131
|
+
e.send("sync-data", { data: t });
|
|
132
|
+
}, r = s.getMutationCache().subscribe(n), a = s.getQueryCache().subscribe(n);
|
|
133
|
+
return () => {
|
|
134
|
+
r(), a();
|
|
135
|
+
};
|
|
136
|
+
}, [e, s]);
|
|
137
|
+
}, S = (e) => {
|
|
138
|
+
const s = u();
|
|
139
|
+
i(() => {
|
|
140
|
+
if (!e)
|
|
141
|
+
return;
|
|
142
|
+
const n = e.onMessage("request-initial-data", () => {
|
|
143
|
+
const r = h(s);
|
|
144
|
+
e.send("sync-data", { data: r });
|
|
145
|
+
});
|
|
146
|
+
return () => {
|
|
147
|
+
n.remove();
|
|
148
|
+
};
|
|
149
|
+
}, [e, s]);
|
|
150
|
+
}, m = () => {
|
|
151
|
+
const e = p({
|
|
152
|
+
pluginId: "@rozenite/tanstack-query-plugin"
|
|
153
|
+
});
|
|
154
|
+
f(e), y(e), g(e), S(e);
|
|
83
155
|
};
|
|
84
|
-
let
|
|
85
|
-
process.env.NODE_ENV !== "production" ?
|
|
156
|
+
let l;
|
|
157
|
+
process.env.NODE_ENV !== "production" ? l = m : l = () => ({ isConnected: !1 });
|
|
86
158
|
export {
|
|
87
|
-
|
|
159
|
+
l as useTanStackQueryDevTools
|
|
88
160
|
};
|
package/dist/tanstack-query.html
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
<script>
|
|
23
23
|
var __ROZENITE_PANEL__ = true;
|
|
24
24
|
</script>
|
|
25
|
-
<script type="module" crossorigin src="./assets/tanstack-query-
|
|
25
|
+
<script type="module" crossorigin src="./assets/tanstack-query-By949uAm.js"></script>
|
|
26
26
|
</head>
|
|
27
27
|
<body>
|
|
28
28
|
<div id="root"></div>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rozenite/tanstack-query-plugin",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.2",
|
|
4
4
|
"description": "TanStack Query for Rozenite.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/react-native.cjs",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"vite": "^6.0.0",
|
|
16
16
|
"react": "*",
|
|
17
17
|
"react-native": "*",
|
|
18
|
-
"rozenite": "1.0.0-alpha.
|
|
18
|
+
"rozenite": "1.0.0-alpha.0",
|
|
19
19
|
"@rozenite/vite-plugin": "1.0.0-alpha.1"
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
3
|
+
import { TanStackQueryPluginClient } from '../shared/messaging';
|
|
4
|
+
|
|
5
|
+
export const useHandleDevToolsMessages = (
|
|
6
|
+
client: TanStackQueryPluginClient | null
|
|
7
|
+
) => {
|
|
8
|
+
const queryClient = useQueryClient();
|
|
9
|
+
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
if (!client) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const subscription = client.onMessage(
|
|
16
|
+
'devtools-action',
|
|
17
|
+
({ type, queryHash }) => {
|
|
18
|
+
const activeQuery = queryClient.getQueryCache().get(queryHash);
|
|
19
|
+
|
|
20
|
+
if (!activeQuery) {
|
|
21
|
+
console.warn(`No active query found for hash: ${queryHash}`);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
switch (type) {
|
|
26
|
+
case 'TRIGGER_ERROR': {
|
|
27
|
+
const __previousQueryOptions = activeQuery.options;
|
|
28
|
+
const error = new Error('Unknown error from devtools');
|
|
29
|
+
|
|
30
|
+
activeQuery.setState({
|
|
31
|
+
status: 'error',
|
|
32
|
+
error,
|
|
33
|
+
fetchMeta: {
|
|
34
|
+
...activeQuery.state.fetchMeta,
|
|
35
|
+
// @ts-expect-error This does exist
|
|
36
|
+
__previousQueryOptions,
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
case 'RESTORE_ERROR': {
|
|
42
|
+
queryClient.resetQueries(activeQuery);
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case 'TRIGGER_LOADING': {
|
|
46
|
+
if (!activeQuery) return;
|
|
47
|
+
const __previousQueryOptions = activeQuery.options;
|
|
48
|
+
// Trigger a fetch in order to trigger suspense as well.
|
|
49
|
+
activeQuery.fetch({
|
|
50
|
+
...__previousQueryOptions,
|
|
51
|
+
queryFn: () => {
|
|
52
|
+
return new Promise(() => {
|
|
53
|
+
// Never resolve - simulates perpetual loading
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
gcTime: -1,
|
|
57
|
+
});
|
|
58
|
+
activeQuery.setState({
|
|
59
|
+
data: undefined,
|
|
60
|
+
status: 'pending',
|
|
61
|
+
fetchMeta: {
|
|
62
|
+
...activeQuery.state.fetchMeta,
|
|
63
|
+
// @ts-expect-error This does exist
|
|
64
|
+
__previousQueryOptions,
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
case 'RESTORE_LOADING': {
|
|
70
|
+
const previousState = activeQuery.state;
|
|
71
|
+
const previousOptions = activeQuery.state.fetchMeta
|
|
72
|
+
? (
|
|
73
|
+
activeQuery.state.fetchMeta as unknown as {
|
|
74
|
+
__previousQueryOptions: unknown;
|
|
75
|
+
}
|
|
76
|
+
).__previousQueryOptions
|
|
77
|
+
: null;
|
|
78
|
+
|
|
79
|
+
activeQuery.cancel({ silent: true });
|
|
80
|
+
activeQuery.setState({
|
|
81
|
+
...previousState,
|
|
82
|
+
fetchStatus: 'idle',
|
|
83
|
+
fetchMeta: null,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
if (previousOptions) {
|
|
87
|
+
activeQuery.fetch(previousOptions);
|
|
88
|
+
}
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
case 'RESET': {
|
|
92
|
+
queryClient.resetQueries(activeQuery);
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
case 'REMOVE': {
|
|
96
|
+
queryClient.removeQueries(activeQuery);
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
case 'REFETCH': {
|
|
100
|
+
activeQuery.fetch().catch(() => {
|
|
101
|
+
// Ignore errors
|
|
102
|
+
});
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case 'INVALIDATE': {
|
|
106
|
+
queryClient.invalidateQueries(activeQuery);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
default: {
|
|
110
|
+
console.warn(`Unknown devtools action: ${type}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
return () => {
|
|
117
|
+
subscription.remove();
|
|
118
|
+
};
|
|
119
|
+
}, [client]);
|
|
120
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { TanStackQueryPluginClient } from '../shared/messaging';
|
|
3
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
4
|
+
import { dehydrateQueryClient } from '../shared/dehydrate';
|
|
5
|
+
|
|
6
|
+
export const useHandleInitialData = (
|
|
7
|
+
client: TanStackQueryPluginClient | null
|
|
8
|
+
) => {
|
|
9
|
+
const queryClient = useQueryClient();
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if (!client) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const subscription = client.onMessage('request-initial-data', () => {
|
|
17
|
+
const dehydratedState = dehydrateQueryClient(queryClient);
|
|
18
|
+
client.send('sync-data', { data: dehydratedState });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return () => {
|
|
22
|
+
subscription.remove();
|
|
23
|
+
};
|
|
24
|
+
}, [client, queryClient]);
|
|
25
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { useEffect } from 'react';
|
|
3
|
+
import { TanStackQueryPluginClient } from '../shared/messaging';
|
|
4
|
+
import { dehydrateQueryClient } from '../shared/dehydrate';
|
|
5
|
+
|
|
6
|
+
export const useSyncTanStackCache = (
|
|
7
|
+
client: TanStackQueryPluginClient | null
|
|
8
|
+
) => {
|
|
9
|
+
const queryClient = useQueryClient();
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if (!client) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const handler = (): void => {
|
|
17
|
+
const dehydratedState = dehydrateQueryClient(queryClient);
|
|
18
|
+
client.send('sync-data', { data: dehydratedState });
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const mutationCacheSubscription = queryClient
|
|
22
|
+
.getMutationCache()
|
|
23
|
+
.subscribe(handler);
|
|
24
|
+
const queryCacheSubscription = queryClient
|
|
25
|
+
.getQueryCache()
|
|
26
|
+
.subscribe(handler);
|
|
27
|
+
|
|
28
|
+
return () => {
|
|
29
|
+
mutationCacheSubscription();
|
|
30
|
+
queryCacheSubscription();
|
|
31
|
+
};
|
|
32
|
+
}, [client, queryClient]);
|
|
33
|
+
};
|