@tanstack/lit-query 0.0.1 → 0.2.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/LICENSE +21 -0
- package/README.md +125 -43
- package/dist/QueryClientProvider.d.ts +85 -0
- package/dist/QueryClientProvider.d.ts.map +1 -0
- package/dist/QueryClientProvider.js +137 -0
- package/dist/QueryClientProvider.js.map +1 -0
- package/dist/accessor.d.ts +32 -0
- package/dist/accessor.d.ts.map +1 -0
- package/dist/accessor.js +12 -0
- package/dist/accessor.js.map +1 -0
- package/dist/context.d.ts +59 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +100 -0
- package/dist/context.js.map +1 -0
- package/dist/controllers/BaseController.d.ts +38 -0
- package/dist/controllers/BaseController.d.ts.map +1 -0
- package/dist/controllers/BaseController.js +204 -0
- package/dist/controllers/BaseController.js.map +1 -0
- package/dist/createInfiniteQueryController.d.ts +76 -0
- package/dist/createInfiniteQueryController.d.ts.map +1 -0
- package/dist/createInfiniteQueryController.js +221 -0
- package/dist/createInfiniteQueryController.js.map +1 -0
- package/dist/createMutationController.d.ts +79 -0
- package/dist/createMutationController.d.ts.map +1 -0
- package/dist/createMutationController.js +193 -0
- package/dist/createMutationController.js.map +1 -0
- package/dist/createQueriesController.d.ts +145 -0
- package/dist/createQueriesController.d.ts.map +1 -0
- package/dist/createQueriesController.js +302 -0
- package/dist/createQueriesController.js.map +1 -0
- package/dist/createQueryController.d.ts +67 -0
- package/dist/createQueryController.d.ts.map +1 -0
- package/dist/createQueryController.js +211 -0
- package/dist/createQueryController.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/infiniteQueryOptions.d.ts +21 -0
- package/dist/infiniteQueryOptions.d.ts.map +1 -0
- package/dist/infiniteQueryOptions.js +22 -0
- package/dist/infiniteQueryOptions.js.map +1 -0
- package/dist/mutationOptions.d.ts +19 -0
- package/dist/mutationOptions.d.ts.map +1 -0
- package/dist/mutationOptions.js +20 -0
- package/dist/mutationOptions.js.map +1 -0
- package/dist/queryOptions.d.ts +62 -0
- package/dist/queryOptions.d.ts.map +1 -0
- package/dist/queryOptions.js +4 -0
- package/dist/queryOptions.js.map +1 -0
- package/dist/types.d.ts +31 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/useIsFetching.d.ts +45 -0
- package/dist/useIsFetching.d.ts.map +1 -0
- package/dist/useIsFetching.js +116 -0
- package/dist/useIsFetching.js.map +1 -0
- package/dist/useIsMutating.d.ts +45 -0
- package/dist/useIsMutating.d.ts.map +1 -0
- package/dist/useIsMutating.js +116 -0
- package/dist/useIsMutating.js.map +1 -0
- package/dist/useMutationState.d.ts +57 -0
- package/dist/useMutationState.d.ts.map +1 -0
- package/dist/useMutationState.js +132 -0
- package/dist/useMutationState.js.map +1 -0
- package/dist-cjs/QueryClientProvider.d.cts +84 -0
- package/dist-cjs/QueryClientProvider.js +141 -0
- package/dist-cjs/QueryClientProvider.js.map +1 -0
- package/dist-cjs/accessor.d.cts +31 -0
- package/dist-cjs/accessor.js +16 -0
- package/dist-cjs/accessor.js.map +1 -0
- package/dist-cjs/context.d.cts +58 -0
- package/dist-cjs/context.js +109 -0
- package/dist-cjs/context.js.map +1 -0
- package/dist-cjs/controllers/BaseController.d.cts +37 -0
- package/dist-cjs/controllers/BaseController.js +208 -0
- package/dist-cjs/controllers/BaseController.js.map +1 -0
- package/dist-cjs/createInfiniteQueryController.d.cts +75 -0
- package/dist-cjs/createInfiniteQueryController.js +224 -0
- package/dist-cjs/createInfiniteQueryController.js.map +1 -0
- package/dist-cjs/createMutationController.d.cts +78 -0
- package/dist-cjs/createMutationController.js +196 -0
- package/dist-cjs/createMutationController.js.map +1 -0
- package/dist-cjs/createQueriesController.d.cts +144 -0
- package/dist-cjs/createQueriesController.js +305 -0
- package/dist-cjs/createQueriesController.js.map +1 -0
- package/dist-cjs/createQueryController.d.cts +66 -0
- package/dist-cjs/createQueryController.js +214 -0
- package/dist-cjs/createQueryController.js.map +1 -0
- package/dist-cjs/index.d.cts +23 -0
- package/dist-cjs/index.js +49 -0
- package/dist-cjs/index.js.map +1 -0
- package/dist-cjs/infiniteQueryOptions.d.cts +20 -0
- package/dist-cjs/infiniteQueryOptions.js +25 -0
- package/dist-cjs/infiniteQueryOptions.js.map +1 -0
- package/dist-cjs/mutationOptions.d.cts +18 -0
- package/dist-cjs/mutationOptions.js +23 -0
- package/dist-cjs/mutationOptions.js.map +1 -0
- package/dist-cjs/package.json +3 -0
- package/dist-cjs/queryOptions.d.cts +61 -0
- package/dist-cjs/queryOptions.js +7 -0
- package/dist-cjs/queryOptions.js.map +1 -0
- package/dist-cjs/types.d.cts +30 -0
- package/dist-cjs/types.js +3 -0
- package/dist-cjs/types.js.map +1 -0
- package/dist-cjs/useIsFetching.d.cts +44 -0
- package/dist-cjs/useIsFetching.js +119 -0
- package/dist-cjs/useIsFetching.js.map +1 -0
- package/dist-cjs/useIsMutating.d.cts +44 -0
- package/dist-cjs/useIsMutating.js +119 -0
- package/dist-cjs/useIsMutating.js.map +1 -0
- package/dist-cjs/useMutationState.d.cts +56 -0
- package/dist-cjs/useMutationState.js +135 -0
- package/dist-cjs/useMutationState.js.map +1 -0
- package/package.json +73 -8
- package/src/QueryClientProvider.ts +174 -0
- package/src/accessor.ts +43 -0
- package/src/context.ts +120 -0
- package/src/controllers/BaseController.ts +284 -0
- package/src/createInfiniteQueryController.ts +394 -0
- package/src/createMutationController.ts +361 -0
- package/src/createQueriesController.ts +633 -0
- package/src/createQueryController.ts +342 -0
- package/src/index.ts +72 -0
- package/src/infiniteQueryOptions.ts +48 -0
- package/src/mutationOptions.ts +31 -0
- package/src/queryOptions.ts +143 -0
- package/src/types.ts +77 -0
- package/src/useIsFetching.ts +159 -0
- package/src/useIsMutating.ts +159 -0
- package/src/useMutationState.ts +201 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { type DefaultError, type DefinedQueryObserverResult, type OmitKeyof, type QueryFunction, type QueryKey, type QueryObserverOptions, type QueryObserverResult, type ThrowOnError } from '@tanstack/query-core';
|
|
2
|
+
import type { QueryClient } from '@tanstack/query-core';
|
|
3
|
+
import type { ReactiveControllerHost } from 'lit' with { "resolution-mode": "import" };
|
|
4
|
+
import { type Accessor, type ValueAccessor } from './accessor.cjs';
|
|
5
|
+
/**
|
|
6
|
+
* Options for one query inside `createQueriesController`.
|
|
7
|
+
*
|
|
8
|
+
* This mirrors `QueryObserverOptions` and is used by the tuple inference that
|
|
9
|
+
* maps each input query to its corresponding result.
|
|
10
|
+
*/
|
|
11
|
+
export type CreateQueriesInput<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>;
|
|
12
|
+
type CreateQueriesInputForController<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = OmitKeyof<CreateQueriesInput<TQueryFnData, TError, TData, TQueryKey>, never>;
|
|
13
|
+
type MAXIMUM_DEPTH = 20;
|
|
14
|
+
type SkipTokenForCreateQueries = symbol;
|
|
15
|
+
type GetCreateQueriesInput<T> = T extends {
|
|
16
|
+
queryFnData: infer TQueryFnData;
|
|
17
|
+
error?: infer TError;
|
|
18
|
+
data: infer TData;
|
|
19
|
+
} ? CreateQueriesInputForController<TQueryFnData, TError, TData> : T extends {
|
|
20
|
+
queryFnData: infer TQueryFnData;
|
|
21
|
+
error?: infer TError;
|
|
22
|
+
} ? CreateQueriesInputForController<TQueryFnData, TError> : T extends {
|
|
23
|
+
data: infer TData;
|
|
24
|
+
error?: infer TError;
|
|
25
|
+
} ? CreateQueriesInputForController<unknown, TError, TData> : T extends [infer TQueryFnData, infer TError, infer TData] ? CreateQueriesInputForController<TQueryFnData, TError, TData> : T extends [infer TQueryFnData, infer TError] ? CreateQueriesInputForController<TQueryFnData, TError> : T extends [infer TQueryFnData] ? CreateQueriesInputForController<TQueryFnData> : T extends {
|
|
26
|
+
queryFn?: QueryFunction<infer TQueryFnData, infer TQueryKey> | SkipTokenForCreateQueries;
|
|
27
|
+
select?: (data: any) => infer TData;
|
|
28
|
+
throwOnError?: ThrowOnError<any, infer TError, any, any>;
|
|
29
|
+
} ? CreateQueriesInputForController<TQueryFnData, unknown extends TError ? DefaultError : TError, unknown extends TData ? TQueryFnData : TData, TQueryKey> : CreateQueriesInputForController;
|
|
30
|
+
type GetDefinedOrUndefinedCreateQueriesResult<T, TData, TError = unknown> = T extends {
|
|
31
|
+
initialData?: infer TInitialData;
|
|
32
|
+
} ? unknown extends TInitialData ? QueryObserverResult<TData, TError> : TInitialData extends TData ? DefinedQueryObserverResult<TData, TError> : TInitialData extends () => infer TInitialDataResult ? unknown extends TInitialDataResult ? QueryObserverResult<TData, TError> : TInitialDataResult extends TData ? DefinedQueryObserverResult<TData, TError> : QueryObserverResult<TData, TError> : QueryObserverResult<TData, TError> : QueryObserverResult<TData, TError>;
|
|
33
|
+
type GetCreateQueriesResult<T> = T extends {
|
|
34
|
+
queryFnData: any;
|
|
35
|
+
error?: infer TError;
|
|
36
|
+
data: infer TData;
|
|
37
|
+
} ? GetDefinedOrUndefinedCreateQueriesResult<T, TData, TError> : T extends {
|
|
38
|
+
queryFnData: infer TQueryFnData;
|
|
39
|
+
error?: infer TError;
|
|
40
|
+
} ? GetDefinedOrUndefinedCreateQueriesResult<T, TQueryFnData, TError> : T extends {
|
|
41
|
+
data: infer TData;
|
|
42
|
+
error?: infer TError;
|
|
43
|
+
} ? GetDefinedOrUndefinedCreateQueriesResult<T, TData, TError> : T extends [any, infer TError, infer TData] ? GetDefinedOrUndefinedCreateQueriesResult<T, TData, TError> : T extends [infer TQueryFnData, infer TError] ? GetDefinedOrUndefinedCreateQueriesResult<T, TQueryFnData, TError> : T extends [infer TQueryFnData] ? GetDefinedOrUndefinedCreateQueriesResult<T, TQueryFnData> : T extends {
|
|
44
|
+
queryFn?: QueryFunction<infer TQueryFnData, any> | SkipTokenForCreateQueries;
|
|
45
|
+
select?: (data: any) => infer TData;
|
|
46
|
+
throwOnError?: ThrowOnError<any, infer TError, any, any>;
|
|
47
|
+
} ? GetDefinedOrUndefinedCreateQueriesResult<T, unknown extends TData ? TQueryFnData : TData, unknown extends TError ? DefaultError : TError> : QueryObserverResult;
|
|
48
|
+
export type CreateQueriesOptions<T extends Array<any>, TResults extends Array<any> = [], TDepth extends ReadonlyArray<number> = []> = TDepth['length'] extends MAXIMUM_DEPTH ? Array<CreateQueriesInputForController> : T extends [] ? [] : T extends [infer Head] ? [...TResults, GetCreateQueriesInput<Head>] : T extends [infer Head, ...infer Tails] ? CreateQueriesOptions<[
|
|
49
|
+
...Tails
|
|
50
|
+
], [
|
|
51
|
+
...TResults,
|
|
52
|
+
GetCreateQueriesInput<Head>
|
|
53
|
+
], [
|
|
54
|
+
...TDepth,
|
|
55
|
+
1
|
|
56
|
+
]> : ReadonlyArray<unknown> extends T ? T : T extends Array<CreateQueriesInputForController<infer TQueryFnData, infer TError, infer TData, infer TQueryKey>> ? Array<CreateQueriesInputForController<TQueryFnData, TError, TData, TQueryKey>> : Array<CreateQueriesInputForController>;
|
|
57
|
+
/**
|
|
58
|
+
* Tuple of query results inferred from the query inputs passed to
|
|
59
|
+
* `createQueriesController`.
|
|
60
|
+
*/
|
|
61
|
+
export type CreateQueriesResults<T extends Array<any>, TResults extends Array<any> = [], TDepth extends ReadonlyArray<number> = []> = TDepth['length'] extends MAXIMUM_DEPTH ? Array<QueryObserverResult> : T extends [] ? [] : T extends [infer Head] ? [...TResults, GetCreateQueriesResult<Head>] : T extends [infer Head, ...infer Tails] ? CreateQueriesResults<[
|
|
62
|
+
...Tails
|
|
63
|
+
], [
|
|
64
|
+
...TResults,
|
|
65
|
+
GetCreateQueriesResult<Head>
|
|
66
|
+
], [
|
|
67
|
+
...TDepth,
|
|
68
|
+
1
|
|
69
|
+
]> : {
|
|
70
|
+
[K in keyof T]: GetCreateQueriesResult<T[K]>;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Options accepted by `createQueriesController`.
|
|
74
|
+
*
|
|
75
|
+
* `queries` can be a static list or a getter that returns the current list.
|
|
76
|
+
* `combine` can reshape the array of query results into a single value for the
|
|
77
|
+
* returned accessor.
|
|
78
|
+
*/
|
|
79
|
+
export type CreateQueriesControllerOptions<TQueryOptions extends Array<any> = Array<any>, TCombinedResult = CreateQueriesResults<TQueryOptions>> = {
|
|
80
|
+
/** Query options to observe, or a getter that returns the current options. */
|
|
81
|
+
queries: Accessor<readonly [...CreateQueriesOptions<TQueryOptions>] | readonly [
|
|
82
|
+
...{
|
|
83
|
+
[K in keyof TQueryOptions]: GetCreateQueriesInput<TQueryOptions[K]>;
|
|
84
|
+
}
|
|
85
|
+
]>;
|
|
86
|
+
/** Optional function that combines the query result array into one value. */
|
|
87
|
+
combine?: (result: CreateQueriesResults<TQueryOptions>) => TCombinedResult;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Accessor returned by `createQueriesController`.
|
|
91
|
+
*
|
|
92
|
+
* Call the accessor or read its `current` property to get the latest combined
|
|
93
|
+
* value.
|
|
94
|
+
*/
|
|
95
|
+
export type QueriesResultAccessor<TCombinedResult> = ValueAccessor<TCombinedResult> & {
|
|
96
|
+
/** Removes the controller from its Lit host and unsubscribes observers. */
|
|
97
|
+
destroy: () => void;
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Creates a Lit reactive controller that subscribes the host to multiple
|
|
101
|
+
* queries.
|
|
102
|
+
*
|
|
103
|
+
* The returned accessor is callable and also exposes `current` and `destroy`.
|
|
104
|
+
* When `options` or `options.queries` is a function, it is re-read during host
|
|
105
|
+
* updates so the query list can follow reactive host state.
|
|
106
|
+
*
|
|
107
|
+
* If `queryClient` is omitted, the controller resolves the client from the
|
|
108
|
+
* nearest connected `QueryClientProvider`.
|
|
109
|
+
*
|
|
110
|
+
* @param host - The Lit reactive controller host that owns the queries
|
|
111
|
+
* subscription.
|
|
112
|
+
* @param options - Queries controller options, or a getter that returns options.
|
|
113
|
+
* @param queryClient - Optional explicit query client. Provide this for
|
|
114
|
+
* controllers that should not resolve a client from Lit context.
|
|
115
|
+
* @returns An accessor for the latest query results, or the value returned by
|
|
116
|
+
* `combine`.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* import { LitElement, html } from 'lit'
|
|
121
|
+
* import { createQueriesController } from '@tanstack/lit-query'
|
|
122
|
+
*
|
|
123
|
+
* class DashboardView extends LitElement {
|
|
124
|
+
* private readonly dashboard = createQueriesController(this, {
|
|
125
|
+
* queries: [
|
|
126
|
+
* { queryKey: ['stats'], queryFn: fetchStats },
|
|
127
|
+
* { queryKey: ['projects'], queryFn: fetchProjects },
|
|
128
|
+
* ],
|
|
129
|
+
* combine: ([stats, projects]) => ({
|
|
130
|
+
* stats: stats.data,
|
|
131
|
+
* projects: projects.data ?? [],
|
|
132
|
+
* isPending: stats.isPending || projects.isPending,
|
|
133
|
+
* }),
|
|
134
|
+
* })
|
|
135
|
+
*
|
|
136
|
+
* render() {
|
|
137
|
+
* const dashboard = this.dashboard()
|
|
138
|
+
* return html`<p>Projects: ${dashboard.projects.length}</p>`
|
|
139
|
+
* }
|
|
140
|
+
* }
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
export declare function createQueriesController<TQueryOptions extends Array<any>, TCombinedResult = CreateQueriesResults<TQueryOptions>>(host: ReactiveControllerHost, options: Accessor<CreateQueriesControllerOptions<TQueryOptions, TCombinedResult>>, queryClient?: QueryClient): QueriesResultAccessor<TCombinedResult>;
|
|
144
|
+
export {};
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createQueriesController = createQueriesController;
|
|
4
|
+
const query_core_1 = require("@tanstack/query-core");
|
|
5
|
+
const accessor_js_1 = require("./accessor.js");
|
|
6
|
+
const context_js_1 = require("./context.js");
|
|
7
|
+
const BaseController_js_1 = require("./controllers/BaseController.js");
|
|
8
|
+
function createPendingQueryObserverResult() {
|
|
9
|
+
return {
|
|
10
|
+
data: undefined,
|
|
11
|
+
dataUpdatedAt: 0,
|
|
12
|
+
error: null,
|
|
13
|
+
errorUpdatedAt: 0,
|
|
14
|
+
failureCount: 0,
|
|
15
|
+
failureReason: null,
|
|
16
|
+
errorUpdateCount: 0,
|
|
17
|
+
isError: false,
|
|
18
|
+
isFetched: false,
|
|
19
|
+
isFetchedAfterMount: false,
|
|
20
|
+
isFetching: false,
|
|
21
|
+
isInitialLoading: false,
|
|
22
|
+
isLoading: false,
|
|
23
|
+
isLoadingError: false,
|
|
24
|
+
isPaused: false,
|
|
25
|
+
isPending: true,
|
|
26
|
+
isPlaceholderData: false,
|
|
27
|
+
isRefetchError: false,
|
|
28
|
+
isRefetching: false,
|
|
29
|
+
isStale: true,
|
|
30
|
+
isEnabled: true,
|
|
31
|
+
isSuccess: false,
|
|
32
|
+
fetchStatus: 'idle',
|
|
33
|
+
status: 'pending',
|
|
34
|
+
refetch: (() => Promise.reject((0, context_js_1.createMissingQueryClientError)())),
|
|
35
|
+
promise: Promise.resolve(undefined),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function createPlaceholderQueryObserverResult(query) {
|
|
39
|
+
const initialData = typeof query.initialData === 'function'
|
|
40
|
+
? query.initialData()
|
|
41
|
+
: query.initialData;
|
|
42
|
+
if (initialData === undefined) {
|
|
43
|
+
return createPendingQueryObserverResult();
|
|
44
|
+
}
|
|
45
|
+
const data = query.select ? query.select(initialData) : initialData;
|
|
46
|
+
const initialDataUpdatedAt = typeof query.initialDataUpdatedAt === 'function'
|
|
47
|
+
? query.initialDataUpdatedAt()
|
|
48
|
+
: query.initialDataUpdatedAt;
|
|
49
|
+
return {
|
|
50
|
+
...createPendingQueryObserverResult(),
|
|
51
|
+
data,
|
|
52
|
+
dataUpdatedAt: initialDataUpdatedAt ?? Date.now(),
|
|
53
|
+
isPending: false,
|
|
54
|
+
isInitialLoading: false,
|
|
55
|
+
isLoading: false,
|
|
56
|
+
isSuccess: true,
|
|
57
|
+
status: 'success',
|
|
58
|
+
promise: Promise.resolve(data),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function resolveQueriesOptions(optionsAccessor, client) {
|
|
62
|
+
const resolvedOptions = (0, accessor_js_1.readAccessor)(optionsAccessor);
|
|
63
|
+
const resolvedQueries = (0, accessor_js_1.readAccessor)(resolvedOptions.queries);
|
|
64
|
+
const combine = resolvedOptions.combine;
|
|
65
|
+
return {
|
|
66
|
+
queries: resolvedQueries.map((query) => {
|
|
67
|
+
const defaulted = client.defaultQueryOptions(query);
|
|
68
|
+
defaulted._optimisticResults =
|
|
69
|
+
'optimistic';
|
|
70
|
+
return defaulted;
|
|
71
|
+
}),
|
|
72
|
+
combine,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
class QueriesController extends BaseController_js_1.BaseController {
|
|
76
|
+
options;
|
|
77
|
+
observer;
|
|
78
|
+
unsubscribe;
|
|
79
|
+
queryClient;
|
|
80
|
+
explicitInitializationError;
|
|
81
|
+
placeholderInitialized = false;
|
|
82
|
+
placeholderRetryableFailure = true;
|
|
83
|
+
constructor(host, options, queryClient) {
|
|
84
|
+
super(host, [], queryClient);
|
|
85
|
+
this.options = options;
|
|
86
|
+
queueMicrotask(() => {
|
|
87
|
+
this.placeholderRetryableFailure = false;
|
|
88
|
+
});
|
|
89
|
+
if (!queryClient) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (this.shouldRefreshOnHostUpdate()) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
this.tryInitializeExplicitClient(queryClient);
|
|
96
|
+
}
|
|
97
|
+
onConnected() {
|
|
98
|
+
if (!this.syncClient()) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
this.refreshOptions();
|
|
102
|
+
this.subscribe();
|
|
103
|
+
}
|
|
104
|
+
onDisconnected() {
|
|
105
|
+
this.unsubscribeObserver();
|
|
106
|
+
this.syncClient();
|
|
107
|
+
}
|
|
108
|
+
onHostUpdate() {
|
|
109
|
+
if (!this.shouldRefreshOnHostUpdate()) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (!this.refreshOptions()) {
|
|
113
|
+
this.setPlaceholderResult();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
onQueryClientChanged() {
|
|
117
|
+
if (!this.syncClient() || !this.connectedState) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
this.refreshOptions();
|
|
121
|
+
this.subscribe();
|
|
122
|
+
}
|
|
123
|
+
subscribe() {
|
|
124
|
+
if (!this.observer) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (this.unsubscribe) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
this.unsubscribe = this.observer.subscribe((next) => {
|
|
131
|
+
const { combine } = this.readResolvedOptions();
|
|
132
|
+
this.setResult(this.computeResult(next, combine));
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
tryInitializeExplicitClient(queryClient) {
|
|
136
|
+
try {
|
|
137
|
+
const { queries, combine } = resolveQueriesOptions(this.options, queryClient);
|
|
138
|
+
const observer = new query_core_1.QueriesObserver(queryClient, queries, {
|
|
139
|
+
combine,
|
|
140
|
+
});
|
|
141
|
+
this.queryClient = queryClient;
|
|
142
|
+
this.observer = observer;
|
|
143
|
+
this.result = this.computeResult(observer.getCurrentResult(), combine);
|
|
144
|
+
this.explicitInitializationError = undefined;
|
|
145
|
+
this.placeholderInitialized = true;
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
// Retry after construction completes so late host fields used by
|
|
150
|
+
// static queries/combine callbacks can finish initializing first.
|
|
151
|
+
this.explicitInitializationError = error;
|
|
152
|
+
this.queryClient = undefined;
|
|
153
|
+
this.observer = undefined;
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
retryExplicitInitializationIfNeeded() {
|
|
158
|
+
if (!this.explicitInitializationError || this.shouldRefreshOnHostUpdate()) {
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
const explicitClient = this.tryGetQueryClient();
|
|
162
|
+
if (!explicitClient) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
return this.tryInitializeExplicitClient(explicitClient);
|
|
166
|
+
}
|
|
167
|
+
unsubscribeObserver() {
|
|
168
|
+
this.unsubscribe?.();
|
|
169
|
+
this.unsubscribe = undefined;
|
|
170
|
+
}
|
|
171
|
+
syncClient() {
|
|
172
|
+
const nextClient = this.tryGetQueryClient();
|
|
173
|
+
if (!nextClient) {
|
|
174
|
+
this.unsubscribeObserver();
|
|
175
|
+
this.queryClient = undefined;
|
|
176
|
+
this.observer = undefined;
|
|
177
|
+
this.setPlaceholderResult();
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
if (nextClient === this.queryClient) {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
this.unsubscribeObserver();
|
|
184
|
+
this.queryClient = nextClient;
|
|
185
|
+
const { queries, combine } = this.readResolvedOptions();
|
|
186
|
+
this.observer = new query_core_1.QueriesObserver(this.queryClient, queries, {
|
|
187
|
+
combine,
|
|
188
|
+
});
|
|
189
|
+
this.setResult(this.computeResult(this.observer.getCurrentResult(), combine));
|
|
190
|
+
this.placeholderInitialized = true;
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
refreshOptions() {
|
|
194
|
+
if (!this.syncClient() || !this.observer) {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
const { queries, combine } = this.readResolvedOptions();
|
|
198
|
+
this.observer.setQueries(queries, {
|
|
199
|
+
combine,
|
|
200
|
+
});
|
|
201
|
+
const [rawResult, getCombinedResult] = this.observer.getOptimisticResult(queries, combine);
|
|
202
|
+
this.setResult(getCombinedResult(rawResult));
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
readResolvedOptions(client = this.queryClient) {
|
|
206
|
+
if (!client) {
|
|
207
|
+
throw (0, context_js_1.createMissingQueryClientError)();
|
|
208
|
+
}
|
|
209
|
+
return resolveQueriesOptions(this.options, client);
|
|
210
|
+
}
|
|
211
|
+
shouldRefreshOnHostUpdate() {
|
|
212
|
+
if (typeof this.options === 'function') {
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
return typeof this.options.queries === 'function';
|
|
216
|
+
}
|
|
217
|
+
computeResult(rawResult, combine) {
|
|
218
|
+
return (combine ? combine(rawResult) : rawResult);
|
|
219
|
+
}
|
|
220
|
+
static createPlaceholderResult(optionsAccessor) {
|
|
221
|
+
const resolvedOptions = (0, accessor_js_1.readAccessor)(optionsAccessor);
|
|
222
|
+
const queries = (0, accessor_js_1.readAccessor)(resolvedOptions.queries);
|
|
223
|
+
const placeholders = queries.map((query) => createPlaceholderQueryObserverResult(query));
|
|
224
|
+
return (resolvedOptions.combine
|
|
225
|
+
? resolvedOptions.combine(placeholders)
|
|
226
|
+
: placeholders);
|
|
227
|
+
}
|
|
228
|
+
readCurrent() {
|
|
229
|
+
if (this.retryExplicitInitializationIfNeeded()) {
|
|
230
|
+
return this.current;
|
|
231
|
+
}
|
|
232
|
+
if (this.explicitInitializationError && !this.placeholderRetryableFailure) {
|
|
233
|
+
throw this.explicitInitializationError;
|
|
234
|
+
}
|
|
235
|
+
if (!this.queryClient && !this.observer && !this.placeholderInitialized) {
|
|
236
|
+
try {
|
|
237
|
+
// Early reads can happen during class-field initialization, before
|
|
238
|
+
// accessors referenced by queries/combine are ready. Retry normally
|
|
239
|
+
// after construction finishes and only surface errors after that point.
|
|
240
|
+
this.setPlaceholderResult();
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
if (!this.placeholderRetryableFailure) {
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return this.current;
|
|
249
|
+
}
|
|
250
|
+
setPlaceholderResult() {
|
|
251
|
+
this.result = QueriesController.createPlaceholderResult(this.options);
|
|
252
|
+
this.placeholderInitialized = true;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Creates a Lit reactive controller that subscribes the host to multiple
|
|
257
|
+
* queries.
|
|
258
|
+
*
|
|
259
|
+
* The returned accessor is callable and also exposes `current` and `destroy`.
|
|
260
|
+
* When `options` or `options.queries` is a function, it is re-read during host
|
|
261
|
+
* updates so the query list can follow reactive host state.
|
|
262
|
+
*
|
|
263
|
+
* If `queryClient` is omitted, the controller resolves the client from the
|
|
264
|
+
* nearest connected `QueryClientProvider`.
|
|
265
|
+
*
|
|
266
|
+
* @param host - The Lit reactive controller host that owns the queries
|
|
267
|
+
* subscription.
|
|
268
|
+
* @param options - Queries controller options, or a getter that returns options.
|
|
269
|
+
* @param queryClient - Optional explicit query client. Provide this for
|
|
270
|
+
* controllers that should not resolve a client from Lit context.
|
|
271
|
+
* @returns An accessor for the latest query results, or the value returned by
|
|
272
|
+
* `combine`.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* ```ts
|
|
276
|
+
* import { LitElement, html } from 'lit'
|
|
277
|
+
* import { createQueriesController } from '@tanstack/lit-query'
|
|
278
|
+
*
|
|
279
|
+
* class DashboardView extends LitElement {
|
|
280
|
+
* private readonly dashboard = createQueriesController(this, {
|
|
281
|
+
* queries: [
|
|
282
|
+
* { queryKey: ['stats'], queryFn: fetchStats },
|
|
283
|
+
* { queryKey: ['projects'], queryFn: fetchProjects },
|
|
284
|
+
* ],
|
|
285
|
+
* combine: ([stats, projects]) => ({
|
|
286
|
+
* stats: stats.data,
|
|
287
|
+
* projects: projects.data ?? [],
|
|
288
|
+
* isPending: stats.isPending || projects.isPending,
|
|
289
|
+
* }),
|
|
290
|
+
* })
|
|
291
|
+
*
|
|
292
|
+
* render() {
|
|
293
|
+
* const dashboard = this.dashboard()
|
|
294
|
+
* return html`<p>Projects: ${dashboard.projects.length}</p>`
|
|
295
|
+
* }
|
|
296
|
+
* }
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
function createQueriesController(host, options, queryClient) {
|
|
300
|
+
const controller = new QueriesController(host, options, queryClient);
|
|
301
|
+
return Object.assign((0, accessor_js_1.createValueAccessor)(() => controller.readCurrent()), {
|
|
302
|
+
destroy: () => controller.destroy(),
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
//# sourceMappingURL=createQueriesController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createQueriesController.js","sourceRoot":"","sources":["../src/createQueriesController.ts"],"names":[],"mappings":";;AAsmBA,0DAkBC;AAxnBD,qDAW6B;AAG7B,+CAKsB;AACtB,6CAA4D;AAC5D,uEAAgE;AAyMhE,SAAS,gCAAgC;IACvC,OAAO;QACL,IAAI,EAAE,SAAS;QACf,aAAa,EAAE,CAAC;QAChB,KAAK,EAAE,IAAI;QACX,cAAc,EAAE,CAAC;QACjB,YAAY,EAAE,CAAC;QACf,aAAa,EAAE,IAAI;QACnB,gBAAgB,EAAE,CAAC;QACnB,OAAO,EAAE,KAAK;QACd,SAAS,EAAE,KAAK;QAChB,mBAAmB,EAAE,KAAK;QAC1B,UAAU,EAAE,KAAK;QACjB,gBAAgB,EAAE,KAAK;QACvB,SAAS,EAAE,KAAK;QAChB,cAAc,EAAE,KAAK;QACrB,QAAQ,EAAE,KAAK;QACf,SAAS,EAAE,IAAI;QACf,iBAAiB,EAAE,KAAK;QACxB,cAAc,EAAE,KAAK;QACrB,YAAY,EAAE,KAAK;QACnB,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;QACf,SAAS,EAAE,KAAK;QAChB,WAAW,EAAE,MAAM;QACnB,MAAM,EAAE,SAAS;QAC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AAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,IAAI,CAAC,2BAA2B,EAAE,CAAC;oBACtC,MAAM,KAAK,CAAA;gBACb,CAAC;YACH,CAAC;QACH,CAAC;QAED,OAAO,IAAI,CAAC,OAAO,CAAA;IACrB,CAAC;IAEO,oBAAoB;QAC1B,IAAI,CAAC,MAAM,GAAG,iBAAiB,CAAC,uBAAuB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QACrE,IAAI,CAAC,sBAAsB,GAAG,IAAI,CAAA;IACpC,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AACH,SAAgB,uBAAuB,CAIrC,IAA4B,EAC5B,OAEC,EACD,WAAyB;IAEzB,MAAM,UAAU,GAAG,IAAI,iBAAiB,CAAC,IAAI,EAAE,OAAO,EAAE,WAAW,CAAC,CAAA;IAEpE,OAAO,MAAM,CAAC,MAAM,CAClB,IAAA,iCAAmB,EAAC,GAAG,EAAE,CAAC,UAAU,CAAC,WAAW,EAAE,CAAC,EACnD;QACE,OAAO,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE;KACpC,CACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { type DefaultError, type QueryKey, type QueryObserverOptions, type QueryObserverResult } from '@tanstack/query-core';
|
|
2
|
+
import type { QueryClient } from '@tanstack/query-core';
|
|
3
|
+
import type { ReactiveControllerHost } from 'lit' with { "resolution-mode": "import" };
|
|
4
|
+
import { type Accessor, type ValueAccessor } from './accessor.cjs';
|
|
5
|
+
/**
|
|
6
|
+
* Options accepted by `createQueryController`.
|
|
7
|
+
*
|
|
8
|
+
* This is the Lit adapter shape for `QueryObserverOptions`. It can be passed
|
|
9
|
+
* directly to `createQueryController`, or wrapped in an `Accessor` when the
|
|
10
|
+
* options depend on Lit host state.
|
|
11
|
+
*/
|
|
12
|
+
export type CreateQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = QueryObserverOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>;
|
|
13
|
+
/**
|
|
14
|
+
* Accessor returned by `createQueryController`.
|
|
15
|
+
*
|
|
16
|
+
* Call the accessor or read its `current` property to get the latest query
|
|
17
|
+
* result. The attached methods delegate to the active query observer.
|
|
18
|
+
*/
|
|
19
|
+
export type QueryResultAccessor<TData, TError> = ValueAccessor<QueryObserverResult<TData, TError>> & {
|
|
20
|
+
/** Refetches the current query. */
|
|
21
|
+
refetch: QueryObserverResult<TData, TError>['refetch'];
|
|
22
|
+
/** Resolves with an optimistic query result, fetching first when needed. */
|
|
23
|
+
suspense: () => Promise<QueryObserverResult<TData, TError>>;
|
|
24
|
+
/** Removes the controller from its Lit host and unsubscribes observers. */
|
|
25
|
+
destroy: () => void;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Creates a Lit reactive controller that subscribes the host to a single query.
|
|
29
|
+
*
|
|
30
|
+
* The returned accessor is callable and also exposes `current`, `refetch`,
|
|
31
|
+
* `suspense`, and `destroy`. When `options` is a function, it is re-read during
|
|
32
|
+
* host updates so query keys and options can follow reactive host state.
|
|
33
|
+
*
|
|
34
|
+
* If `queryClient` is omitted, the controller resolves the client from the
|
|
35
|
+
* nearest connected `QueryClientProvider`.
|
|
36
|
+
*
|
|
37
|
+
* @param host - The Lit reactive controller host that owns the query
|
|
38
|
+
* subscription.
|
|
39
|
+
* @param options - Query observer options, or a getter that returns options.
|
|
40
|
+
* @param queryClient - Optional explicit query client. Provide this for
|
|
41
|
+
* controllers that should not resolve a client from Lit context.
|
|
42
|
+
* @returns An accessor for the latest query result with query helper methods.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { LitElement, html } from 'lit'
|
|
47
|
+
* import { createQueryController } from '@tanstack/lit-query'
|
|
48
|
+
*
|
|
49
|
+
* class TodosView extends LitElement {
|
|
50
|
+
* private readonly todos = createQueryController(this, {
|
|
51
|
+
* queryKey: ['todos'],
|
|
52
|
+
* queryFn: async () => fetch('/api/todos').then((r) => r.json()),
|
|
53
|
+
* })
|
|
54
|
+
*
|
|
55
|
+
* render() {
|
|
56
|
+
* const query = this.todos()
|
|
57
|
+
*
|
|
58
|
+
* if (query.isPending) return html`Loading...`
|
|
59
|
+
* if (query.isError) return html`Error`
|
|
60
|
+
*
|
|
61
|
+
* return html`<ul>${query.data.map((todo) => html`<li>${todo.title}</li>`)}</ul>`
|
|
62
|
+
* }
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare function createQueryController<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(host: ReactiveControllerHost, options: Accessor<CreateQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>>, queryClient?: QueryClient): QueryResultAccessor<TData, TError>;
|