@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,418 +0,0 @@
1
- import {
2
- defineAgentToolContract,
3
- definePaginatedAgentToolContract,
4
- type AgentToolContract,
5
- type JSONSchema7,
6
- } from '@rozenite/agent-shared';
7
- import type { FetchStatus, MutationStatus, QueryStatus } from '@tanstack/react-query';
8
- import type { applyTanStackQueryDevtoolsAction } from '../react-native/devtools-actions';
9
-
10
- export const TANSTACK_QUERY_AGENT_PLUGIN_ID = '@rozenite/tanstack-query-plugin';
11
-
12
- export type TanStackQueryAgentPaginationInput = {
13
- limit?: number;
14
- cursor?: string;
15
- };
16
-
17
- export type TanStackQueryAgentQueryHashInput = {
18
- queryHash: string;
19
- };
20
-
21
- export type TanStackQueryAgentQueryToggleInput = TanStackQueryAgentQueryHashInput & {
22
- enabled: boolean;
23
- };
24
-
25
- export type TanStackQueryAgentMutationIdInput = {
26
- mutationId: number;
27
- };
28
-
29
- export type TanStackQueryAgentOnlineStatusInput = {
30
- online: boolean;
31
- };
32
-
33
- export type TanStackQueryAgentSafeValue =
34
- | null
35
- | boolean
36
- | number
37
- | string
38
- | TanStackQueryAgentSafeValue[]
39
- | { [key: string]: TanStackQueryAgentSafeValue };
40
-
41
- export type TanStackQueryCacheSummary = {
42
- online: boolean;
43
- queries: {
44
- total: number;
45
- active: number;
46
- fetching: number;
47
- pending: number;
48
- success: number;
49
- error: number;
50
- invalidated: number;
51
- };
52
- mutations: {
53
- total: number;
54
- pending: number;
55
- success: number;
56
- error: number;
57
- paused: number;
58
- };
59
- };
60
-
61
- export type TanStackQueryPage = {
62
- limit: number;
63
- hasMore: boolean;
64
- nextCursor?: string;
65
- };
66
-
67
- export type TanStackQueryQuerySummary = {
68
- queryHash: string;
69
- queryKey: TanStackQueryAgentSafeValue;
70
- status: QueryStatus;
71
- fetchStatus: FetchStatus;
72
- observersCount: number;
73
- isInvalidated: boolean;
74
- dataUpdatedAt: number;
75
- errorUpdatedAt: number;
76
- hasData: boolean;
77
- hasError: boolean;
78
- };
79
-
80
- export type TanStackQueryObserverOptionsSummary = {
81
- enabled: TanStackQueryAgentSafeValue;
82
- networkMode: TanStackQueryAgentSafeValue;
83
- staleTime: TanStackQueryAgentSafeValue;
84
- gcTime: TanStackQueryAgentSafeValue;
85
- retry: TanStackQueryAgentSafeValue;
86
- retryDelay: TanStackQueryAgentSafeValue;
87
- refetchInterval: TanStackQueryAgentSafeValue;
88
- refetchOnMount: TanStackQueryAgentSafeValue;
89
- refetchOnReconnect: TanStackQueryAgentSafeValue;
90
- refetchOnWindowFocus: TanStackQueryAgentSafeValue;
91
- subscribed: TanStackQueryAgentSafeValue;
92
- meta: TanStackQueryAgentSafeValue;
93
- hasQueryFn: boolean;
94
- hasSelect: boolean;
95
- hasPlaceholderData: boolean;
96
- hasInitialData: boolean;
97
- };
98
-
99
- export type TanStackQueryListQueriesResult = TanStackQueryCacheSummary & {
100
- total: number;
101
- items: TanStackQueryQuerySummary[];
102
- page: TanStackQueryPage;
103
- };
104
-
105
- export type TanStackQueryGetQueryDetailsResult = {
106
- summary: TanStackQueryCacheSummary;
107
- query: TanStackQueryQuerySummary & {
108
- data: TanStackQueryAgentSafeValue;
109
- error: TanStackQueryAgentSafeValue;
110
- observers: Array<{
111
- queryHash: string;
112
- options: TanStackQueryObserverOptionsSummary | null;
113
- }>;
114
- };
115
- };
116
-
117
- export type TanStackQueryActionResult = Awaited<
118
- ReturnType<typeof applyTanStackQueryDevtoolsAction>
119
- >;
120
-
121
- export type TanStackQueryMutationSummary = {
122
- mutationId: number;
123
- mutationKey: TanStackQueryAgentSafeValue;
124
- status: MutationStatus;
125
- isPaused: boolean;
126
- submittedAt: number;
127
- failureCount: number;
128
- hasData: boolean;
129
- hasError: boolean;
130
- };
131
-
132
- export type TanStackQueryMutationOptionsSummary = {
133
- mutationKey: TanStackQueryAgentSafeValue;
134
- networkMode: TanStackQueryAgentSafeValue;
135
- gcTime: TanStackQueryAgentSafeValue;
136
- retry: TanStackQueryAgentSafeValue;
137
- retryDelay: TanStackQueryAgentSafeValue;
138
- meta: TanStackQueryAgentSafeValue;
139
- scope: TanStackQueryAgentSafeValue;
140
- hasMutationFn: boolean;
141
- hasOnMutate: boolean;
142
- hasOnSuccess: boolean;
143
- hasOnError: boolean;
144
- hasOnSettled: boolean;
145
- };
146
-
147
- export type TanStackQueryListMutationsResult = TanStackQueryCacheSummary & {
148
- total: number;
149
- items: TanStackQueryMutationSummary[];
150
- page: TanStackQueryPage;
151
- };
152
-
153
- export type TanStackQueryGetMutationDetailsResult = {
154
- summary: TanStackQueryCacheSummary;
155
- mutation: TanStackQueryMutationSummary & {
156
- variables: TanStackQueryAgentSafeValue;
157
- data: TanStackQueryAgentSafeValue;
158
- error: TanStackQueryAgentSafeValue;
159
- context: TanStackQueryAgentSafeValue;
160
- failureReason: TanStackQueryAgentSafeValue;
161
- options: TanStackQueryMutationOptionsSummary;
162
- };
163
- };
164
-
165
- export type TanStackQueryGetCacheSummaryArgs = undefined;
166
- export type TanStackQueryGetCacheSummaryResult = TanStackQueryCacheSummary;
167
- export type TanStackQueryGetOnlineStatusArgs = undefined;
168
- export type TanStackQueryGetOnlineStatusResult = { online: boolean };
169
- export type TanStackQuerySetOnlineStatusArgs = TanStackQueryAgentOnlineStatusInput;
170
- export type TanStackQuerySetOnlineStatusResult = { online: boolean };
171
- export type TanStackQueryListQueriesArgs = TanStackQueryAgentPaginationInput;
172
- export type TanStackQueryGetQueryDetailsArgs = TanStackQueryAgentQueryHashInput;
173
- export type TanStackQueryRefetchQueryArgs = TanStackQueryAgentQueryHashInput;
174
- export type TanStackQuerySetQueryLoadingArgs = TanStackQueryAgentQueryToggleInput;
175
- export type TanStackQuerySetQueryErrorArgs = TanStackQueryAgentQueryToggleInput;
176
- export type TanStackQueryInvalidateQueryArgs = TanStackQueryAgentQueryHashInput;
177
- export type TanStackQueryResetQueryArgs = TanStackQueryAgentQueryHashInput;
178
- export type TanStackQueryRemoveQueryArgs = TanStackQueryAgentQueryHashInput;
179
- export type TanStackQueryClearQueryCacheArgs = undefined;
180
- export type TanStackQueryClearQueryCacheResult = TanStackQueryActionResult;
181
- export type TanStackQueryListMutationsArgs = TanStackQueryAgentPaginationInput;
182
- export type TanStackQueryGetMutationDetailsArgs = TanStackQueryAgentMutationIdInput;
183
- export type TanStackQueryClearMutationCacheArgs = undefined;
184
- export type TanStackQueryClearMutationCacheResult = TanStackQueryActionResult;
185
-
186
- const queryActionProperties = {
187
- queryHash: {
188
- type: 'string',
189
- description: 'TanStack Query queryHash identifying the query.',
190
- },
191
- } satisfies Record<string, JSONSchema7>;
192
-
193
- const mutationActionProperties = {
194
- mutationId: {
195
- type: 'number',
196
- description: 'TanStack Query mutationId identifying the mutation.',
197
- },
198
- } satisfies Record<string, JSONSchema7>;
199
-
200
- const paginationProperties = {
201
- limit: {
202
- type: 'number',
203
- description: 'Maximum number of items to return. Defaults to 20. Maximum 100.',
204
- },
205
- cursor: {
206
- type: 'string',
207
- description: 'Opaque pagination cursor from a previous list call.',
208
- },
209
- } satisfies Record<string, JSONSchema7>;
210
-
211
- const emptyInputSchema: JSONSchema7 = {
212
- type: 'object',
213
- properties: {},
214
- };
215
-
216
- export const tanstackQueryToolDefinitions = {
217
- getCacheSummary: defineAgentToolContract<
218
- TanStackQueryGetCacheSummaryArgs,
219
- TanStackQueryGetCacheSummaryResult
220
- >({
221
- name: 'get-cache-summary',
222
- description: 'Return aggregate TanStack Query cache health and count information.',
223
- inputSchema: emptyInputSchema,
224
- }),
225
- getOnlineStatus: defineAgentToolContract<
226
- TanStackQueryGetOnlineStatusArgs,
227
- TanStackQueryGetOnlineStatusResult
228
- >({
229
- name: 'get-online-status',
230
- description: 'Return the current TanStack Query onlineManager status.',
231
- inputSchema: emptyInputSchema,
232
- }),
233
- setOnlineStatus: defineAgentToolContract<
234
- TanStackQuerySetOnlineStatusArgs,
235
- TanStackQuerySetOnlineStatusResult
236
- >({
237
- name: 'set-online-status',
238
- description: 'Set the TanStack Query onlineManager status for testing offline/online behavior.',
239
- inputSchema: {
240
- type: 'object',
241
- properties: {
242
- online: {
243
- type: 'boolean',
244
- description: 'Whether the TanStack Query onlineManager should be online.',
245
- },
246
- },
247
- required: ['online'],
248
- },
249
- }),
250
- listQueries: definePaginatedAgentToolContract<
251
- TanStackQueryListQueriesArgs,
252
- TanStackQueryListQueriesResult
253
- >({
254
- name: 'list-queries',
255
- description: 'List TanStack Query query summaries using cursor pagination.',
256
- inputSchema: {
257
- type: 'object',
258
- properties: paginationProperties,
259
- },
260
- pagination: {
261
- kind: 'cursor',
262
- fields: [
263
- 'queryHash',
264
- 'queryKey',
265
- 'status',
266
- 'fetchStatus',
267
- 'observersCount',
268
- 'isInvalidated',
269
- 'dataUpdatedAt',
270
- 'errorUpdatedAt',
271
- 'hasData',
272
- 'hasError',
273
- ],
274
- defaultFields: ['queryHash', 'queryKey', 'status', 'fetchStatus', 'hasError'],
275
- },
276
- }),
277
- getQueryDetails: defineAgentToolContract<
278
- TanStackQueryGetQueryDetailsArgs,
279
- TanStackQueryGetQueryDetailsResult
280
- >({
281
- name: 'get-query-details',
282
- description: 'Return a JSON-safe TanStack Query query snapshot and observer summary.',
283
- inputSchema: {
284
- type: 'object',
285
- properties: queryActionProperties,
286
- required: ['queryHash'],
287
- },
288
- }),
289
- refetchQuery: defineAgentToolContract<TanStackQueryRefetchQueryArgs, TanStackQueryActionResult>({
290
- name: 'refetch-query',
291
- description: 'Refetch a TanStack Query query by queryHash.',
292
- inputSchema: {
293
- type: 'object',
294
- properties: queryActionProperties,
295
- required: ['queryHash'],
296
- },
297
- }),
298
- setQueryLoading: defineAgentToolContract<
299
- TanStackQuerySetQueryLoadingArgs,
300
- TanStackQueryActionResult
301
- >({
302
- name: 'set-query-loading',
303
- description:
304
- 'Enable or disable TanStack Query loading-state simulation for a query by queryHash.',
305
- inputSchema: {
306
- type: 'object',
307
- properties: {
308
- ...queryActionProperties,
309
- enabled: {
310
- type: 'boolean',
311
- description: 'Whether the loading-state simulation should be enabled.',
312
- },
313
- },
314
- required: ['queryHash', 'enabled'],
315
- },
316
- }),
317
- setQueryError: defineAgentToolContract<TanStackQuerySetQueryErrorArgs, TanStackQueryActionResult>(
318
- {
319
- name: 'set-query-error',
320
- description:
321
- 'Enable or disable TanStack Query error-state simulation for a query by queryHash.',
322
- inputSchema: {
323
- type: 'object',
324
- properties: {
325
- ...queryActionProperties,
326
- enabled: {
327
- type: 'boolean',
328
- description: 'Whether the error-state simulation should be enabled.',
329
- },
330
- },
331
- required: ['queryHash', 'enabled'],
332
- },
333
- },
334
- ),
335
- invalidateQuery: defineAgentToolContract<
336
- TanStackQueryInvalidateQueryArgs,
337
- TanStackQueryActionResult
338
- >({
339
- name: 'invalidate-query',
340
- description: 'Invalidate a TanStack Query query by queryHash.',
341
- inputSchema: {
342
- type: 'object',
343
- properties: queryActionProperties,
344
- required: ['queryHash'],
345
- },
346
- }),
347
- resetQuery: defineAgentToolContract<TanStackQueryResetQueryArgs, TanStackQueryActionResult>({
348
- name: 'reset-query',
349
- description: 'Reset a TanStack Query query by queryHash.',
350
- inputSchema: {
351
- type: 'object',
352
- properties: queryActionProperties,
353
- required: ['queryHash'],
354
- },
355
- }),
356
- removeQuery: defineAgentToolContract<TanStackQueryRemoveQueryArgs, TanStackQueryActionResult>({
357
- name: 'remove-query',
358
- description: 'Remove a TanStack Query query from the cache by queryHash.',
359
- inputSchema: {
360
- type: 'object',
361
- properties: queryActionProperties,
362
- required: ['queryHash'],
363
- },
364
- }),
365
- clearQueryCache: defineAgentToolContract<
366
- TanStackQueryClearQueryCacheArgs,
367
- TanStackQueryClearQueryCacheResult
368
- >({
369
- name: 'clear-query-cache',
370
- description: 'Clear the full TanStack Query query cache.',
371
- inputSchema: emptyInputSchema,
372
- }),
373
- listMutations: definePaginatedAgentToolContract<
374
- TanStackQueryListMutationsArgs,
375
- TanStackQueryListMutationsResult
376
- >({
377
- name: 'list-mutations',
378
- description: 'List TanStack Query mutation summaries using cursor pagination.',
379
- inputSchema: {
380
- type: 'object',
381
- properties: paginationProperties,
382
- },
383
- pagination: {
384
- kind: 'cursor',
385
- fields: [
386
- 'mutationId',
387
- 'mutationKey',
388
- 'status',
389
- 'isPaused',
390
- 'submittedAt',
391
- 'failureCount',
392
- 'hasData',
393
- 'hasError',
394
- ],
395
- defaultFields: ['mutationId', 'mutationKey', 'status', 'hasError'],
396
- },
397
- }),
398
- getMutationDetails: defineAgentToolContract<
399
- TanStackQueryGetMutationDetailsArgs,
400
- TanStackQueryGetMutationDetailsResult
401
- >({
402
- name: 'get-mutation-details',
403
- description: 'Return a JSON-safe TanStack Query mutation snapshot for a mutationId.',
404
- inputSchema: {
405
- type: 'object',
406
- properties: mutationActionProperties,
407
- required: ['mutationId'],
408
- },
409
- }),
410
- clearMutationCache: defineAgentToolContract<
411
- TanStackQueryClearMutationCacheArgs,
412
- TanStackQueryClearMutationCacheResult
413
- >({
414
- name: 'clear-mutation-cache',
415
- description: 'Clear the full TanStack Query mutation cache.',
416
- inputSchema: emptyInputSchema,
417
- }),
418
- } as const satisfies Record<string, AgentToolContract<unknown, unknown>>;
@@ -1,40 +0,0 @@
1
- import type { Query, Mutation, QueryClient } from '@tanstack/react-query';
2
- import type {
3
- SerializableQuery,
4
- SerializableMutation,
5
- SerializableObserver,
6
- SerializableQueryClient,
7
- } from './types';
8
-
9
- export const dehydrateObservers = (query: Query): SerializableObserver[] => {
10
- return query.observers.map((observer) => ({
11
- queryHash: query.queryHash,
12
- options: observer.options,
13
- }));
14
- };
15
-
16
- export const dehydrateQuery = (query: Query): SerializableQuery => {
17
- const dehydratedObservers: SerializableObserver[] = dehydrateObservers(query);
18
-
19
- return {
20
- state: query.state,
21
- queryKey: query.queryKey,
22
- queryHash: query.queryHash,
23
- observers: dehydratedObservers,
24
- };
25
- };
26
-
27
- export const dehydrateMutation = (mutation: Mutation): SerializableMutation => {
28
- return {
29
- mutationId: mutation.mutationId,
30
- state: mutation.state,
31
- options: mutation.options,
32
- };
33
- };
34
-
35
- export const dehydrateQueryClient = (queryClient: QueryClient): SerializableQueryClient => {
36
- return {
37
- queries: queryClient.getQueryCache().getAll().map(dehydrateQuery),
38
- mutations: queryClient.getMutationCache().getAll().map(dehydrateMutation),
39
- };
40
- };
@@ -1,243 +0,0 @@
1
- import {
2
- InfiniteQueryObserverOptions,
3
- MutationOptions,
4
- MutationState,
5
- QueryClient,
6
- QueryObserver,
7
- QueryObserverOptions,
8
- QueryState,
9
- } from '@tanstack/react-query';
10
- import type {
11
- SerializableQuery,
12
- SerializableMutation,
13
- SerializableQueryClient,
14
- SerializableObserver,
15
- PartialQueryState,
16
- } from './types';
17
- import { applyRemoteQueryState, instrumentQuery } from './query-data-sync';
18
-
19
- const mockQueryFn = () => {
20
- return Promise.resolve(null);
21
- };
22
-
23
- const hydrateObservers = (
24
- client: QueryClient,
25
- queryHash: string,
26
- dehydratedObservers: SerializableObserver[],
27
- ) => {
28
- const query = client.getQueryCache().get(queryHash);
29
-
30
- if (!query) {
31
- return;
32
- }
33
-
34
- query.observers.forEach((observer) => {
35
- query.removeObserver(observer);
36
- });
37
-
38
- dehydratedObservers.forEach((observerState) => {
39
- const hydratedOptions: InfiniteQueryObserverOptions | QueryObserverOptions =
40
- observerState.options;
41
-
42
- if ('initialPageParam' in hydratedOptions) {
43
- delete hydratedOptions.initialPageParam;
44
- }
45
-
46
- if ('behavior' in hydratedOptions) {
47
- delete hydratedOptions.behavior;
48
- }
49
-
50
- hydratedOptions.queryFn = mockQueryFn;
51
-
52
- const observer = new QueryObserver(client, hydratedOptions as QueryObserverOptions);
53
- query.addObserver(observer);
54
- });
55
- };
56
-
57
- const hydrateMutation = (client: QueryClient, dehydratedMutation: SerializableMutation) => {
58
- const mutationCache = client.getMutationCache();
59
- const { options, state } = dehydratedMutation;
60
-
61
- const existingMutation = mutationCache.find({
62
- mutationKey: options.mutationKey,
63
- });
64
- const hydratedState: MutationState<unknown, Error, void, unknown> = state as MutationState<
65
- unknown,
66
- Error,
67
- void,
68
- unknown
69
- >;
70
- const hydratedOptions: MutationOptions = options;
71
-
72
- if (existingMutation) {
73
- mutationCache.remove(existingMutation);
74
- }
75
-
76
- mutationCache.build(client, hydratedOptions, hydratedState);
77
- };
78
-
79
- const hydrateQuery = (client: QueryClient, dehydratedQuery: SerializableQuery) => {
80
- const queryCache = client.getQueryCache();
81
- const { queryKey, state, queryHash, observers } = dehydratedQuery;
82
-
83
- let query = queryCache.get(queryHash);
84
- const data = state.data;
85
- const hydratedState: QueryState = state;
86
-
87
- // Do not hydrate if an existing query exists with newer data
88
- if (query) {
89
- if (
90
- query.state.dataUpdatedAt < state.dataUpdatedAt ||
91
- query.state.fetchStatus !== state.fetchStatus
92
- ) {
93
- applyRemoteQueryState(query, {
94
- ...hydratedState,
95
- data,
96
- });
97
- query.setOptions({
98
- ...query.options,
99
- queryFn: mockQueryFn,
100
- retry: 0,
101
- });
102
- }
103
- } else {
104
- // Restore query
105
- query = queryCache.build(
106
- client,
107
- {
108
- ...client.getDefaultOptions().hydrate?.queries,
109
- queryKey,
110
- queryHash,
111
- queryFn: mockQueryFn,
112
- },
113
- {
114
- ...hydratedState,
115
- data,
116
- },
117
- );
118
- }
119
-
120
- instrumentQuery(query);
121
-
122
- hydrateObservers(client, queryHash, observers);
123
- };
124
-
125
- export const hydrateQueryClient = (
126
- client: QueryClient,
127
- dehydratedState: SerializableQueryClient,
128
- ): void => {
129
- // Hydrate mutations
130
- dehydratedState.mutations.forEach((mutation) => hydrateMutation(client, mutation));
131
-
132
- // Hydrate queries
133
- dehydratedState.queries.forEach((query) => hydrateQuery(client, query));
134
- };
135
-
136
- export const applyQueryEvent = (
137
- queryClient: QueryClient,
138
- type:
139
- | 'added'
140
- | 'updated'
141
- | 'removed'
142
- | 'observerAdded'
143
- | 'observerRemoved'
144
- | 'observerResultsUpdated'
145
- | 'observerOptionsUpdated',
146
- data: SerializableQuery | PartialQueryState,
147
- action?: string,
148
- ): void => {
149
- const queryCache = queryClient.getQueryCache();
150
-
151
- switch (type) {
152
- case 'added': {
153
- hydrateQuery(queryClient, data as SerializableQuery);
154
- break;
155
- }
156
- case 'updated': {
157
- if (action && 'state' in data && !('queryKey' in data)) {
158
- // Handle action-based partial state update
159
- applyPartialQueryState(queryClient, data as PartialQueryState);
160
- } else {
161
- // Handle full query update (backward compatibility)
162
- hydrateQuery(queryClient, data as SerializableQuery);
163
- }
164
- break;
165
- }
166
- case 'observerAdded':
167
- case 'observerRemoved':
168
- case 'observerOptionsUpdated': {
169
- hydrateObservers(queryClient, data.queryHash, (data as any).observers);
170
- break;
171
- }
172
- case 'removed': {
173
- const query = queryCache.get(data.queryHash);
174
- if (query) {
175
- queryCache.remove(query);
176
- }
177
- break;
178
- }
179
- }
180
- };
181
-
182
- // New function to handle action-based partial state updates
183
- export const applyPartialQueryState = (queryClient: QueryClient, data: PartialQueryState): void => {
184
- const queryCache = queryClient.getQueryCache();
185
- const query = queryCache.get(data.queryHash);
186
-
187
- if (!query) {
188
- // Query doesn't exist, we need to create it with minimal data
189
- // This is a fallback for when we receive a partial update for a non-existent query
190
- return;
191
- }
192
-
193
- // Apply the partial state changes
194
- if (data.state) {
195
- applyRemoteQueryState(query, data.state);
196
- }
197
- };
198
-
199
- export const applyMutationEvent = (
200
- queryClient: QueryClient,
201
- type:
202
- | 'added'
203
- | 'updated'
204
- | 'removed'
205
- | 'observerAdded'
206
- | 'observerRemoved'
207
- | 'observerResultsUpdated'
208
- | 'observerOptionsUpdated',
209
- data: SerializableMutation,
210
- ): void => {
211
- const mutationCache = queryClient.getMutationCache();
212
-
213
- switch (type) {
214
- case 'added':
215
- case 'updated':
216
- case 'observerAdded':
217
- case 'observerRemoved':
218
- case 'observerResultsUpdated':
219
- case 'observerOptionsUpdated': {
220
- hydrateMutation(queryClient, data);
221
- break;
222
- }
223
- case 'removed': {
224
- const mutation = mutationCache.find({
225
- mutationKey: data.options.mutationKey,
226
- });
227
- if (mutation) {
228
- mutationCache.remove(mutation);
229
- }
230
- break;
231
- }
232
- }
233
- };
234
-
235
- export const applyQueryObserverEvent = (
236
- queryClient: QueryClient,
237
- data: {
238
- queryHash: string;
239
- observers: SerializableObserver[];
240
- },
241
- ): void => {
242
- hydrateObservers(queryClient, data.queryHash, data.observers);
243
- };