@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.
@@ -0,0 +1,24 @@
1
+ import { useEffect } from 'react';
2
+ import { TanStackQueryPluginClient } from '../shared/messaging';
3
+ import { useQueryClient } from '@tanstack/react-query';
4
+ import { hydrateQueryClient } from '../shared/hydrate';
5
+
6
+ export const useHandleSyncMessages = (
7
+ client: TanStackQueryPluginClient | null
8
+ ) => {
9
+ const queryClient = useQueryClient();
10
+
11
+ useEffect(() => {
12
+ if (!client) {
13
+ return;
14
+ }
15
+
16
+ const subscription = client.onMessage('sync-data', ({ data }) => {
17
+ hydrateQueryClient(queryClient, data);
18
+ });
19
+
20
+ return () => {
21
+ subscription.remove();
22
+ };
23
+ }, [client, queryClient]);
24
+ };
@@ -0,0 +1,55 @@
1
+ import { useQueryClient } from '@tanstack/react-query';
2
+ import { useEffect } from 'react';
3
+ import { TanStackQueryPluginClient } from '../shared/messaging';
4
+
5
+ export const useSyncDevToolsEvents = (
6
+ client: TanStackQueryPluginClient | null
7
+ ) => {
8
+ const queryClient = useQueryClient();
9
+
10
+ useEffect(() => {
11
+ if (!client) {
12
+ return;
13
+ }
14
+
15
+ const handleEvent = (event: Event) => {
16
+ const detail = (event as CustomEvent).detail;
17
+ const { type, queryHash } = detail;
18
+
19
+ // Get the query from cache if we have a hash
20
+ const query = queryHash
21
+ ? queryClient.getQueryCache().get(queryHash)
22
+ : undefined;
23
+ if (
24
+ !query &&
25
+ type !== 'CLEAR_QUERY_CACHE' &&
26
+ type !== 'CLEAR_MUTATION_CACHE'
27
+ ) {
28
+ return;
29
+ }
30
+
31
+ // Special handling for cache clear actions which don't require a query
32
+ if (type === 'CLEAR_QUERY_CACHE' || type === 'CLEAR_MUTATION_CACHE') {
33
+ client.send('devtools-action', {
34
+ type,
35
+ queryHash: '',
36
+ });
37
+ return;
38
+ }
39
+
40
+ // For all other actions, ensure we have a valid query
41
+ if (!query || !queryHash) {
42
+ return;
43
+ }
44
+
45
+ client.send('devtools-action', {
46
+ type,
47
+ queryHash: query.queryHash,
48
+ });
49
+ };
50
+
51
+ window.addEventListener('@tanstack/query-devtools-event', handleEvent);
52
+ return () =>
53
+ window.removeEventListener('@tanstack/query-devtools-event', handleEvent);
54
+ }, [client]);
55
+ };
@@ -0,0 +1,14 @@
1
+ import { useEffect } from 'react';
2
+ import { TanStackQueryPluginClient } from '../shared/messaging';
3
+
4
+ export const useSyncInitialData = (
5
+ client: TanStackQueryPluginClient | null
6
+ ) => {
7
+ useEffect(() => {
8
+ if (!client) {
9
+ return;
10
+ }
11
+
12
+ client.send('request-initial-data', {});
13
+ }, [client]);
14
+ };