@rozenite/tanstack-query-plugin 2.1.0 → 2.3.0

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.
Files changed (67) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/devtools/assets/{CXEL7IU7-BWccRGN3.js → CXEL7IU7-D2MCp9XC.js} +1 -1
  3. package/dist/devtools/assets/{tanstack-query-CbF_imft.js → tanstack-query-Bk8rHBM2.js} +12 -12
  4. package/dist/devtools/tanstack-query.html +1 -1
  5. package/dist/react-native/cjs/package.json +3 -0
  6. package/dist/react-native/cjs/react-native.js +12 -0
  7. package/dist/react-native/cjs/src/react-native/agent/tanstack-query-agent.js +376 -0
  8. package/dist/react-native/cjs/src/react-native/agent/useTanStackQueryAgentTools.js +98 -0
  9. package/dist/react-native/cjs/src/react-native/devtools-actions.js +161 -0
  10. package/dist/react-native/cjs/src/react-native/useHandleDevToolsMessages.js +26 -0
  11. package/dist/react-native/cjs/src/react-native/useHandleInitialData.js +26 -0
  12. package/dist/react-native/cjs/src/react-native/useSyncTanStackCache.js +203 -0
  13. package/dist/react-native/cjs/src/react-native/useTanStackQueryDevTools.js +20 -0
  14. package/dist/react-native/cjs/src/shared/agent-tools.js +197 -0
  15. package/dist/react-native/cjs/src/shared/dehydrate.js +35 -0
  16. package/dist/react-native/cjs/src/shared/messaging.js +2 -0
  17. package/dist/react-native/cjs/src/shared/types.js +2 -0
  18. package/dist/react-native/cjs/src/shared/useSyncOnlineStatus.js +23 -0
  19. package/dist/react-native/package.json +3 -0
  20. package/dist/react-native/react-native.d.ts +1 -0
  21. package/dist/react-native/react-native.js +10 -0
  22. package/dist/{sdk/index.d.ts → react-native/src/react-native/agent/tanstack-query-agent.d.ts} +2105 -1939
  23. package/dist/react-native/src/react-native/agent/tanstack-query-agent.js +370 -0
  24. package/dist/react-native/src/react-native/agent/useTanStackQueryAgentTools.d.ts +2 -0
  25. package/dist/react-native/src/react-native/agent/useTanStackQueryAgentTools.js +94 -0
  26. package/dist/react-native/src/react-native/devtools-actions.d.ts +115 -0
  27. package/dist/react-native/src/react-native/devtools-actions.js +157 -0
  28. package/dist/react-native/src/react-native/useHandleDevToolsMessages.d.ts +3 -0
  29. package/dist/react-native/src/react-native/useHandleDevToolsMessages.js +22 -0
  30. package/dist/react-native/src/react-native/useHandleInitialData.d.ts +3 -0
  31. package/dist/react-native/src/react-native/useHandleInitialData.js +22 -0
  32. package/dist/react-native/src/react-native/useSyncTanStackCache.d.ts +3 -0
  33. package/dist/react-native/src/react-native/useSyncTanStackCache.js +196 -0
  34. package/dist/react-native/src/react-native/useTanStackQueryDevTools.d.ts +2 -0
  35. package/dist/react-native/src/react-native/useTanStackQueryDevTools.js +16 -0
  36. package/dist/react-native/src/shared/agent-tools.d.ts +967 -0
  37. package/dist/react-native/src/shared/agent-tools.js +194 -0
  38. package/dist/react-native/src/shared/dehydrate.d.ts +6 -0
  39. package/dist/react-native/src/shared/dehydrate.js +28 -0
  40. package/dist/react-native/src/shared/messaging.d.ts +44 -0
  41. package/dist/react-native/src/shared/messaging.js +1 -0
  42. package/dist/react-native/src/shared/types.d.ts +25 -0
  43. package/dist/react-native/src/shared/types.js +1 -0
  44. package/dist/react-native/src/shared/useSyncOnlineStatus.d.ts +2 -0
  45. package/dist/react-native/src/shared/useSyncOnlineStatus.js +19 -0
  46. package/dist/rozenite.json +1 -1
  47. package/dist/sdk/package.json +3 -0
  48. package/dist/sdk/sdk.d.ts +812 -0
  49. package/dist/sdk/sdk.js +8 -0
  50. package/dist/sdk/src/react-native/devtools-actions.d.ts +115 -0
  51. package/dist/sdk/src/react-native/devtools-actions.js +161 -0
  52. package/dist/sdk/src/shared/agent-tools.d.ts +967 -0
  53. package/dist/sdk/src/shared/agent-tools.js +197 -0
  54. package/dist/sdk/src/shared/types.d.ts +25 -0
  55. package/dist/sdk/src/shared/types.js +2 -0
  56. package/package.json +18 -18
  57. package/react-native.ts +8 -1
  58. package/rozenite.config.ts +1 -0
  59. package/src/__tests__/release-bundle.test.ts +32 -0
  60. package/tsconfig.json +1 -3
  61. package/dist/react-native/chunks/useTanStackQueryDevTools.require.cjs +0 -1
  62. package/dist/react-native/chunks/useTanStackQueryDevTools.require.js +0 -895
  63. package/dist/react-native/index.cjs +0 -1
  64. package/dist/react-native/index.d.ts +0 -7
  65. package/dist/react-native/index.js +0 -6
  66. package/dist/sdk/index.cjs +0 -1
  67. package/dist/sdk/index.js +0 -199
