@rozenite/tanstack-query-plugin 1.5.1 → 1.6.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.
@@ -0,0 +1,147 @@
1
+ import { useEffect, useMemo } from 'react';
2
+ import { QueryClient } from '@tanstack/react-query';
3
+ import { useRozenitePluginAgentTool } from '@rozenite/agent-bridge';
4
+ import {
5
+ clearMutationCacheTool,
6
+ clearQueryCacheTool,
7
+ createTanStackQueryAgentController,
8
+ getCacheSummaryTool,
9
+ getMutationDetailsTool,
10
+ getOnlineStatusTool,
11
+ getQueryDetailsTool,
12
+ invalidateQueryTool,
13
+ listMutationsTool,
14
+ listQueriesTool,
15
+ refetchQueryTool,
16
+ removeQueryTool,
17
+ resetQueryTool,
18
+ setQueryErrorTool,
19
+ setQueryLoadingTool,
20
+ TANSTACK_QUERY_AGENT_PLUGIN_ID,
21
+ type TanStackQueryAgentMutationIdInput,
22
+ type TanStackQueryAgentOnlineStatusInput,
23
+ type TanStackQueryAgentPaginationInput,
24
+ type TanStackQueryAgentQueryHashInput,
25
+ type TanStackQueryAgentQueryToggleInput,
26
+ setOnlineStatusTool,
27
+ } from './tanstack-query-agent';
28
+
29
+ export const useTanStackQueryAgentTools = (queryClient: QueryClient) => {
30
+ const controller = useMemo(
31
+ () => createTanStackQueryAgentController(queryClient),
32
+ [queryClient]
33
+ );
34
+
35
+ useEffect(() => {
36
+ const unsubscribeQueryCache = queryClient
37
+ .getQueryCache()
38
+ .subscribe((event) => controller.handleQueryCacheEvent(event));
39
+ const unsubscribeMutationCache = queryClient
40
+ .getMutationCache()
41
+ .subscribe((event) => controller.handleMutationCacheEvent(event));
42
+
43
+ return () => {
44
+ unsubscribeQueryCache();
45
+ unsubscribeMutationCache();
46
+ };
47
+ }, [controller, queryClient]);
48
+
49
+ useRozenitePluginAgentTool({
50
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
51
+ tool: getCacheSummaryTool,
52
+ handler: () => controller.getCacheSummary(),
53
+ });
54
+
55
+ useRozenitePluginAgentTool({
56
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
57
+ tool: getOnlineStatusTool,
58
+ handler: () => controller.getOnlineStatus(),
59
+ });
60
+
61
+ useRozenitePluginAgentTool<TanStackQueryAgentOnlineStatusInput>({
62
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
63
+ tool: setOnlineStatusTool,
64
+ handler: (input: TanStackQueryAgentOnlineStatusInput) =>
65
+ controller.setOnlineStatus(input),
66
+ });
67
+
68
+ useRozenitePluginAgentTool<TanStackQueryAgentPaginationInput>({
69
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
70
+ tool: listQueriesTool,
71
+ handler: (input = {}) => controller.listQueries(input),
72
+ });
73
+
74
+ useRozenitePluginAgentTool<TanStackQueryAgentQueryHashInput>({
75
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
76
+ tool: getQueryDetailsTool,
77
+ handler: (input: TanStackQueryAgentQueryHashInput) =>
78
+ controller.getQueryDetails(input),
79
+ });
80
+
81
+ useRozenitePluginAgentTool<TanStackQueryAgentQueryHashInput>({
82
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
83
+ tool: refetchQueryTool,
84
+ handler: (input: TanStackQueryAgentQueryHashInput) =>
85
+ controller.refetchQuery(input),
86
+ });
87
+
88
+ useRozenitePluginAgentTool<TanStackQueryAgentQueryToggleInput>({
89
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
90
+ tool: setQueryLoadingTool,
91
+ handler: (input: TanStackQueryAgentQueryToggleInput) =>
92
+ controller.setQueryLoading(input),
93
+ });
94
+
95
+ useRozenitePluginAgentTool<TanStackQueryAgentQueryToggleInput>({
96
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
97
+ tool: setQueryErrorTool,
98
+ handler: (input: TanStackQueryAgentQueryToggleInput) =>
99
+ controller.setQueryError(input),
100
+ });
101
+
102
+ useRozenitePluginAgentTool<TanStackQueryAgentQueryHashInput>({
103
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
104
+ tool: invalidateQueryTool,
105
+ handler: (input: TanStackQueryAgentQueryHashInput) =>
106
+ controller.invalidateQuery(input),
107
+ });
108
+
109
+ useRozenitePluginAgentTool<TanStackQueryAgentQueryHashInput>({
110
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
111
+ tool: resetQueryTool,
112
+ handler: (input: TanStackQueryAgentQueryHashInput) =>
113
+ controller.resetQuery(input),
114
+ });
115
+
116
+ useRozenitePluginAgentTool<TanStackQueryAgentQueryHashInput>({
117
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
118
+ tool: removeQueryTool,
119
+ handler: (input: TanStackQueryAgentQueryHashInput) =>
120
+ controller.removeQuery(input),
121
+ });
122
+
123
+ useRozenitePluginAgentTool({
124
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
125
+ tool: clearQueryCacheTool,
126
+ handler: () => controller.clearQueryCache(),
127
+ });
128
+
129
+ useRozenitePluginAgentTool<TanStackQueryAgentPaginationInput>({
130
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
131
+ tool: listMutationsTool,
132
+ handler: (input = {}) => controller.listMutations(input),
133
+ });
134
+
135
+ useRozenitePluginAgentTool<TanStackQueryAgentMutationIdInput>({
136
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
137
+ tool: getMutationDetailsTool,
138
+ handler: (input: TanStackQueryAgentMutationIdInput) =>
139
+ controller.getMutationDetails(input),
140
+ });
141
+
142
+ useRozenitePluginAgentTool({
143
+ pluginId: TANSTACK_QUERY_AGENT_PLUGIN_ID,
144
+ tool: clearMutationCacheTool,
145
+ handler: () => controller.clearMutationCache(),
146
+ });
147
+ };
@@ -0,0 +1,197 @@
1
+ import { QueryClient } from '@tanstack/react-query';
2
+ import { DevToolsActionType } from '../shared/types';
3
+
4
+ type QueryScopedActionInput = {
5
+ type: Exclude<
6
+ DevToolsActionType,
7
+ 'CLEAR_MUTATION_CACHE' | 'CLEAR_QUERY_CACHE'
8
+ >;
9
+ queryHash: string;
10
+ };
11
+
12
+ type CacheScopedActionInput = {
13
+ type: 'CLEAR_MUTATION_CACHE' | 'CLEAR_QUERY_CACHE';
14
+ queryHash?: string;
15
+ };
16
+
17
+ export type TanStackQueryDevtoolsActionInput =
18
+ | QueryScopedActionInput
19
+ | CacheScopedActionInput;
20
+
21
+ const getActiveQuery = (queryClient: QueryClient, queryHash?: string) => {
22
+ if (!queryHash) {
23
+ throw new Error('queryHash is required for this TanStack Query action.');
24
+ }
25
+
26
+ const activeQuery = queryClient.getQueryCache().get(queryHash);
27
+ if (!activeQuery) {
28
+ throw new Error(`No active query found for hash: ${queryHash}`);
29
+ }
30
+
31
+ return activeQuery;
32
+ };
33
+
34
+ export const applyTanStackQueryDevtoolsAction = async (
35
+ queryClient: QueryClient,
36
+ input: TanStackQueryDevtoolsActionInput
37
+ ) => {
38
+ switch (input.type) {
39
+ case 'CLEAR_QUERY_CACHE': {
40
+ const queryCountBefore = queryClient.getQueryCache().getAll().length;
41
+ queryClient.getQueryCache().clear();
42
+ return {
43
+ applied: true,
44
+ action: input.type,
45
+ cleared: true,
46
+ queryCountBefore,
47
+ queryCountAfter: queryClient.getQueryCache().getAll().length,
48
+ };
49
+ }
50
+
51
+ case 'CLEAR_MUTATION_CACHE': {
52
+ const mutationCountBefore =
53
+ queryClient.getMutationCache().getAll().length;
54
+ queryClient.getMutationCache().clear();
55
+ return {
56
+ applied: true,
57
+ action: input.type,
58
+ cleared: true,
59
+ mutationCountBefore,
60
+ mutationCountAfter: queryClient.getMutationCache().getAll().length,
61
+ };
62
+ }
63
+
64
+ case 'TRIGGER_ERROR': {
65
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
66
+ const previousQueryOptions = activeQuery.options;
67
+ const error = new Error('Unknown error from devtools');
68
+
69
+ activeQuery.setState({
70
+ status: 'error',
71
+ error,
72
+ fetchMeta: {
73
+ ...activeQuery.state.fetchMeta,
74
+ // @ts-expect-error This does exist
75
+ __previousQueryOptions: previousQueryOptions,
76
+ },
77
+ });
78
+
79
+ return {
80
+ applied: true,
81
+ action: input.type,
82
+ queryHash: activeQuery.queryHash,
83
+ };
84
+ }
85
+
86
+ case 'RESTORE_ERROR': {
87
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
88
+ await queryClient.resetQueries(activeQuery);
89
+ return {
90
+ applied: true,
91
+ action: input.type,
92
+ queryHash: activeQuery.queryHash,
93
+ };
94
+ }
95
+
96
+ case 'TRIGGER_LOADING': {
97
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
98
+ const previousQueryOptions = activeQuery.options;
99
+
100
+ void activeQuery.fetch({
101
+ ...previousQueryOptions,
102
+ queryFn: () =>
103
+ new Promise(() => {
104
+ // Never resolve - simulates perpetual loading
105
+ }),
106
+ gcTime: -1,
107
+ });
108
+
109
+ activeQuery.setState({
110
+ data: undefined,
111
+ status: 'pending',
112
+ fetchMeta: {
113
+ ...activeQuery.state.fetchMeta,
114
+ // @ts-expect-error This does exist
115
+ __previousQueryOptions: previousQueryOptions,
116
+ },
117
+ });
118
+
119
+ return {
120
+ applied: true,
121
+ action: input.type,
122
+ queryHash: activeQuery.queryHash,
123
+ };
124
+ }
125
+
126
+ case 'RESTORE_LOADING': {
127
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
128
+ const previousState = activeQuery.state;
129
+ const previousOptions = activeQuery.state.fetchMeta
130
+ ? (
131
+ activeQuery.state.fetchMeta as unknown as {
132
+ __previousQueryOptions: unknown;
133
+ }
134
+ ).__previousQueryOptions
135
+ : null;
136
+
137
+ activeQuery.cancel({ silent: true });
138
+ activeQuery.setState({
139
+ ...previousState,
140
+ fetchStatus: 'idle',
141
+ fetchMeta: null,
142
+ });
143
+
144
+ if (previousOptions) {
145
+ void activeQuery.fetch(previousOptions);
146
+ }
147
+
148
+ return {
149
+ applied: true,
150
+ action: input.type,
151
+ queryHash: activeQuery.queryHash,
152
+ };
153
+ }
154
+
155
+ case 'RESET': {
156
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
157
+ await queryClient.resetQueries(activeQuery);
158
+ return {
159
+ applied: true,
160
+ action: input.type,
161
+ queryHash: activeQuery.queryHash,
162
+ };
163
+ }
164
+
165
+ case 'REMOVE': {
166
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
167
+ queryClient.removeQueries(activeQuery);
168
+ return {
169
+ applied: true,
170
+ action: input.type,
171
+ queryHash: activeQuery.queryHash,
172
+ };
173
+ }
174
+
175
+ case 'REFETCH': {
176
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
177
+ await activeQuery.fetch().catch(() => {
178
+ // Ignore errors from refetch in the agent/UI bridge.
179
+ });
180
+ return {
181
+ applied: true,
182
+ action: input.type,
183
+ queryHash: activeQuery.queryHash,
184
+ };
185
+ }
186
+
187
+ case 'INVALIDATE': {
188
+ const activeQuery = getActiveQuery(queryClient, input.queryHash);
189
+ await queryClient.invalidateQueries(activeQuery);
190
+ return {
191
+ applied: true,
192
+ action: input.type,
193
+ queryHash: activeQuery.queryHash,
194
+ };
195
+ }
196
+ }
197
+ };
@@ -1,6 +1,7 @@
1
1
  import { useEffect } from 'react';
