@veams/status-quo-query 0.3.0 → 0.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.
- package/.turbo/turbo-build.log +14 -12
- package/README.md +382 -16
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/mutation.d.ts +43 -0
- package/dist/mutation.js +97 -12
- package/dist/mutation.js.map +1 -1
- package/dist/provider.d.ts +30 -5
- package/dist/provider.js +47 -3
- package/dist/provider.js.map +1 -1
- package/dist/query.d.ts +48 -0
- package/dist/query.js +109 -17
- package/dist/query.js.map +1 -1
- package/dist/tracking.d.ts +85 -0
- package/dist/tracking.js +226 -0
- package/dist/tracking.js.map +1 -0
- package/package.json +15 -1
- package/src/__tests__/mutation.spec.ts +2 -2
- package/src/__tests__/provider.spec.ts +12 -12
- package/src/__tests__/tracked.spec.ts +276 -0
- package/src/index.ts +12 -0
- package/src/mutation.ts +239 -15
- package/src/provider.ts +135 -4
- package/src/query.ts +242 -23
- package/src/tracking.ts +384 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
>
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
>
|
|
12
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
> @veams/status-quo-query@0.3.0 build
|
|
4
|
+
> npm-run-all compile
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
> @veams/status-quo-query@0.3.0 compile
|
|
8
|
+
> npm-run-all bundle:ts
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
> @veams/status-quo-query@0.3.0 bundle:ts
|
|
12
|
+
> tsc --project tsconfig.json
|
|
13
|
+
|
|
14
|
+
⠙[1G[0K⠙[1G[0K⠙[1G[0K
|
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ npm install @veams/status-quo-query @tanstack/query-core
|
|
|
12
12
|
|
|
13
13
|
Root exports:
|
|
14
14
|
|
|
15
|
-
- `
|
|
15
|
+
- `setupQueryManager`
|
|
16
16
|
- `setupQuery`
|
|
17
17
|
- `setupMutation`
|
|
18
18
|
- `isQueryLoading`
|
|
@@ -20,17 +20,28 @@ Root exports:
|
|
|
20
20
|
- `QueryFetchStatus`
|
|
21
21
|
- `QueryStatus`
|
|
22
22
|
- `MutationStatus`
|
|
23
|
-
- `
|
|
23
|
+
- `QueryManager`
|
|
24
24
|
- `CreateQuery`
|
|
25
25
|
- `CreateMutation`
|
|
26
|
+
- `CreateTrackedQuery`
|
|
27
|
+
- `CreateTrackedMutation`
|
|
28
|
+
- `CreateTrackedQueryAndMutation`
|
|
29
|
+
- `CreateTrackedMutationWithDefaults`
|
|
26
30
|
- `QueryService`
|
|
27
31
|
- `MutationService`
|
|
28
32
|
- `QueryServiceSnapshot`
|
|
29
33
|
- `MutationServiceSnapshot`
|
|
30
34
|
- `QueryServiceOptions`
|
|
31
35
|
- `MutationServiceOptions`
|
|
36
|
+
- `TrackedMutationServiceOptions`
|
|
32
37
|
- `QueryInvalidateOptions`
|
|
33
38
|
- `QueryMetaState`
|
|
39
|
+
- `TrackedDependencyRecord`
|
|
40
|
+
- `TrackedDependencyValue`
|
|
41
|
+
- `TrackedInvalidateOn`
|
|
42
|
+
- `TrackedMatchMode`
|
|
43
|
+
- `TrackedQueryKey`
|
|
44
|
+
- `TrackedQueryKeySegment`
|
|
34
45
|
|
|
35
46
|
Subpath exports:
|
|
36
47
|
|
|
@@ -42,36 +53,347 @@ Subpath exports:
|
|
|
42
53
|
|
|
43
54
|
```ts
|
|
44
55
|
import { QueryClient } from '@tanstack/query-core';
|
|
45
|
-
import {
|
|
46
|
-
setupQueryProvider,
|
|
47
|
-
} from '@veams/status-quo-query';
|
|
56
|
+
import { setupQueryManager } from '@veams/status-quo-query';
|
|
48
57
|
|
|
49
58
|
const queryClient = new QueryClient();
|
|
50
|
-
const
|
|
59
|
+
const manager = setupQueryManager(queryClient);
|
|
60
|
+
const applicationId = 'app-1';
|
|
61
|
+
const productId = 'product-1';
|
|
62
|
+
|
|
63
|
+
const fetchProduct = async (currentApplicationId: string, currentProductId: string) => ({
|
|
64
|
+
applicationId: currentApplicationId,
|
|
65
|
+
name: 'Ada',
|
|
66
|
+
productId: currentProductId,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const saveProduct = async (variables: {
|
|
70
|
+
applicationId: string;
|
|
71
|
+
productId: string;
|
|
72
|
+
productName: string;
|
|
73
|
+
}) => ({
|
|
74
|
+
...variables,
|
|
75
|
+
saved: true as const,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const [createTrackedQuery, createTrackedMutation] = manager.createTrackedQueryAndMutation([
|
|
79
|
+
'applicationId',
|
|
80
|
+
'productId',
|
|
81
|
+
] as const);
|
|
82
|
+
|
|
83
|
+
const productQuery = createTrackedQuery(
|
|
84
|
+
['product', { deps: { applicationId: 'app-1', productId: 'product-1' }, view: { page: 1 } }],
|
|
85
|
+
() => fetchProduct('app-1', 'product-1'),
|
|
86
|
+
{
|
|
87
|
+
enabled: false,
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const updateProduct = createTrackedMutation(saveProduct, {
|
|
92
|
+
invalidateOn: 'success',
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
await productQuery.refetch();
|
|
96
|
+
await updateProduct.mutate({
|
|
97
|
+
applicationId: 'app-1',
|
|
98
|
+
productId: 'product-1',
|
|
99
|
+
productName: 'Ada',
|
|
100
|
+
});
|
|
51
101
|
|
|
52
|
-
const userQuery =
|
|
102
|
+
const userQuery = manager.createQuery(['user', 42], () => fetchUser(42), {
|
|
53
103
|
enabled: false,
|
|
54
104
|
});
|
|
55
105
|
await userQuery.refetch();
|
|
56
106
|
await userQuery.invalidate({ refetchType: 'none' });
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Why Tracked Invalidation
|
|
110
|
+
|
|
111
|
+
TanStack Query gives you flexible invalidation primitives, but the application still has to know which keys to invalidate after every mutation. Tracked invalidation moves that bookkeeping into the facade:
|
|
112
|
+
|
|
113
|
+
- queries declare their domain dependencies once in `queryKey[..., { deps, view }]`
|
|
114
|
+
- tracked mutations resolve the same dependency names from their variables
|
|
115
|
+
- the manager invalidates matching queries automatically
|
|
116
|
+
|
|
117
|
+
That changes the developer workflow from "remember which cache keys this mutation affects" to "describe which domain entities this query and mutation belong to".
|
|
118
|
+
|
|
119
|
+
Benefits:
|
|
120
|
+
|
|
121
|
+
- less manual cache invalidation code spread across features
|
|
122
|
+
- lower risk of stale UI because one dependent query was forgotten
|
|
123
|
+
- clearer separation between invalidation semantics in `deps` and UI variants in `view`
|
|
124
|
+
- typed paired helpers that remove repeated dependency mapping in the common case
|
|
125
|
+
|
|
126
|
+
## Examples
|
|
127
|
+
|
|
128
|
+
Tracked query keys use a final object segment:
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
['products', { deps: { applicationId, productId }, view: { page, sort } }]
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Rules:
|
|
135
|
+
|
|
136
|
+
- `deps` is required for tracked queries
|
|
137
|
+
- only `deps` is used for invalidation matching
|
|
138
|
+
- `view` is optional and recommended for pagination, sorting, filtering, and other cache variants
|
|
139
|
+
- tracked invalidation is manager-only because queries and mutations need one shared registry
|
|
140
|
+
|
|
141
|
+
### Paired Helper With Default Dependency Resolution
|
|
142
|
+
|
|
143
|
+
Use the paired helper when mutation variables already expose the dependency keys directly:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import { QueryClient } from '@tanstack/query-core';
|
|
147
|
+
import { setupQueryManager } from '@veams/status-quo-query';
|
|
148
|
+
|
|
149
|
+
const queryClient = new QueryClient();
|
|
150
|
+
const manager = setupQueryManager(queryClient);
|
|
151
|
+
const applicationId = 'app-1';
|
|
152
|
+
const productId = 'product-1';
|
|
153
|
+
|
|
154
|
+
const fetchProduct = async (currentApplicationId: string, currentProductId: string) => ({
|
|
155
|
+
applicationId: currentApplicationId,
|
|
156
|
+
name: 'Ada',
|
|
157
|
+
productId: currentProductId,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const saveProduct = async (variables: {
|
|
161
|
+
applicationId: string;
|
|
162
|
+
productId: string;
|
|
163
|
+
productName: string;
|
|
164
|
+
}) => variables;
|
|
165
|
+
|
|
166
|
+
const [createTrackedQuery, createTrackedMutation] = manager.createTrackedQueryAndMutation([
|
|
167
|
+
'applicationId',
|
|
168
|
+
'productId',
|
|
169
|
+
] as const);
|
|
170
|
+
|
|
171
|
+
const productQuery = createTrackedQuery(
|
|
172
|
+
['product', { deps: { applicationId, productId }, view: { page: 1 } }],
|
|
173
|
+
() => fetchProduct(applicationId, productId),
|
|
174
|
+
{ enabled: false }
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
const saveProductMutation = createTrackedMutation(saveProduct);
|
|
178
|
+
|
|
179
|
+
await saveProductMutation.mutate({
|
|
180
|
+
applicationId,
|
|
181
|
+
productId,
|
|
182
|
+
productName: 'New title',
|
|
183
|
+
});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
In this shape the mutation does not need `resolveDependencies`, because the paired helper already knows which dependency keys to read from the mutation variables.
|
|
187
|
+
|
|
188
|
+
### Default `intersection` Matching
|
|
189
|
+
|
|
190
|
+
`intersection` is the default. A mutation invalidates only queries that match all provided dependency pairs:
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
import { QueryClient } from '@tanstack/query-core';
|
|
194
|
+
import { setupQueryManager } from '@veams/status-quo-query';
|
|
195
|
+
|
|
196
|
+
const queryClient = new QueryClient();
|
|
197
|
+
const manager = setupQueryManager(queryClient);
|
|
198
|
+
|
|
199
|
+
const fetchProduct = async (applicationId: string, productId: string) => ({
|
|
200
|
+
applicationId,
|
|
201
|
+
name: 'Ada',
|
|
202
|
+
productId,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const saveProduct = async (variables: {
|
|
206
|
+
applicationId: string;
|
|
207
|
+
productId: string;
|
|
208
|
+
productName: string;
|
|
209
|
+
}) => variables;
|
|
210
|
+
|
|
211
|
+
const [createTrackedQuery, createTrackedMutation] = manager.createTrackedQueryAndMutation([
|
|
212
|
+
'applicationId',
|
|
213
|
+
'productId',
|
|
214
|
+
] as const);
|
|
215
|
+
|
|
216
|
+
createTrackedQuery(
|
|
217
|
+
['product', { deps: { applicationId: 'app-1', productId: 'product-1' }, view: { page: 1 } }],
|
|
218
|
+
() => fetchProduct('app-1', 'product-1')
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
createTrackedQuery(
|
|
222
|
+
['product', { deps: { applicationId: 'app-1', productId: 'product-2' }, view: { page: 1 } }],
|
|
223
|
+
() => fetchProduct('app-1', 'product-2')
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
const renameProduct = createTrackedMutation(saveProduct);
|
|
227
|
+
|
|
228
|
+
await renameProduct.mutate({
|
|
229
|
+
applicationId: 'app-1',
|
|
230
|
+
productId: 'product-1',
|
|
231
|
+
productName: 'Ada',
|
|
232
|
+
});
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Only the `app-1` / `product-1` query is invalidated.
|
|
236
|
+
|
|
237
|
+
### `union` Matching
|
|
238
|
+
|
|
239
|
+
Use `matchMode: 'union'` when a mutation should invalidate anything that matches any provided dependency pair:
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
import { QueryClient } from '@tanstack/query-core';
|
|
243
|
+
import { setupQueryManager } from '@veams/status-quo-query';
|
|
244
|
+
|
|
245
|
+
const queryClient = new QueryClient();
|
|
246
|
+
const manager = setupQueryManager(queryClient);
|
|
247
|
+
|
|
248
|
+
const fetchProduct = async (applicationId: string, productId: string) => ({
|
|
249
|
+
applicationId,
|
|
250
|
+
name: 'Ada',
|
|
251
|
+
productId,
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
const syncProductData = async (variables: {
|
|
255
|
+
applicationId: string;
|
|
256
|
+
productId: string;
|
|
257
|
+
}) => variables;
|
|
258
|
+
|
|
259
|
+
const [createTrackedQuery, createTrackedMutation] = manager.createTrackedQueryAndMutation([
|
|
260
|
+
'applicationId',
|
|
261
|
+
'productId',
|
|
262
|
+
] as const);
|
|
263
|
+
|
|
264
|
+
createTrackedQuery(
|
|
265
|
+
['product', { deps: { applicationId: 'app-1', productId: 'product-1' }, view: { page: 1 } }],
|
|
266
|
+
() => fetchProduct('app-1', 'product-1')
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
createTrackedQuery(
|
|
270
|
+
['product', { deps: { applicationId: 'app-2', productId: 'product-1' }, view: { page: 1 } }],
|
|
271
|
+
() => fetchProduct('app-2', 'product-1')
|
|
272
|
+
);
|
|
273
|
+
|
|
274
|
+
const syncProduct = createTrackedMutation(syncProductData, {
|
|
275
|
+
matchMode: 'union',
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
await syncProduct.mutate({
|
|
279
|
+
applicationId: 'app-1',
|
|
280
|
+
productId: 'product-1',
|
|
281
|
+
});
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
This invalidates tracked queries that match:
|
|
285
|
+
|
|
286
|
+
- `applicationId === 'app-1'`
|
|
287
|
+
- or `productId === 'product-1'`
|
|
288
|
+
|
|
289
|
+
Use it when a mutation affects a wider slice of cached state and exact intersection would be too narrow.
|
|
290
|
+
|
|
291
|
+
### Partial Dependency Invalidation
|
|
292
|
+
|
|
293
|
+
Tracked mutations may resolve only some dependency keys:
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
import { QueryClient } from '@tanstack/query-core';
|
|
297
|
+
import { setupQueryManager } from '@veams/status-quo-query';
|
|
298
|
+
|
|
299
|
+
const queryClient = new QueryClient();
|
|
300
|
+
const manager = setupQueryManager(queryClient);
|
|
301
|
+
|
|
302
|
+
const syncApplicationProducts = async (variables: { applicationId: string }) => variables;
|
|
303
|
+
|
|
304
|
+
const [createTrackedQuery, createTrackedMutation] = manager.createTrackedQueryAndMutation([
|
|
305
|
+
'applicationId',
|
|
306
|
+
'productId',
|
|
307
|
+
] as const);
|
|
308
|
+
|
|
309
|
+
const refreshApplicationProducts = createTrackedMutation(syncApplicationProducts);
|
|
310
|
+
|
|
311
|
+
await refreshApplicationProducts.mutate({
|
|
312
|
+
applicationId: 'app-1',
|
|
313
|
+
});
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
This invalidates all tracked queries that match `applicationId === 'app-1'`, regardless of `productId`.
|
|
317
|
+
|
|
318
|
+
### Lifecycle Timing
|
|
319
|
+
|
|
320
|
+
Automatic invalidation runs on success by default. Change `invalidateOn` when the mutation workflow needs different timing:
|
|
321
|
+
|
|
322
|
+
```ts
|
|
323
|
+
import { QueryClient } from '@tanstack/query-core';
|
|
324
|
+
import { setupQueryManager } from '@veams/status-quo-query';
|
|
325
|
+
|
|
326
|
+
const queryClient = new QueryClient();
|
|
327
|
+
const manager = setupQueryManager(queryClient);
|
|
328
|
+
|
|
329
|
+
const removeProduct = async (variables: { applicationId: string }) => variables;
|
|
330
|
+
|
|
331
|
+
const [createTrackedQuery, createTrackedMutation] = manager.createTrackedQueryAndMutation([
|
|
332
|
+
'applicationId',
|
|
333
|
+
] as const);
|
|
334
|
+
|
|
335
|
+
const cleanupMutation = createTrackedMutation(removeProduct, {
|
|
336
|
+
invalidateOn: 'settled',
|
|
337
|
+
});
|
|
338
|
+
```
|
|
57
339
|
|
|
58
|
-
|
|
59
|
-
await updateUser.mutate({ id: 42 });
|
|
340
|
+
Supported values:
|
|
60
341
|
|
|
61
|
-
|
|
62
|
-
|
|
342
|
+
- `'success'` invalidates only after a successful mutation
|
|
343
|
+
- `'error'` invalidates only after a failed mutation
|
|
344
|
+
- `'settled'` invalidates after either outcome
|
|
345
|
+
|
|
346
|
+
### Custom Dependency Resolution
|
|
347
|
+
|
|
348
|
+
Use `resolveDependencies` when mutation variables do not expose the tracked keys directly:
|
|
349
|
+
|
|
350
|
+
```ts
|
|
351
|
+
import { QueryClient } from '@tanstack/query-core';
|
|
352
|
+
import { setupQueryManager } from '@veams/status-quo-query';
|
|
353
|
+
|
|
354
|
+
const queryClient = new QueryClient();
|
|
355
|
+
const manager = setupQueryManager(queryClient);
|
|
356
|
+
|
|
357
|
+
const saveProduct = async (variables: {
|
|
358
|
+
payload: { applicationId: string };
|
|
359
|
+
product: { id: string };
|
|
360
|
+
productName: string;
|
|
361
|
+
}) => variables;
|
|
362
|
+
|
|
363
|
+
const nestedMutation = manager.createTrackedMutation(saveProduct, {
|
|
364
|
+
resolveDependencies: (variables: {
|
|
365
|
+
payload: { applicationId: string };
|
|
366
|
+
product: { id: string };
|
|
367
|
+
}) => ({
|
|
368
|
+
applicationId: variables.payload.applicationId,
|
|
369
|
+
productId: variables.product.id,
|
|
370
|
+
}),
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
await nestedMutation.mutate({
|
|
374
|
+
payload: { applicationId: 'app-1' },
|
|
375
|
+
product: { id: 'product-1' },
|
|
376
|
+
});
|
|
63
377
|
```
|
|
64
378
|
|
|
379
|
+
Standalone tracked mutations need either:
|
|
380
|
+
|
|
381
|
+
- `dependencyKeys`
|
|
382
|
+
- or `resolveDependencies`
|
|
383
|
+
|
|
65
384
|
## API
|
|
66
385
|
|
|
67
|
-
### `
|
|
386
|
+
### `setupQueryManager(queryClient)`
|
|
68
387
|
|
|
69
|
-
Creates the package-level
|
|
388
|
+
Creates the package-level query manager facade around an existing TanStack `QueryClient`.
|
|
70
389
|
|
|
71
|
-
Returns `
|
|
390
|
+
Returns `QueryManager` with:
|
|
72
391
|
|
|
73
392
|
- `createQuery(queryKey, queryFn, options?)`
|
|
74
393
|
- `createMutation(mutationFn, options?)`
|
|
394
|
+
- `createTrackedQuery(queryKey, queryFn, options?)`
|
|
395
|
+
- `createTrackedMutation(mutationFn, options?)`
|
|
396
|
+
- `createTrackedQueryAndMutation(dependencyKeys)`
|
|
75
397
|
- `cancelQueries(...)`
|
|
76
398
|
- `getQueryData(...)`
|
|
77
399
|
- `invalidateQueries(...)`
|
|
@@ -81,7 +403,50 @@ Returns `CacheApi` with:
|
|
|
81
403
|
- `setQueryData(...)`
|
|
82
404
|
- `unsafe_getClient()`
|
|
83
405
|
|
|
84
|
-
All
|
|
406
|
+
All manager methods forward directly to the corresponding `QueryClient` methods. `unsafe_getClient()` returns the raw TanStack client as an explicit escape hatch.
|
|
407
|
+
|
|
408
|
+
### Tracked Queries and Mutations
|
|
409
|
+
|
|
410
|
+
Tracked queries embed dependency metadata into the final query-key segment:
|
|
411
|
+
|
|
412
|
+
```ts
|
|
413
|
+
['products', { deps: { applicationId, productId }, view: { page, sort } }]
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
Only `deps` participates in automatic invalidation tracking. `view` is optional and is treated as normal query-key data.
|
|
417
|
+
|
|
418
|
+
`createTrackedQuery(queryKey, queryFn, options?)` returns the same `QueryService<TData, TError>` shape as `createQuery(...)`, but it registers the query hash under every `deps` entry and re-registers on `refetch()` or the first `subscribe(...)` if TanStack has removed the cache entry in the meantime.
|
|
419
|
+
|
|
420
|
+
`createTrackedMutation(mutationFn, options?)` returns the same `MutationService<TData, TError, TVariables, TOnMutateResult>` shape as `createMutation(...)`, but adds:
|
|
421
|
+
|
|
422
|
+
- `dependencyKeys?`
|
|
423
|
+
- `resolveDependencies?`
|
|
424
|
+
- `invalidateOn?` with `'success' | 'error' | 'settled'`
|
|
425
|
+
- `matchMode?` with `'intersection' | 'union'`
|
|
426
|
+
|
|
427
|
+
Standalone tracked mutations need either `dependencyKeys` or `resolveDependencies`.
|
|
428
|
+
|
|
429
|
+
`createTrackedQueryAndMutation(dependencyKeys)` captures dependency keys once and returns:
|
|
430
|
+
|
|
431
|
+
- the tracked query factory
|
|
432
|
+
- a tracked mutation factory whose default resolver reads `variables[dependencyKey]`
|
|
433
|
+
|
|
434
|
+
Use `resolveDependencies` when the mutation variables do not expose the tracked dependency fields directly.
|
|
435
|
+
|
|
436
|
+
### `createTrackedQueryAndMutation(dependencyKeys)`
|
|
437
|
+
|
|
438
|
+
Captures dependency names once and returns:
|
|
439
|
+
|
|
440
|
+
- the tracked query factory
|
|
441
|
+
- a tracked mutation factory whose default resolver reads `variables[dependencyKey]`
|
|
442
|
+
|
|
443
|
+
The tracked query factory still expects a query key with a final `{ deps, view? }` segment. The tracked mutation factory keeps the same `MutationService` shape as `createMutation(...)`, but no longer needs `dependencyKeys` repeated in each call.
|
|
444
|
+
|
|
445
|
+
Reach for standalone `createTrackedMutation(...)` when:
|
|
446
|
+
|
|
447
|
+
- query and mutation do not share one dependency-key list
|
|
448
|
+
- mutation variables need a custom `resolveDependencies(...)`
|
|
449
|
+
- you want one tracked mutation without pairing it to one tracked query workflow
|
|
85
450
|
|
|
86
451
|
### `setupQuery(queryClient)`
|
|
87
452
|
|
|
@@ -170,4 +535,5 @@ Creates a `createMutation` factory bound to a `QueryClient`.
|
|
|
170
535
|
- `getSnapshot()` always returns passive state only.
|
|
171
536
|
- Commands live on the handle itself: `refetch`, `invalidate`, `mutate`, `reset`.
|
|
172
537
|
- Raw TanStack observer and client access is explicit through `unsafe_getResult()` and `unsafe_getClient()`.
|
|
173
|
-
-
|
|
538
|
+
- Manager-level operations live on `setupQueryManager()`, not on individual snapshots.
|
|
539
|
+
- Tracked invalidation is manager-only because the registry must be shared across query and mutation handles.
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
// Re-export all mutation-related types and functions.
|
|
1
2
|
export * from './mutation';
|
|
3
|
+
// Re-export all query-related types and functions.
|
|
2
4
|
export * from './query';
|
|
5
|
+
// Re-export all provider-related types and functions for cache management.
|
|
3
6
|
export * from './provider';
|
|
4
7
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,cAAc,YAAY,CAAC;AAC3B,mDAAmD;AACnD,cAAc,SAAS,CAAC;AACxB,2EAA2E;AAC3E,cAAc,YAAY,CAAC"}
|
package/dist/mutation.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { type MutationFunction, type MutateOptions, type MutationObserverOptions, type MutationObserverResult, type MutationStatus as TanstackMutationStatus, type QueryClient } from '@tanstack/query-core';
|
|
2
|
+
import { type TrackedDependencyRecord, type TrackingRegistry, type TrackedInvalidateOn, type TrackedMatchMode } from './tracking';
|
|
2
3
|
export type MutationStatus = TanstackMutationStatus;
|
|
4
|
+
/**
|
|
5
|
+
* Represents a stable snapshot of the mutation service's state.
|
|
6
|
+
*/
|
|
3
7
|
export interface MutationServiceSnapshot<TData = unknown, TError = Error, TVariables = void> {
|
|
4
8
|
data: TData | undefined;
|
|
5
9
|
error: TError | null;
|
|
@@ -10,6 +14,9 @@ export interface MutationServiceSnapshot<TData = unknown, TError = Error, TVaria
|
|
|
10
14
|
isPending: boolean;
|
|
11
15
|
isSuccess: boolean;
|
|
12
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Defines the public API for a mutation service.
|
|
19
|
+
*/
|
|
13
20
|
export interface MutationService<TData = unknown, TError = Error, TVariables = void, TOnMutateResult = unknown> {
|
|
14
21
|
getSnapshot: () => MutationServiceSnapshot<TData, TError, TVariables>;
|
|
15
22
|
subscribe: (listener: (snapshot: MutationServiceSnapshot<TData, TError, TVariables>) => void) => () => void;
|
|
@@ -17,8 +24,44 @@ export interface MutationService<TData = unknown, TError = Error, TVariables = v
|
|
|
17
24
|
reset: () => void;
|
|
18
25
|
unsafe_getResult: () => MutationObserverResult<TData, TError, TVariables, TOnMutateResult>;
|
|
19
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Configuration options for creating a mutation service, excluding the mutation function itself.
|
|
29
|
+
*/
|
|
20
30
|
export type MutationServiceOptions<TData = unknown, TError = Error, TVariables = void, TOnMutateResult = unknown> = Omit<MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>, 'mutationFn'>;
|
|
31
|
+
/**
|
|
32
|
+
* Function signature for the mutation factory.
|
|
33
|
+
*/
|
|
21
34
|
export interface CreateMutation {
|
|
22
35
|
<TData = unknown, TError = Error, TVariables = void, TOnMutateResult = unknown>(mutationFn: MutationFunction<TData, TVariables>, options?: MutationServiceOptions<TData, TError, TVariables, TOnMutateResult>): MutationService<TData, TError, TVariables, TOnMutateResult>;
|
|
23
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Additional options for tracked mutations that invalidate queries automatically.
|
|
39
|
+
*
|
|
40
|
+
* The tracked mutation still behaves like a normal mutation service from the outside. These
|
|
41
|
+
* options only describe how the facade should derive dependency values and when it should
|
|
42
|
+
* invalidate matching tracked queries after the mutation lifecycle settles.
|
|
43
|
+
*/
|
|
44
|
+
export interface TrackedMutationServiceOptions<TDeps extends TrackedDependencyRecord = TrackedDependencyRecord, TData = unknown, TError = Error, TVariables = void, TOnMutateResult = unknown> extends MutationServiceOptions<TData, TError, TVariables, TOnMutateResult> {
|
|
45
|
+
dependencyKeys?: readonly (keyof TDeps & string)[];
|
|
46
|
+
resolveDependencies?: (variables: TVariables) => Partial<TDeps>;
|
|
47
|
+
invalidateOn?: TrackedInvalidateOn;
|
|
48
|
+
matchMode?: TrackedMatchMode;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Function signature for tracked mutation factories.
|
|
52
|
+
*/
|
|
53
|
+
export interface CreateTrackedMutation {
|
|
54
|
+
<TDeps extends TrackedDependencyRecord = TrackedDependencyRecord, TData = unknown, TError = Error, TVariables = void, TOnMutateResult = unknown>(mutationFn: MutationFunction<TData, TVariables>, options?: TrackedMutationServiceOptions<TDeps, TData, TError, TVariables, TOnMutateResult>): MutationService<TData, TError, TVariables, TOnMutateResult>;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Prepares the mutation factory by binding it to a specific QueryClient instance.
|
|
58
|
+
*/
|
|
24
59
|
export declare function setupMutation(queryClient: QueryClient): CreateMutation;
|
|
60
|
+
/**
|
|
61
|
+
* Prepares a tracked mutation factory that coordinates invalidation through the shared registry.
|
|
62
|
+
*
|
|
63
|
+
* The implementation intentionally wraps the normal mutation service instead of re-implementing
|
|
64
|
+
* TanStack lifecycle behavior. TanStack still owns retries, callbacks, and state transitions;
|
|
65
|
+
* the facade only adds dependency resolution plus the follow-up invalidation pass.
|
|
66
|
+
*/
|
|
67
|
+
export declare function setupTrackedMutation(queryClient: QueryClient, trackingRegistry: TrackingRegistry, defaultDependencyKeys?: readonly string[]): CreateTrackedMutation;
|