@tanstack/lit-query 0.2.25 → 0.2.27
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/build/legacy/index.cjs +143 -29
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +24 -36
- package/build/legacy/index.d.cts.map +1 -1
- package/build/legacy/index.d.ts +24 -36
- package/build/legacy/index.d.ts.map +1 -1
- package/build/legacy/index.js +143 -29
- package/build/legacy/index.js.map +1 -1
- package/build/modern/index.cjs +143 -29
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +24 -36
- package/build/modern/index.d.cts.map +1 -1
- package/build/modern/index.d.ts +24 -36
- package/build/modern/index.d.ts.map +1 -1
- package/build/modern/index.js +143 -29
- package/build/modern/index.js.map +1 -1
- package/package.json +18 -19
- package/src/QueryClientProvider.ts +34 -14
- package/src/accessor.ts +11 -2
- package/src/context.ts +11 -5
- package/src/controllers/BaseController.ts +77 -4
- package/src/createInfiniteQueryController.ts +22 -23
- package/src/createMutationController.ts +25 -38
- package/src/createQueriesController.ts +42 -32
- package/src/createQueryController.ts +22 -29
- package/src/infiniteQueryOptions.ts +0 -2
- package/src/mutationOptions.ts +0 -2
- package/src/queryObserverResultTracker.ts +15 -0
- package/src/queryOptions.ts +2 -5
- package/src/useIsFetching.ts +7 -13
- package/src/useIsMutating.ts +7 -13
- package/src/useMutationState.ts +17 -23
|
@@ -1,26 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
replaceEqualDeep,
|
|
4
|
-
type DefaultError,
|
|
5
|
-
type DefinedQueryObserverResult,
|
|
6
|
-
type OmitKeyof,
|
|
7
|
-
type QueriesObserverOptions,
|
|
8
|
-
type QueryFunction,
|
|
9
|
-
type QueryKey,
|
|
10
|
-
type QueryObserverOptions,
|
|
11
|
-
type QueryObserverResult,
|
|
12
|
-
type ThrowOnError,
|
|
13
|
-
} from '@tanstack/query-core'
|
|
14
|
-
import type { QueryClient } from '@tanstack/query-core'
|
|
15
|
-
import type { ReactiveControllerHost } from 'lit'
|
|
16
|
-
import {
|
|
17
|
-
createValueAccessor,
|
|
18
|
-
readAccessor,
|
|
19
|
-
type Accessor,
|
|
20
|
-
type ValueAccessor,
|
|
21
|
-
} from './accessor.js'
|
|
1
|
+
import { QueriesObserver, replaceEqualDeep } from '@tanstack/query-core'
|
|
2
|
+
import { createValueAccessor, readAccessor } from './accessor.js'
|
|
22
3
|
import { createMissingQueryClientError } from './context.js'
|
|
23
4
|
import { BaseController } from './controllers/BaseController.js'
|
|
5
|
+
import type { Accessor, ValueAccessor } from './accessor.js'
|
|
6
|
+
import type {
|
|
7
|
+
DefaultError,
|
|
8
|
+
DefinedQueryObserverResult,
|
|
9
|
+
OmitKeyof,
|
|
10
|
+
QueriesObserverOptions,
|
|
11
|
+
QueryClient,
|
|
12
|
+
QueryFunction,
|
|
13
|
+
QueryKey,
|
|
14
|
+
QueryObserverOptions,
|
|
15
|
+
QueryObserverResult,
|
|
16
|
+
ThrowOnError,
|
|
17
|
+
} from '@tanstack/query-core'
|
|
18
|
+
import type { ReactiveControllerHost } from 'lit'
|
|
24
19
|
|
|
25
20
|
/**
|
|
26
21
|
* Options for one query inside `createQueriesController`.
|
|
@@ -221,6 +216,11 @@ export type QueriesResultAccessor<TCombinedResult> =
|
|
|
221
216
|
destroy: () => void
|
|
222
217
|
}
|
|
223
218
|
|
|
219
|
+
/**
|
|
220
|
+
* Returns the result used while no `QueryClient` is available: `'pending'` and idle, with methods
|
|
221
|
+
* that reject with the missing client error.
|
|
222
|
+
* @returns A new result object in that state.
|
|
223
|
+
*/
|
|
224
224
|
function createPendingQueryObserverResult(): QueryObserverResult {
|
|
225
225
|
return {
|
|
226
226
|
data: undefined,
|
|
@@ -254,6 +254,12 @@ function createPendingQueryObserverResult(): QueryObserverResult {
|
|
|
254
254
|
} as unknown as QueryObserverResult
|
|
255
255
|
}
|
|
256
256
|
|
|
257
|
+
/**
|
|
258
|
+
* Returns the result of a query used while no `QueryClient` is available: a `'success'` result with
|
|
259
|
+
* the query's `initialData` (after `select`), or the pending result if there is no `initialData`.
|
|
260
|
+
* @param query - The query options.
|
|
261
|
+
* @returns The `'success'` result with `initialData`, or a pending one.
|
|
262
|
+
*/
|
|
257
263
|
function createPlaceholderQueryObserverResult(
|
|
258
264
|
query: QueryObserverOptions,
|
|
259
265
|
): QueryObserverResult {
|
|
@@ -284,6 +290,12 @@ function createPlaceholderQueryObserverResult(
|
|
|
284
290
|
} as QueryObserverResult
|
|
285
291
|
}
|
|
286
292
|
|
|
293
|
+
/**
|
|
294
|
+
* Reads the controller's options and defaults each query with the client.
|
|
295
|
+
* @param optionsAccessor - The options, or a getter that returns them.
|
|
296
|
+
* @param client - The client used to default the query options.
|
|
297
|
+
* @returns The defaulted query options and the `combine` function.
|
|
298
|
+
*/
|
|
287
299
|
function resolveQueriesOptions<TCombinedResult>(
|
|
288
300
|
optionsAccessor: Accessor<
|
|
289
301
|
CreateQueriesControllerOptions<any, TCombinedResult>
|
|
@@ -354,7 +366,7 @@ class QueriesController<
|
|
|
354
366
|
this.tryInitializeExplicitClient(queryClient)
|
|
355
367
|
}
|
|
356
368
|
|
|
357
|
-
protected onConnected(): void {
|
|
369
|
+
protected override onConnected(): void {
|
|
358
370
|
if (!this.syncClient()) {
|
|
359
371
|
return
|
|
360
372
|
}
|
|
@@ -363,12 +375,12 @@ class QueriesController<
|
|
|
363
375
|
this.subscribe()
|
|
364
376
|
}
|
|
365
377
|
|
|
366
|
-
protected onDisconnected(): void {
|
|
378
|
+
protected override onDisconnected(): void {
|
|
367
379
|
this.unsubscribeObserver()
|
|
368
380
|
this.syncClient()
|
|
369
381
|
}
|
|
370
382
|
|
|
371
|
-
protected onHostUpdate(): void {
|
|
383
|
+
protected override onHostUpdate(): void {
|
|
372
384
|
if (!this.shouldRefreshOnHostUpdate()) {
|
|
373
385
|
return
|
|
374
386
|
}
|
|
@@ -378,7 +390,7 @@ class QueriesController<
|
|
|
378
390
|
}
|
|
379
391
|
}
|
|
380
392
|
|
|
381
|
-
protected onQueryClientChanged(): void {
|
|
393
|
+
protected override onQueryClientChanged(): void {
|
|
382
394
|
if (!this.syncClient() || !this.connectedState) {
|
|
383
395
|
return
|
|
384
396
|
}
|
|
@@ -411,7 +423,7 @@ class QueriesController<
|
|
|
411
423
|
this.combine = combine
|
|
412
424
|
const observer = new QueriesObserver(queryClient, this.queries, {
|
|
413
425
|
combine: this.combine,
|
|
414
|
-
}
|
|
426
|
+
})
|
|
415
427
|
this.queryClient = queryClient
|
|
416
428
|
this.observer = observer
|
|
417
429
|
this.assignObserverResult(observer.getCurrentResult(), true)
|
|
@@ -471,7 +483,7 @@ class QueriesController<
|
|
|
471
483
|
this.combine = combine
|
|
472
484
|
this.observer = new QueriesObserver(this.queryClient, this.queries, {
|
|
473
485
|
combine: this.combine,
|
|
474
|
-
}
|
|
486
|
+
})
|
|
475
487
|
this.setObserverResult(this.observer.getCurrentResult(), true)
|
|
476
488
|
this.placeholderInitialized = true
|
|
477
489
|
return true
|
|
@@ -488,7 +500,7 @@ class QueriesController<
|
|
|
488
500
|
|
|
489
501
|
this.observer.setQueries(this.queries, {
|
|
490
502
|
combine: this.combine,
|
|
491
|
-
}
|
|
503
|
+
})
|
|
492
504
|
|
|
493
505
|
this.setObserverResult(this.observer.getCurrentResult(), true)
|
|
494
506
|
return true
|
|
@@ -529,7 +541,7 @@ class QueriesController<
|
|
|
529
541
|
this.combinedResult = replaceEqualDeep(
|
|
530
542
|
this.combinedResult,
|
|
531
543
|
combine(trackedResult),
|
|
532
|
-
)
|
|
544
|
+
)
|
|
533
545
|
|
|
534
546
|
return this.combinedResult
|
|
535
547
|
}
|
|
@@ -613,7 +625,7 @@ class QueriesController<
|
|
|
613
625
|
)
|
|
614
626
|
return (
|
|
615
627
|
resolvedOptions.combine
|
|
616
|
-
? resolvedOptions.combine(placeholders
|
|
628
|
+
? resolvedOptions.combine(placeholders)
|
|
617
629
|
: placeholders
|
|
618
630
|
) as TCombinedResult
|
|
619
631
|
}
|
|
@@ -664,7 +676,6 @@ class QueriesController<
|
|
|
664
676
|
*
|
|
665
677
|
* If `queryClient` is omitted, the controller resolves the client from the
|
|
666
678
|
* nearest connected `QueryClientProvider`.
|
|
667
|
-
*
|
|
668
679
|
* @param host - The Lit reactive controller host that owns the queries
|
|
669
680
|
* subscription.
|
|
670
681
|
* @param options - Queries controller options, or a getter that returns options.
|
|
@@ -672,7 +683,6 @@ class QueriesController<
|
|
|
672
683
|
* controllers that should not resolve a client from Lit context.
|
|
673
684
|
* @returns An accessor for the latest query results, or the value returned by
|
|
674
685
|
* `combine`.
|
|
675
|
-
*
|
|
676
686
|
* @example
|
|
677
687
|
* ```ts
|
|
678
688
|
* import { LitElement, html } from 'lit'
|
|
@@ -1,22 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
type DefaultError,
|
|
4
|
-
type DefaultedQueryObserverOptions,
|
|
5
|
-
type QueryKey,
|
|
6
|
-
type QueryObserverOptions,
|
|
7
|
-
type QueryObserverResult,
|
|
8
|
-
} from '@tanstack/query-core'
|
|
9
|
-
import type { QueryClient } from '@tanstack/query-core'
|
|
10
|
-
import type { ReactiveControllerHost } from 'lit'
|
|
11
|
-
import {
|
|
12
|
-
createValueAccessor,
|
|
13
|
-
readAccessor,
|
|
14
|
-
type Accessor,
|
|
15
|
-
type ValueAccessor,
|
|
16
|
-
} from './accessor.js'
|
|
1
|
+
import { QueryObserver } from '@tanstack/query-core'
|
|
2
|
+
import { createValueAccessor, readAccessor } from './accessor.js'
|
|
17
3
|
import { createMissingQueryClientError } from './context.js'
|
|
18
4
|
import { BaseController } from './controllers/BaseController.js'
|
|
19
5
|
import { QueryObserverResultTracker } from './queryObserverResultTracker.js'
|
|
6
|
+
import type { Accessor, ValueAccessor } from './accessor.js'
|
|
7
|
+
import type {
|
|
8
|
+
DefaultError,
|
|
9
|
+
DefaultedQueryObserverOptions,
|
|
10
|
+
QueryClient,
|
|
11
|
+
QueryKey,
|
|
12
|
+
QueryObserverOptions,
|
|
13
|
+
QueryObserverResult,
|
|
14
|
+
} from '@tanstack/query-core'
|
|
15
|
+
import type { ReactiveControllerHost } from 'lit'
|
|
20
16
|
|
|
21
17
|
/**
|
|
22
18
|
* Options accepted by `createQueryController`.
|
|
@@ -50,6 +46,11 @@ export type QueryResultAccessor<TData, TError> = ValueAccessor<
|
|
|
50
46
|
destroy: () => void
|
|
51
47
|
}
|
|
52
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Returns the result used while no `QueryClient` is available: `'pending'` and idle, with methods
|
|
51
|
+
* that reject with the missing client error.
|
|
52
|
+
* @returns A new result object in that state.
|
|
53
|
+
*/
|
|
53
54
|
function createPendingQueryResult<TData, TError>(): QueryObserverResult<
|
|
54
55
|
TData,
|
|
55
56
|
TError
|
|
@@ -132,7 +133,7 @@ class QueryController<
|
|
|
132
133
|
this.assignObserverResult(observer.getOptimisticResult(defaulted))
|
|
133
134
|
}
|
|
134
135
|
|
|
135
|
-
protected onConnected(): void {
|
|
136
|
+
protected override onConnected(): void {
|
|
136
137
|
if (!this.syncClient()) {
|
|
137
138
|
return
|
|
138
139
|
}
|
|
@@ -145,12 +146,12 @@ class QueryController<
|
|
|
145
146
|
}
|
|
146
147
|
}
|
|
147
148
|
|
|
148
|
-
protected onDisconnected(): void {
|
|
149
|
+
protected override onDisconnected(): void {
|
|
149
150
|
this.unsubscribeObserver()
|
|
150
151
|
this.syncClient()
|
|
151
152
|
}
|
|
152
153
|
|
|
153
|
-
protected onHostUpdate(): void {
|
|
154
|
+
protected override onHostUpdate(): void {
|
|
154
155
|
if (typeof this.options !== 'function') {
|
|
155
156
|
return
|
|
156
157
|
}
|
|
@@ -158,7 +159,7 @@ class QueryController<
|
|
|
158
159
|
this.refreshOptions()
|
|
159
160
|
}
|
|
160
161
|
|
|
161
|
-
protected onQueryClientChanged(): void {
|
|
162
|
+
protected override onQueryClientChanged(): void {
|
|
162
163
|
if (!this.syncClient()) {
|
|
163
164
|
return
|
|
164
165
|
}
|
|
@@ -300,13 +301,7 @@ class QueryController<
|
|
|
300
301
|
this.queryClient = resolvedClient
|
|
301
302
|
const defaulted = resolvedClient.defaultQueryOptions(
|
|
302
303
|
readAccessor(this.options),
|
|
303
|
-
)
|
|
304
|
-
TQueryFnData,
|
|
305
|
-
TError,
|
|
306
|
-
TData,
|
|
307
|
-
TQueryData,
|
|
308
|
-
TQueryKey
|
|
309
|
-
>
|
|
304
|
+
)
|
|
310
305
|
;(defaulted as { _optimisticResults?: 'optimistic' })._optimisticResults =
|
|
311
306
|
'optimistic'
|
|
312
307
|
return defaulted
|
|
@@ -322,14 +317,12 @@ class QueryController<
|
|
|
322
317
|
*
|
|
323
318
|
* If `queryClient` is omitted, the controller resolves the client from the
|
|
324
319
|
* nearest connected `QueryClientProvider`.
|
|
325
|
-
*
|
|
326
320
|
* @param host - The Lit reactive controller host that owns the query
|
|
327
321
|
* subscription.
|
|
328
322
|
* @param options - Query observer options, or a getter that returns options.
|
|
329
323
|
* @param queryClient - Optional explicit query client. Provide this for
|
|
330
324
|
* controllers that should not resolve a client from Lit context.
|
|
331
325
|
* @returns An accessor for the latest query result with query helper methods.
|
|
332
|
-
*
|
|
333
326
|
* @example
|
|
334
327
|
* ```ts
|
|
335
328
|
* import { LitElement, html } from 'lit'
|
|
@@ -9,10 +9,8 @@ import type {
|
|
|
9
9
|
/**
|
|
10
10
|
* Brands infinite query options so the `queryKey` carries the infinite query
|
|
11
11
|
* data and error types across TanStack Query APIs.
|
|
12
|
-
*
|
|
13
12
|
* @param options - Infinite query options to preserve and brand.
|
|
14
13
|
* @returns The same options object with a typed `queryKey`.
|
|
15
|
-
*
|
|
16
14
|
* @example
|
|
17
15
|
* ```ts
|
|
18
16
|
* import { infiniteQueryOptions } from '@tanstack/lit-query'
|
package/src/mutationOptions.ts
CHANGED
|
@@ -5,10 +5,8 @@ import type {
|
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Preserves and types mutation options for reuse across Lit Query APIs.
|
|
8
|
-
*
|
|
9
8
|
* @param options - Mutation options to preserve.
|
|
10
9
|
* @returns The same options object.
|
|
11
|
-
*
|
|
12
10
|
* @example
|
|
13
11
|
* ```ts
|
|
14
12
|
* import { mutationOptions } from '@tanstack/lit-query'
|
|
@@ -3,15 +3,30 @@ type TrackableQueryObserver<TResult extends object> = {
|
|
|
3
3
|
trackResult: (result: TResult) => unknown
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Wraps observer results with property tracking when `notifyOnChangeProps` is
|
|
8
|
+
* not set, so the observer only notifies about the properties the host reads.
|
|
9
|
+
*/
|
|
6
10
|
export class QueryObserverResultTracker<TResult extends object> {
|
|
7
11
|
private result: TResult | undefined
|
|
8
12
|
private usesTracking = false
|
|
9
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Forgets the last result, so the next `update` returns a result again.
|
|
16
|
+
*/
|
|
10
17
|
reset(): void {
|
|
11
18
|
this.result = undefined
|
|
12
19
|
this.usesTracking = false
|
|
13
20
|
}
|
|
14
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Returns the result to expose, tracked through the observer when it uses
|
|
24
|
+
* property tracking.
|
|
25
|
+
* @param observer - The observer the result comes from, if any.
|
|
26
|
+
* @param result - The latest result of the observer.
|
|
27
|
+
* @returns The result to expose, or `undefined` if neither the result nor
|
|
28
|
+
* the tracking mode changed since the last call.
|
|
29
|
+
*/
|
|
15
30
|
update(
|
|
16
31
|
observer: TrackableQueryObserver<TResult> | undefined,
|
|
17
32
|
result: TResult,
|
package/src/queryOptions.ts
CHANGED
|
@@ -23,7 +23,8 @@ export type DefinedInitialDataOptions<
|
|
|
23
23
|
'queryFn'
|
|
24
24
|
> & {
|
|
25
25
|
initialData:
|
|
26
|
-
|
|
26
|
+
| NonUndefinedGuard<TQueryFnData>
|
|
27
|
+
| (() => NonUndefinedGuard<TQueryFnData>)
|
|
27
28
|
queryFn?: QueryFunction<TQueryFnData, TQueryKey>
|
|
28
29
|
}
|
|
29
30
|
|
|
@@ -75,10 +76,8 @@ export type UndefinedInitialDataOptions<
|
|
|
75
76
|
/**
|
|
76
77
|
* Brands query options so the `queryKey` carries the query function data and
|
|
77
78
|
* error types across TanStack Query APIs.
|
|
78
|
-
*
|
|
79
79
|
* @param options - Query options to preserve and brand.
|
|
80
80
|
* @returns The same options object with a typed `queryKey`.
|
|
81
|
-
*
|
|
82
81
|
* @example
|
|
83
82
|
* ```ts
|
|
84
83
|
* import { queryOptions } from '@tanstack/lit-query'
|
|
@@ -104,7 +103,6 @@ export function queryOptions<
|
|
|
104
103
|
/**
|
|
105
104
|
* Brands query options so the `queryKey` carries the query function data and
|
|
106
105
|
* error types across TanStack Query APIs.
|
|
107
|
-
*
|
|
108
106
|
* @param options - Query options to preserve and brand.
|
|
109
107
|
* @returns The same options object with a typed `queryKey`.
|
|
110
108
|
*/
|
|
@@ -122,7 +120,6 @@ export function queryOptions<
|
|
|
122
120
|
/**
|
|
123
121
|
* Brands query options so the `queryKey` carries the query function data and
|
|
124
122
|
* error types across TanStack Query APIs.
|
|
125
|
-
*
|
|
126
123
|
* @param options - Query options to preserve and brand.
|
|
127
124
|
* @returns The same options object with a typed `queryKey`.
|
|
128
125
|
*/
|
package/src/useIsFetching.ts
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
|
+
import { createValueAccessor, readAccessor } from './accessor.js'
|
|
2
|
+
import { BaseController } from './controllers/BaseController.js'
|
|
3
|
+
import type { Accessor, ValueAccessor } from './accessor.js'
|
|
1
4
|
import type { QueryClient, QueryFilters } from '@tanstack/query-core'
|
|
2
5
|
import type { ReactiveControllerHost } from 'lit'
|
|
3
|
-
import {
|
|
4
|
-
createValueAccessor,
|
|
5
|
-
readAccessor,
|
|
6
|
-
type Accessor,
|
|
7
|
-
type ValueAccessor,
|
|
8
|
-
} from './accessor.js'
|
|
9
|
-
import { BaseController } from './controllers/BaseController.js'
|
|
10
6
|
|
|
11
7
|
/**
|
|
12
8
|
* Accessor returned by `useIsFetching`.
|
|
@@ -35,7 +31,7 @@ class IsFetchingController extends BaseController<number> {
|
|
|
35
31
|
this.result = this.computeValue()
|
|
36
32
|
}
|
|
37
33
|
|
|
38
|
-
protected onConnected(): void {
|
|
34
|
+
protected override onConnected(): void {
|
|
39
35
|
if (!this.syncClient()) {
|
|
40
36
|
this.setResult(0)
|
|
41
37
|
return
|
|
@@ -45,13 +41,13 @@ class IsFetchingController extends BaseController<number> {
|
|
|
45
41
|
this.setResult(this.computeValue())
|
|
46
42
|
}
|
|
47
43
|
|
|
48
|
-
protected onDisconnected(): void {
|
|
44
|
+
protected override onDisconnected(): void {
|
|
49
45
|
this.unsubscribe?.()
|
|
50
46
|
this.unsubscribe = undefined
|
|
51
47
|
this.syncClient()
|
|
52
48
|
}
|
|
53
49
|
|
|
54
|
-
protected onHostUpdate(): void {
|
|
50
|
+
protected override onHostUpdate(): void {
|
|
55
51
|
if (typeof this.filters !== 'function') {
|
|
56
52
|
return
|
|
57
53
|
}
|
|
@@ -59,7 +55,7 @@ class IsFetchingController extends BaseController<number> {
|
|
|
59
55
|
this.setResult(this.syncClient() ? this.computeValue() : 0)
|
|
60
56
|
}
|
|
61
57
|
|
|
62
|
-
protected onQueryClientChanged(): void {
|
|
58
|
+
protected override onQueryClientChanged(): void {
|
|
63
59
|
if (!this.syncClient()) {
|
|
64
60
|
this.setResult(0)
|
|
65
61
|
return
|
|
@@ -120,14 +116,12 @@ class IsFetchingController extends BaseController<number> {
|
|
|
120
116
|
* When `filters` is a function, it is re-read during host updates so the count
|
|
121
117
|
* can follow reactive host state. If `queryClient` is omitted, the controller
|
|
122
118
|
* resolves the client from the nearest connected `QueryClientProvider`.
|
|
123
|
-
*
|
|
124
119
|
* @param host - The Lit reactive controller host that owns the cache
|
|
125
120
|
* subscription.
|
|
126
121
|
* @param filters - Query filters, or a getter that returns query filters.
|
|
127
122
|
* @param queryClient - Optional explicit query client. Provide this for
|
|
128
123
|
* controllers that should not resolve a client from Lit context.
|
|
129
124
|
* @returns An accessor for the current number of matching fetching queries.
|
|
130
|
-
*
|
|
131
125
|
* @example
|
|
132
126
|
* ```ts
|
|
133
127
|
* import { LitElement, html } from 'lit'
|
package/src/useIsMutating.ts
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
|
+
import { createValueAccessor, readAccessor } from './accessor.js'
|
|
2
|
+
import { BaseController } from './controllers/BaseController.js'
|
|
3
|
+
import type { Accessor, ValueAccessor } from './accessor.js'
|
|
1
4
|
import type { MutationFilters, QueryClient } from '@tanstack/query-core'
|
|
2
5
|
import type { ReactiveControllerHost } from 'lit'
|
|
3
|
-
import {
|
|
4
|
-
createValueAccessor,
|
|
5
|
-
readAccessor,
|
|
6
|
-
type Accessor,
|
|
7
|
-
type ValueAccessor,
|
|
8
|
-
} from './accessor.js'
|
|
9
|
-
import { BaseController } from './controllers/BaseController.js'
|
|
10
6
|
|
|
11
7
|
/**
|
|
12
8
|
* Accessor returned by `useIsMutating`.
|
|
@@ -35,7 +31,7 @@ class IsMutatingController extends BaseController<number> {
|
|
|
35
31
|
this.result = this.computeValue()
|
|
36
32
|
}
|
|
37
33
|
|
|
38
|
-
protected onConnected(): void {
|
|
34
|
+
protected override onConnected(): void {
|
|
39
35
|
if (!this.syncClient()) {
|
|
40
36
|
this.setResult(0)
|
|
41
37
|
return
|
|
@@ -45,13 +41,13 @@ class IsMutatingController extends BaseController<number> {
|
|
|
45
41
|
this.setResult(this.computeValue())
|
|
46
42
|
}
|
|
47
43
|
|
|
48
|
-
protected onDisconnected(): void {
|
|
44
|
+
protected override onDisconnected(): void {
|
|
49
45
|
this.unsubscribe?.()
|
|
50
46
|
this.unsubscribe = undefined
|
|
51
47
|
this.syncClient()
|
|
52
48
|
}
|
|
53
49
|
|
|
54
|
-
protected onHostUpdate(): void {
|
|
50
|
+
protected override onHostUpdate(): void {
|
|
55
51
|
if (typeof this.filters !== 'function') {
|
|
56
52
|
return
|
|
57
53
|
}
|
|
@@ -59,7 +55,7 @@ class IsMutatingController extends BaseController<number> {
|
|
|
59
55
|
this.setResult(this.syncClient() ? this.computeValue() : 0)
|
|
60
56
|
}
|
|
61
57
|
|
|
62
|
-
protected onQueryClientChanged(): void {
|
|
58
|
+
protected override onQueryClientChanged(): void {
|
|
63
59
|
if (!this.syncClient()) {
|
|
64
60
|
this.setResult(0)
|
|
65
61
|
return
|
|
@@ -120,14 +116,12 @@ class IsMutatingController extends BaseController<number> {
|
|
|
120
116
|
* When `filters` is a function, it is re-read during host updates so the count
|
|
121
117
|
* can follow reactive host state. If `queryClient` is omitted, the controller
|
|
122
118
|
* resolves the client from the nearest connected `QueryClientProvider`.
|
|
123
|
-
*
|
|
124
119
|
* @param host - The Lit reactive controller host that owns the cache
|
|
125
120
|
* subscription.
|
|
126
121
|
* @param filters - Mutation filters, or a getter that returns mutation filters.
|
|
127
122
|
* @param queryClient - Optional explicit query client. Provide this for
|
|
128
123
|
* controllers that should not resolve a client from Lit context.
|
|
129
124
|
* @returns An accessor for the current number of matching pending mutations.
|
|
130
|
-
*
|
|
131
125
|
* @example
|
|
132
126
|
* ```ts
|
|
133
127
|
* import { LitElement, html } from 'lit'
|
package/src/useMutationState.ts
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { replaceEqualDeep } from '@tanstack/query-core'
|
|
2
|
+
import { createValueAccessor, readAccessor } from './accessor.js'
|
|
3
|
+
import { BaseController } from './controllers/BaseController.js'
|
|
4
|
+
import type { Accessor, ValueAccessor } from './accessor.js'
|
|
5
|
+
import type {
|
|
6
|
+
Mutation,
|
|
7
|
+
MutationFilters,
|
|
8
|
+
MutationState,
|
|
9
|
+
QueryClient,
|
|
7
10
|
} from '@tanstack/query-core'
|
|
8
11
|
import type { ReactiveControllerHost } from 'lit'
|
|
9
|
-
import {
|
|
10
|
-
createValueAccessor,
|
|
11
|
-
readAccessor,
|
|
12
|
-
type Accessor,
|
|
13
|
-
type ValueAccessor,
|
|
14
|
-
} from './accessor.js'
|
|
15
|
-
import { BaseController } from './controllers/BaseController.js'
|
|
16
12
|
|
|
17
13
|
/**
|
|
18
14
|
* Options accepted by `useMutationState`.
|
|
@@ -30,12 +26,12 @@ export type MutationStateOptions<TResult> = {
|
|
|
30
26
|
* Call the accessor or read its `current` property to get the selected state for
|
|
31
27
|
* matching mutations.
|
|
32
28
|
*/
|
|
33
|
-
export type MutationStateAccessor<TResult> = ValueAccessor<TResult
|
|
29
|
+
export type MutationStateAccessor<TResult> = ValueAccessor<Array<TResult>> & {
|
|
34
30
|
/** Removes the controller from its Lit host and unsubscribes observers. */
|
|
35
31
|
destroy: () => void
|
|
36
32
|
}
|
|
37
33
|
|
|
38
|
-
class MutationStateController<TResult> extends BaseController<TResult
|
|
34
|
+
class MutationStateController<TResult> extends BaseController<Array<TResult>> {
|
|
39
35
|
private queryClient: QueryClient | undefined
|
|
40
36
|
private unsubscribe: (() => void) | undefined
|
|
41
37
|
|
|
@@ -54,7 +50,7 @@ class MutationStateController<TResult> extends BaseController<TResult[]> {
|
|
|
54
50
|
this.result = this.computeState()
|
|
55
51
|
}
|
|
56
52
|
|
|
57
|
-
protected onConnected(): void {
|
|
53
|
+
protected override onConnected(): void {
|
|
58
54
|
if (!this.syncClient()) {
|
|
59
55
|
this.setMutationState([])
|
|
60
56
|
return
|
|
@@ -64,13 +60,13 @@ class MutationStateController<TResult> extends BaseController<TResult[]> {
|
|
|
64
60
|
this.setMutationState(this.computeState())
|
|
65
61
|
}
|
|
66
62
|
|
|
67
|
-
protected onDisconnected(): void {
|
|
63
|
+
protected override onDisconnected(): void {
|
|
68
64
|
this.unsubscribe?.()
|
|
69
65
|
this.unsubscribe = undefined
|
|
70
66
|
this.syncClient()
|
|
71
67
|
}
|
|
72
68
|
|
|
73
|
-
protected onHostUpdate(): void {
|
|
69
|
+
protected override onHostUpdate(): void {
|
|
74
70
|
if (!this.shouldRefreshOnHostUpdate()) {
|
|
75
71
|
return
|
|
76
72
|
}
|
|
@@ -78,7 +74,7 @@ class MutationStateController<TResult> extends BaseController<TResult[]> {
|
|
|
78
74
|
this.setMutationState(this.syncClient() ? this.computeState() : [])
|
|
79
75
|
}
|
|
80
76
|
|
|
81
|
-
protected onQueryClientChanged(): void {
|
|
77
|
+
protected override onQueryClientChanged(): void {
|
|
82
78
|
if (!this.syncClient()) {
|
|
83
79
|
this.setMutationState([])
|
|
84
80
|
return
|
|
@@ -123,7 +119,7 @@ class MutationStateController<TResult> extends BaseController<TResult[]> {
|
|
|
123
119
|
})
|
|
124
120
|
}
|
|
125
121
|
|
|
126
|
-
private setMutationState(next: TResult
|
|
122
|
+
private setMutationState(next: Array<TResult>): void {
|
|
127
123
|
this.setResult(replaceEqualDeep(this.result, next))
|
|
128
124
|
}
|
|
129
125
|
|
|
@@ -134,7 +130,7 @@ class MutationStateController<TResult> extends BaseController<TResult[]> {
|
|
|
134
130
|
)
|
|
135
131
|
}
|
|
136
132
|
|
|
137
|
-
private computeState(): TResult
|
|
133
|
+
private computeState(): Array<TResult> {
|
|
138
134
|
if (!this.queryClient) {
|
|
139
135
|
return []
|
|
140
136
|
}
|
|
@@ -164,14 +160,12 @@ class MutationStateController<TResult> extends BaseController<TResult[]> {
|
|
|
164
160
|
* the selection can follow reactive host state. If `queryClient` is omitted,
|
|
165
161
|
* the controller resolves the client from the nearest connected
|
|
166
162
|
* `QueryClientProvider`.
|
|
167
|
-
*
|
|
168
163
|
* @param host - The Lit reactive controller host that owns the mutation cache
|
|
169
164
|
* subscription.
|
|
170
165
|
* @param options - Mutation state filters and optional selector.
|
|
171
166
|
* @param queryClient - Optional explicit query client. Provide this for
|
|
172
167
|
* controllers that should not resolve a client from Lit context.
|
|
173
168
|
* @returns An accessor for the selected mutation state array.
|
|
174
|
-
*
|
|
175
169
|
* @example
|
|
176
170
|
* ```ts
|
|
177
171
|
* import { LitElement, html } from 'lit'
|