2
2
  import { QueryClient } from '@tanstack/react-query';
3
3
  import { TanStackQueryPluginClient } from '../shared/messaging';
4
+ import { applyTanStackQueryDevtoolsAction } from './devtools-actions';
4
5
 
5
6
  export const useHandleDevToolsMessages = (
6
7
  queryClient: QueryClient,
@@ -14,110 +15,14 @@ export const useHandleDevToolsMessages = (
14
15
  const subscription = client.onMessage(
15
16
  'devtools-action',
16
17
  ({ type, queryHash }) => {
17
- const activeQuery = queryClient.getQueryCache().get(queryHash);
18
-
19
- if (!activeQuery) {
20
- console.warn(`No active query found for hash: ${queryHash}`);
21
- return;
22
- }
23
-
24
- switch (type) {
25
- case 'TRIGGER_ERROR': {
26
- // Code from React Query External Sync:
27
- // https://github.com/LovesWorking/react-query-external-sync/blob/main/src/react-query-external-sync/useSyncQueriesExternal.ts#L717
28
-
29
- const __previousQueryOptions = activeQuery.options;
30
- const error = new Error('Unknown error from devtools');
31
-
32
- activeQuery.setState({
33
- status: 'error',
34
- error,
35
- fetchMeta: {
36
- ...activeQuery.state.fetchMeta,
37
- // @ts-expect-error This does exist
38
- __previousQueryOptions,
39
- },
40
- });
41
- break;
42
- }
43
- case 'RESTORE_ERROR': {
44
- queryClient.resetQueries(activeQuery);
45
- break;
46
- }
47
- case 'TRIGGER_LOADING': {
48
- // Code from React Query External Sync:
49
- // https://github.com/LovesWorking/react-query-external-sync/blob/main/src/react-query-external-sync/useSyncQueriesExternal.ts#L742
50
-
51
- if (!activeQuery) return;
52
- const __previousQueryOptions = activeQuery.options;
53
- // Trigger a fetch in order to trigger suspense as well.
54
- activeQuery.fetch({
55
- ...__previousQueryOptions,
56
- queryFn: () => {
57
- return new Promise(() => {
58
- // Never resolve - simulates perpetual loading
59
- });
60
- },
61
- gcTime: -1,
62
- });
63
- activeQuery.setState({
64
- data: undefined,
65
- status: 'pending',
66
- fetchMeta: {
67
- ...activeQuery.state.fetchMeta,
68
- // @ts-expect-error This does exist
69
- __previousQueryOptions,
70
- },
71
- });
72
- break;
73
- }
74
- case 'RESTORE_LOADING': {
75
- // Code from React Query External Sync:
76
- // https://github.com/LovesWorking/tanstack-query-dev-tools-expo-plugin/blob/main/src/useSyncQueries.ts#L176
77
-
78
- const previousState = activeQuery.state;
79
- const previousOptions = activeQuery.state.fetchMeta
80
- ? (
81
- activeQuery.state.fetchMeta as unknown as {
82
- __previousQueryOptions: unknown;
83
- }
84
- ).__previousQueryOptions
85
- : null;
86
-
87
- activeQuery.cancel({ silent: true });
88
- activeQuery.setState({
89
- ...previousState,
90
- fetchStatus: 'idle',
91
- fetchMeta: null,
92
- });
93
-
94
- if (previousOptions) {
95
- activeQuery.fetch(previousOptions);
96
- }
97
- break;
98
- }
99
- case 'RESET': {
100
- queryClient.resetQueries(activeQuery);
101
- break;
102
- }
103
- case 'REMOVE': {
104
- queryClient.removeQueries(activeQuery);
105
- break;
106
- }
107
- case 'REFETCH': {
108
- activeQuery.fetch().catch(() => {
109
- // Ignore errors
110
- });
111
- break;
112
- }
113
- case 'INVALIDATE': {
114
- queryClient.invalidateQueries(activeQuery);
115
- break;
116
- }
117
- default: {
118
- console.warn(`Unknown devtools action: ${type}`);
119
- }
120
- }
18
+ void applyTanStackQueryDevtoolsAction(queryClient, { type, queryHash })
19
+ .catch((error) => {
20
+ const message =
21
+ error instanceof Error ? error.message : String(error);
22
+ console.warn(
23
+ `[Rozenite, tanstack-query-plugin] Failed to apply devtools action "${type}": ${message}`
24
+ );
25
+ });
121
26
  }
122
27
  );
123
28
 
@@ -5,6 +5,7 @@ import { useSyncOnlineStatus } from '../shared/useSyncOnlineStatus';
5
5
  import { useHandleDevToolsMessages } from './useHandleDevToolsMessages';
6
6
  import { useSyncTanStackCache } from './useSyncTanStackCache';
7
7
  import { useHandleInitialData } from './useHandleInitialData';
8
+ import { useTanStackQueryAgentTools } from './agent/useTanStackQueryAgentTools';
8
9
 
9
10
  export const useTanStackQueryDevTools = (queryClient: QueryClient) => {
10
11
  const client = useRozeniteDevToolsClient<TanStackQueryPluginEventMap>({
@@ -18,4 +19,6 @@ export const useTanStackQueryDevTools = (queryClient: QueryClient) => {
18
19
  useSyncTanStackCache(queryClient, client);
19
20
 
20
21
  useHandleInitialData(queryClient, client);
22
+
23
+ useTanStackQueryAgentTools(queryClient);
21
24
  };
package/tsconfig.json CHANGED
@@ -19,6 +19,9 @@
19
19
  "include": ["src/**/*", "react-native.ts", "rozenite.config.ts"],
20
20
  "exclude": ["node_modules", "dist", "build"],
21
21
  "references": [
22
+ {
23
+ "path": "../agent-bridge"
24
+ },
22
25
  {
23
26
  "path": "../plugin-bridge"
24
27
  },