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