@rozenite/tanstack-query-plugin 1.0.0-alpha.4 → 1.0.0-alpha.6
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-B737zydj.js → CXEL7IU7-B6IQn3K5.js} +1 -1
- package/dist/assets/{tanstack-query-DSN-k5Qe.js → tanstack-query-PB2x2Kty.js} +16 -16
- package/dist/react-native.cjs +1 -1
- package/dist/react-native.d.ts +1 -5
- package/dist/react-native.js +1 -1
- package/dist/rozenite.config.d.ts +7 -0
- package/dist/rozenite.json +1 -1
- package/dist/src/react-native/useHandleDevToolsMessages.d.ts +3 -0
- package/dist/src/react-native/useHandleInitialData.d.ts +3 -0
- package/dist/src/react-native/useSyncTanStackCache.d.ts +3 -0
- package/dist/src/react-native/useTanStackQueryDevTools.d.ts +2 -0
- package/dist/src/shared/dehydrate.d.ts +6 -0
- package/dist/src/shared/hydrate.d.ts +10 -0
- package/dist/src/shared/messaging.d.ts +34 -0
- package/dist/src/shared/types.d.ts +25 -0
- package/dist/src/shared/useSyncOnlineStatus.d.ts +2 -0
- package/dist/src/ui/tanstack-query.d.ts +1 -0
- package/dist/src/ui/useHandleSyncMessages.d.ts +2 -0
- package/dist/src/ui/useSyncDevToolsEvents.d.ts +2 -0
- package/dist/src/ui/useSyncInitialData.d.ts +2 -0
- package/dist/tanstack-query.html +1 -1
- package/dist/useTanstackQueryDevTools.cjs +1 -0
- package/dist/useTanstackQueryDevTools.js +312 -0
- package/package.json +13 -12
- package/src/react-native/useHandleDevToolsMessages.ts +2 -3
- package/src/react-native/useHandleInitialData.ts +2 -3
- package/src/react-native/useSyncTanStackCache.ts +223 -10
- package/src/react-native/{useTanStackQueryDevTools.ts → useTanstackQueryDevTools.ts} +5 -4
- package/src/shared/dehydrate.ts +8 -6
- package/src/shared/hydrate.ts +218 -74
- package/src/shared/messaging.ts +36 -1
- package/src/shared/types.ts +5 -0
- package/src/shared/useSyncOnlineStatus.ts +2 -3
- package/src/ui/tanstack-query.tsx +1 -1
- package/src/ui/useHandleSyncMessages.ts +45 -3
- package/dist/useTanStackQueryDevTools.js +0 -1
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
2
|
import { TanStackQueryPluginClient } from '../shared/messaging';
|
|
3
3
|
import { useQueryClient } from '@tanstack/react-query';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
hydrateQueryClient,
|
|
6
|
+
applyQueryEvent,
|
|
7
|
+
applyMutationEvent,
|
|
8
|
+
applyQueryObserverEvent,
|
|
9
|
+
} from '../shared/hydrate';
|
|
5
10
|
|
|
6
11
|
export const useHandleSyncMessages = (
|
|
7
12
|
client: TanStackQueryPluginClient | null
|
|
@@ -13,12 +18,49 @@ export const useHandleSyncMessages = (
|
|
|
13
18
|
return;
|
|
14
19
|
}
|
|
15
20
|
|
|
16
|
-
const
|
|
21
|
+
const querySubscription = client.onMessage(
|
|
22
|
+
'sync-query-event',
|
|
23
|
+
(message) => {
|
|
24
|
+
const { type, data } = message;
|
|
25
|
+
|
|
26
|
+
if (type === 'added' || type === 'removed') {
|
|
27
|
+
applyQueryEvent(queryClient, type, data);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (type === 'updated') {
|
|
32
|
+
const action = 'action' in message ? message.action : undefined;
|
|
33
|
+
applyQueryEvent(queryClient, type, data, action);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (
|
|
38
|
+
type === 'observerAdded' ||
|
|
39
|
+
type === 'observerRemoved' ||
|
|
40
|
+
type === 'observerOptionsUpdated'
|
|
41
|
+
) {
|
|
42
|
+
applyQueryObserverEvent(queryClient, data);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const mutationSubscription = client.onMessage(
|
|
49
|
+
'sync-mutation-event',
|
|
50
|
+
({ type, data }) => {
|
|
51
|
+
applyMutationEvent(queryClient, type, data);
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// Keep the full sync for initial data requests
|
|
56
|
+
const fullSyncSubscription = client.onMessage('sync-data', ({ data }) => {
|
|
17
57
|
hydrateQueryClient(queryClient, data);
|
|
18
58
|
});
|
|
19
59
|
|
|
20
60
|
return () => {
|
|
21
|
-
|
|
61
|
+
querySubscription.remove();
|
|
62
|
+
mutationSubscription.remove();
|
|
63
|
+
fullSyncSubscription.remove();
|
|
22
64
|
};
|
|
23
65
|
}, [client, queryClient]);
|
|
24
66
|
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@rozenite/plugin-bridge"),i=require("react"),o=require("@tanstack/react-query"),h=e=>{i.useEffect(()=>{if(!e)return;const s=o.onlineManager.subscribe(r=>{}),n=e.onMessage("online-status-changed",({online:r})=>{console.log("changed from ",o.onlineManager.isOnline(),"to",r)});return()=>{s(),n.remove()}},[e])},y=e=>{const s=o.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 a=t.options,c=new Error("Unknown error from devtools");t.setState({status:"error",error:c,fetchMeta:{...t.state.fetchMeta,__previousQueryOptions:a}});break}case"RESTORE_ERROR":{s.resetQueries(t);break}case"TRIGGER_LOADING":{if(!t)return;const a=t.options;t.fetch({...a,queryFn:()=>new Promise(()=>{}),gcTime:-1}),t.setState({data:void 0,status:"pending",fetchMeta:{...t.state.fetchMeta,__previousQueryOptions:a}});break}case"RESTORE_LOADING":{const a=t.state,c=t.state.fetchMeta?t.state.fetchMeta.__previousQueryOptions:null;t.cancel({silent:!0}),t.setState({...a,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)}),g=e=>{const s=o.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])},b=e=>{const s=o.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])},v=()=>{const e=l.useRozeniteDevToolsClient({pluginId:"@rozenite/tanstack-query-plugin"});h(e),y(e),g(e),b(e)};exports.useTanStackQueryDevTools=v;
|