@@ -0,0 +1,157 @@
1
+ const getActiveQuery = (queryClient, queryHash) => {
2
+ if (!queryHash) {
3
+ throw new Error('queryHash is required for this TanStack Query action.');
4
+ }
5
+ const activeQuery = queryClient.getQueryCache().get(queryHash);
6
+ if (!activeQuery) {
7
+ throw new Error(`No active query found for hash: ${queryHash}`);
8
+ }
9
+ return activeQuery;
10
+ };
11
+ export const applyTanStackQueryDevtoolsAction = async (queryClient, input) => {
12
+ switch (input.type) {
13
+ case 'CLEAR_QUERY_CACHE': {
14
+ const queryCountBefore = queryClient.getQueryCache().getAll().length;
15
+ queryClient.getQueryCache().clear();
16
+ return {
17
+ applied: true,
18
+ action: input.type,
19
+ cleared: true,
20
+ queryCountBefore,
21
+ queryCountAfter: queryClient.getQueryCache().getAll().length,
22
+ };
23
+ }
24
+ case 'CLEAR_MUTATION_CACHE': {
25
+ const mutationCountBefore = queryClient.getMutationCache().getAll().length;
26
+ queryClient.getMutationCache().clear();
27
+ return {
28
+ applied: true,
29
+ action: input.type,
30
+ cleared: true,
31
+ mutationCountBefore,
32
+ mutationCountAfter: queryClient.getMutationCache().getAll().length,
33
+ };
34
+ }
35
+ case 'SET_QUERY_DATA': {
36
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
37
+ queryClient.setQueryData(activeQuery.queryKey, input.metadata?.data);
38
+ return {
39
+ applied: true,
40
+ action: input.type,
41
+ queryHash: activeQuery.queryHash,
42
+ };
43
+ }
44
+ case 'TRIGGER_ERROR': {
45
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
46
+ const previousQueryOptions = activeQuery.options;
47
+ const error = new Error('Unknown error from devtools');
48
+ activeQuery.setState({
49
+ status: 'error',
50
+ error,
51
+ fetchMeta: {
52
+ ...activeQuery.state.fetchMeta,
53
+ // @ts-expect-error This does exist
54
+ __previousQueryOptions: previousQueryOptions,
55
+ },
56
+ });
57
+ return {
58
+ applied: true,
59
+ action: input.type,
60
+ queryHash: activeQuery.queryHash,
61
+ };
62
+ }
63
+ case 'RESTORE_ERROR': {
64
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
65
+ await queryClient.resetQueries(activeQuery);
66
+ return {
67
+ applied: true,
68
+ action: input.type,
69
+ queryHash: activeQuery.queryHash,
70
+ };
71
+ }
72
+ case 'TRIGGER_LOADING': {
73
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
74
+ const previousQueryOptions = activeQuery.options;
75
+ void activeQuery.fetch({
76
+ ...previousQueryOptions,
77
+ queryFn: () => new Promise(() => {
78
+ // Never resolve - simulates perpetual loading
79
+ }),
80
+ gcTime: -1,
81
+ });
82
+ activeQuery.setState({
83
+ data: undefined,
84
+ status: 'pending',
85
+ fetchMeta: {
86
+ ...activeQuery.state.fetchMeta,
87
+ // @ts-expect-error This does exist
88
+ __previousQueryOptions: previousQueryOptions,
89
+ },
90
+ });
91
+ return {
92
+ applied: true,
93
+ action: input.type,
94
+ queryHash: activeQuery.queryHash,
95
+ };
96
+ }
97
+ case 'RESTORE_LOADING': {
98
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
99
+ const previousState = activeQuery.state;
100
+ const previousOptions = activeQuery.state.fetchMeta
101
+ ? activeQuery.state.fetchMeta.__previousQueryOptions
102
+ : null;
103
+ activeQuery.cancel({ silent: true });
104
+ activeQuery.setState({
105
+ ...previousState,
106
+ fetchStatus: 'idle',
107
+ fetchMeta: null,
108
+ });
109
+ if (previousOptions) {
110
+ void activeQuery.fetch(previousOptions);
111
+ }
112
+ return {
113
+ applied: true,
114
+ action: input.type,
115
+ queryHash: activeQuery.queryHash,
116
+ };
117
+ }
118
+ case 'RESET': {
119
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
120
+ await queryClient.resetQueries(activeQuery);
121
+ return {
122
+ applied: true,
123
+ action: input.type,
124
+ queryHash: activeQuery.queryHash,
125
+ };
126
+ }
127
+ case 'REMOVE': {
128
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
129
+ queryClient.removeQueries(activeQuery);
130
+ return {
131
+ applied: true,
132
+ action: input.type,
133
+ queryHash: activeQuery.queryHash,
134
+ };
135
+ }
136
+ case 'REFETCH': {
137
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
138
+ await activeQuery.fetch().catch(() => {
139
+ // Ignore errors from refetch in the agent/UI bridge.
140
+ });
141
+ return {
142
+ applied: true,
143
+ action: input.type,
144
+ queryHash: activeQuery.queryHash,
145
+ };
146
+ }
147
+ case 'INVALIDATE': {
148
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
149
+ await queryClient.invalidateQueries(activeQuery);
150
+ return {
151
+ applied: true,
152
+ action: input.type,
153
+ queryHash: activeQuery.queryHash,
154
+ };
155
+ }
156
+ }
157
+ };
@@ -0,0 +1,3 @@
1
+ import { QueryClient } from '@tanstack/react-query';
2
+ import { TanStackQueryPluginClient } from '../shared/messaging';
3
+ export declare const useHandleDevToolsMessages: (queryClient: QueryClient, client: TanStackQueryPluginClient | null) => void;
@@ -0,0 +1,22 @@
1
+ import { useEffect } from 'react';
2
+ import { applyTanStackQueryDevtoolsAction } from './devtools-actions';
3
+ export const useHandleDevToolsMessages = (queryClient, client) => {
4
+ useEffect(() => {
5
+ if (!client) {
6
+ return;
7
+ }
8
+ const subscription = client.onMessage('devtools-action', ({ type, queryHash, metadata }) => {
9
+ void applyTanStackQueryDevtoolsAction(queryClient, {
10
+ type,
11
+ queryHash,
12
+ metadata,
13
+ }).catch((error) => {
14
+ const message = error instanceof Error ? error.message : String(error);
15
+ console.warn(`[Rozenite, tanstack-query-plugin] Failed to apply devtools action "${type}": ${message}`);
16
+ });
17
+ });
18
+ return () => {
19
+ subscription.remove();
20
+ };
21
+ }, [client]);
22
+ };
@@ -0,0 +1,3 @@
1
+ import { TanStackQueryPluginClient } from '../shared/messaging';
2
+ import { QueryClient } from '@tanstack/react-query';
3
+ export declare const useHandleInitialData: (queryClient: QueryClient, client: TanStackQueryPluginClient | null) => void;
@@ -0,0 +1,22 @@
1
+ import { useEffect } from 'react';
2
+ import { dehydrateQueryClient } from '../shared/dehydrate';
3
+ export const useHandleInitialData = (queryClient, client) => {
4
+ useEffect(() => {
5
+ if (!client) {
6
+ return;
7
+ }
8
+ const subscription = client.onMessage('request-initial-data', () => {
9
+ const dehydratedState = dehydrateQueryClient(queryClient);
10
+ client.send('sync-data', { data: dehydratedState });
11
+ });
12
+ // This hook runs last in `useTanStackQueryDevTools`, so by the time the
13
+ // panel reacts to this every handler is listening. The panel is recreated on
14
+ // every app reload and asks for the cache as soon as it boots, which usually
15
+ // beats the app's React tree to the punch; without this its only request is
16
+ // dropped and the panel stays empty until the app is restarted.
17
+ client.send('device-ready', {});
18
+ return () => {
19
+ subscription.remove();
20
+ };
21
+ }, [client, queryClient]);
22
+ };
@@ -0,0 +1,3 @@
1
+ import { QueryClient } from '@tanstack/react-query';
2
+ import { TanStackQueryPluginClient } from '../shared/messaging';
3
+ export declare const useSyncTanStackCache: (queryClient: QueryClient, client: TanStackQueryPluginClient | null) => void;
@@ -0,0 +1,196 @@
1
+ import { useEffect, useMemo, useRef } from 'react';
2
+ import equal from 'fast-deep-equal';
3
+ import { dehydrateQuery, dehydrateMutation, dehydrateObservers } from '../shared/dehydrate';
4
+ export const useSyncTanStackCache = (queryClient, client) => {
5
+ const previousObserversRef = useRef(new Map());
6
+ const handler = useMemo(() => {
7
+ return (event) => {
8
+ if (!client) {
9
+ return;
10
+ }
11
+ if (event.type === 'observerResultsUpdated') {
12
+ // We don't need to sync this type of events.
13
+ return;
14
+ }
15
+ if ('query' in event) {
16
+ const { query, type } = event;
17
+ if (type === 'added' || type === 'removed') {
18
+ if (type === 'removed') {
19
+ // We don't need to store observers for removed queries.
20
+ previousObserversRef.current.delete(query.queryHash);
21
+ }
22
+ const dehydratedQuery = dehydrateQuery(query);
23
+ client.send('sync-query-event', { type, data: dehydratedQuery });
24
+ return;
25
+ }
26
+ if (type === 'updated' && 'action' in event) {
27
+ const action = event.action;
28
+ switch (action.type) {
29
+ case 'fetch': {
30
+ // Only need loading state and fetch metadata
31
+ const fetchData = {
32
+ queryHash: query.queryHash,
33
+ state: {
34
+ status: query.state.status,
35
+ fetchStatus: query.state.fetchStatus,
36
+ fetchMeta: query.state.fetchMeta,
37
+ dataUpdatedAt: query.state.dataUpdatedAt,
38
+ errorUpdatedAt: query.state.errorUpdatedAt,
39
+ },
40
+ };
41
+ client.send('sync-query-event', {
42
+ type: 'updated',
43
+ action: 'fetch',
44
+ data: fetchData,
45
+ });
46
+ break;
47
+ }
48
+ case 'success': {
49
+ // Only need new data and success state
50
+ const successData = {
51
+ queryHash: query.queryHash,
52
+ state: {
53
+ status: query.state.status,
54
+ data: query.state.data,
55
+ dataUpdatedAt: query.state.dataUpdatedAt,
56
+ error: query.state.error,
57
+ errorUpdatedAt: query.state.errorUpdatedAt,
58
+ fetchStatus: query.state.fetchStatus,
59
+ },
60
+ };
61
+ client.send('sync-query-event', {
62
+ type: 'updated',
63
+ action: 'success',
64
+ data: successData,
65
+ });
66
+ break;
67
+ }
68
+ case 'error': {
69
+ // Only need error information
70
+ const errorData = {
71
+ queryHash: query.queryHash,
72
+ state: {
73
+ status: query.state.status,
74
+ error: query.state.error,
75
+ errorUpdatedAt: query.state.errorUpdatedAt,
76
+ fetchStatus: query.state.fetchStatus,
77
+ },
78
+ };
79
+ client.send('sync-query-event', {
80
+ type: 'updated',
81
+ action: 'error',
82
+ data: errorData,
83
+ });
84
+ break;
85
+ }
86
+ case 'setState': {
87
+ // Only need the specific state changes
88
+ const setStateData = {
89
+ queryHash: query.queryHash,
90
+ state: action.state, // Only the changed state parts
91
+ };
92
+ client.send('sync-query-event', {
93
+ type: 'updated',
94
+ action: 'setState',
95
+ data: setStateData,
96
+ });
97
+ break;
98
+ }
99
+ case 'invalidate': {
100
+ // Only need invalidation flag
101
+ const invalidateData = {
102
+ queryHash: query.queryHash,
103
+ state: {
104
+ isInvalidated: query.state.isInvalidated,
105
+ },
106
+ };
107
+ client.send('sync-query-event', {
108
+ type: 'updated',
109
+ action: 'invalidate',
110
+ data: invalidateData,
111
+ });
112
+ break;
113
+ }
114
+ case 'pause': {
115
+ // Need to sync the paused fetch status
116
+ const pauseData = {
117
+ queryHash: query.queryHash,
118
+ state: {
119
+ fetchStatus: query.state.fetchStatus,
120
+ },
121
+ };
122
+ client.send('sync-query-event', {
123
+ type: 'updated',
124
+ action: 'pause',
125
+ data: pauseData,
126
+ });
127
+ break;
128
+ }
129
+ case 'continue': {
130
+ // Need to sync the resumed fetch status
131
+ const continueData = {
132
+ queryHash: query.queryHash,
133
+ state: {
134
+ fetchStatus: query.state.fetchStatus,
135
+ },
136
+ };
137
+ client.send('sync-query-event', {
138
+ type: 'updated',
139
+ action: 'continue',
140
+ data: continueData,
141
+ });
142
+ break;
143
+ }
144
+ default: {
145
+ // Fallback to full data for unknown actions
146
+ const dehydratedQuery = dehydrateQuery(query);
147
+ client.send('sync-query-event', { type, data: dehydratedQuery });
148
+ }
149
+ }
150
+ return;
151
+ }
152
+ if (type === 'observerAdded' ||
153
+ type === 'observerRemoved' ||
154
+ type === 'observerOptionsUpdated') {
155
+ const dehydratedObservers = dehydrateObservers(query);
156
+ const previousObservers = previousObserversRef.current.get(query.queryHash);
157
+ // For observerOptionsUpdated, only send if the observers actually changed
158
+ if (type === 'observerOptionsUpdated' && previousObservers) {
159
+ if (equal(previousObservers, dehydratedObservers)) {
160
+ // No meaningful change, skip sending
161
+ return;
162
+ }
163
+ }
164
+ previousObserversRef.current.set(query.queryHash, dehydratedObservers);
165
+ client.send('sync-query-event', {
166
+ type,
167
+ data: {
168
+ queryHash: query.queryHash,
169
+ observers: dehydratedObservers,
170
+ },
171
+ });
172
+ return;
173
+ }
174
+ }
175
+ const { mutation, type } = event;
176
+ if (mutation) {
177
+ const dehydratedMutation = dehydrateMutation(mutation);
178
+ client.send('sync-mutation-event', {
179
+ type,
180
+ data: dehydratedMutation,
181
+ });
182
+ }
183
+ };
184
+ }, [client]);
185
+ useEffect(() => {
186
+ if (!client) {
187
+ return;
188
+ }
189
+ const mutationCacheSubscription = queryClient.getMutationCache().subscribe(handler);
190
+ const queryCacheSubscription = queryClient.getQueryCache().subscribe(handler);
191
+ return () => {
192
+ mutationCacheSubscription();
193
+ queryCacheSubscription();
194
+ };
195
+ }, [client, queryClient, handler]);
196
+ };
@@ -0,0 +1,2 @@
1
+ import { QueryClient } from '@tanstack/react-query';
2
+ export declare const useTanStackQueryDevTools: (queryClient: QueryClient) => void;
@@ -0,0 +1,16 @@
1
+ import { useRozeniteDevToolsClient } from '@rozenite/plugin-bridge';
2
+ import { useSyncOnlineStatus } from '../shared/useSyncOnlineStatus';
3
+ import { useHandleDevToolsMessages } from './useHandleDevToolsMessages';
4
+ import { useSyncTanStackCache } from './useSyncTanStackCache';
5
+ import { useHandleInitialData } from './useHandleInitialData';
6
+ import { useTanStackQueryAgentTools } from './agent/useTanStackQueryAgentTools';
7
+ export const useTanStackQueryDevTools = (queryClient) => {
8
+ const client = useRozeniteDevToolsClient({
9
+ pluginId: '@rozenite/tanstack-query-plugin',
10
+ });
11
+ useSyncOnlineStatus(client);
12
+ useHandleDevToolsMessages(queryClient, client);
13
+ useSyncTanStackCache(queryClient, client);
14
+ useHandleInitialData(queryClient, client);
15
+ useTanStackQueryAgentTools(queryClient);
16
+ };