@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,75 @@
|
|
|
1
|
+
import { type DefaultError, type InfiniteData, type InfiniteQueryObserverOptions, type InfiniteQueryObserverResult, type QueryKey } 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 `createInfiniteQueryController`.
|
|
7
|
+
*
|
|
8
|
+
* This is the Lit adapter shape for `InfiniteQueryObserverOptions`. Pass it
|
|
9
|
+
* directly or through an `Accessor` when the options depend on Lit host state.
|
|
10
|
+
*/
|
|
11
|
+
export type CreateInfiniteQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> = InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>;
|
|
12
|
+
/**
|
|
13
|
+
* Accessor returned by `createInfiniteQueryController`.
|
|
14
|
+
*
|
|
15
|
+
* Call the accessor or read its `current` property to get the latest infinite
|
|
16
|
+
* query result. The attached methods delegate to the active infinite query
|
|
17
|
+
* observer.
|
|
18
|
+
*/
|
|
19
|
+
export type InfiniteQueryResultAccessor<TData, TError> = ValueAccessor<InfiniteQueryObserverResult<TData, TError>> & {
|
|
20
|
+
/** Refetches the current infinite query. */
|
|
21
|
+
refetch: InfiniteQueryObserverResult<TData, TError>['refetch'];
|
|
22
|
+
/** Fetches the next page for the current infinite query. */
|
|
23
|
+
fetchNextPage: InfiniteQueryObserverResult<TData, TError>['fetchNextPage'];
|
|
24
|
+
/** Fetches the previous page for the current infinite query. */
|
|
25
|
+
fetchPreviousPage: InfiniteQueryObserverResult<TData, TError>['fetchPreviousPage'];
|
|
26
|
+
/** Removes the controller from its Lit host and unsubscribes observers. */
|
|
27
|
+
destroy: () => void;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Creates a Lit reactive controller that subscribes the host to an infinite
|
|
31
|
+
* query.
|
|
32
|
+
*
|
|
33
|
+
* The returned accessor is callable and also exposes `current`, `refetch`,
|
|
34
|
+
* `fetchNextPage`, `fetchPreviousPage`, and `destroy`. When `options` is a
|
|
35
|
+
* function, it is re-read during host updates so query keys and options can
|
|
36
|
+
* follow reactive host state.
|
|
37
|
+
*
|
|
38
|
+
* If `queryClient` is omitted, the controller resolves the client from the
|
|
39
|
+
* nearest connected `QueryClientProvider`.
|
|
40
|
+
*
|
|
41
|
+
* @param host - The Lit reactive controller host that owns the infinite query
|
|
42
|
+
* subscription.
|
|
43
|
+
* @param options - Infinite query observer options, or a getter that returns
|
|
44
|
+
* options.
|
|
45
|
+
* @param queryClient - Optional explicit query client. Provide this for
|
|
46
|
+
* controllers that should not resolve a client from Lit context.
|
|
47
|
+
* @returns An accessor for the latest infinite query result with page helper
|
|
48
|
+
* methods.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { LitElement, html } from 'lit'
|
|
53
|
+
* import { createInfiniteQueryController } from '@tanstack/lit-query'
|
|
54
|
+
*
|
|
55
|
+
* class ProjectsView extends LitElement {
|
|
56
|
+
* private readonly projects = createInfiniteQueryController(this, {
|
|
57
|
+
* queryKey: ['projects'],
|
|
58
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
59
|
+
* initialPageParam: 0,
|
|
60
|
+
* getNextPageParam: (lastPage) => lastPage.nextCursor,
|
|
61
|
+
* })
|
|
62
|
+
*
|
|
63
|
+
* render() {
|
|
64
|
+
* const query = this.projects()
|
|
65
|
+
*
|
|
66
|
+
* return html`
|
|
67
|
+
* <button ?disabled=${!query.hasNextPage} @click=${() => this.projects.fetchNextPage()}>
|
|
68
|
+
* Load more
|
|
69
|
+
* </button>
|
|
70
|
+
* `
|
|
71
|
+
* }
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function createInfiniteQueryController<TQueryFnData = unknown, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(host: ReactiveControllerHost, options: Accessor<CreateInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>>, queryClient?: QueryClient): InfiniteQueryResultAccessor<TData, TError>;
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createInfiniteQueryController = createInfiniteQueryController;
|
|
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 createPendingInfiniteQueryResult() {
|
|
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
|
+
fetchNextPage: (() => Promise.reject((0, context_js_1.createMissingQueryClientError)())),
|
|
36
|
+
fetchPreviousPage: (() => Promise.reject((0, context_js_1.createMissingQueryClientError)())),
|
|
37
|
+
hasNextPage: false,
|
|
38
|
+
hasPreviousPage: false,
|
|
39
|
+
isFetchNextPageError: false,
|
|
40
|
+
isFetchingNextPage: false,
|
|
41
|
+
isFetchPreviousPageError: false,
|
|
42
|
+
isFetchingPreviousPage: false,
|
|
43
|
+
promise: Promise.resolve(undefined),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
class InfiniteQueryController extends BaseController_js_1.BaseController {
|
|
47
|
+
options;
|
|
48
|
+
observer;
|
|
49
|
+
unsubscribe;
|
|
50
|
+
queryClient;
|
|
51
|
+
constructor(host, options, queryClient) {
|
|
52
|
+
super(host, createPendingInfiniteQueryResult(), queryClient);
|
|
53
|
+
this.options = options;
|
|
54
|
+
if (!queryClient) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (typeof options === 'function') {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const defaulted = this.defaultOptions(queryClient);
|
|
61
|
+
const observer = new query_core_1.InfiniteQueryObserver(queryClient, defaulted);
|
|
62
|
+
this.queryClient = queryClient;
|
|
63
|
+
this.observer = observer;
|
|
64
|
+
this.result = observer.getOptimisticResult(defaulted);
|
|
65
|
+
}
|
|
66
|
+
onConnected() {
|
|
67
|
+
if (!this.syncClient()) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.refreshOptions();
|
|
71
|
+
this.subscribe();
|
|
72
|
+
this.observer?.updateResult();
|
|
73
|
+
if (this.observer) {
|
|
74
|
+
this.setResult(this.observer.getCurrentResult());
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
onDisconnected() {
|
|
78
|
+
this.unsubscribeObserver();
|
|
79
|
+
this.syncClient();
|
|
80
|
+
}
|
|
81
|
+
onHostUpdate() {
|
|
82
|
+
if (typeof this.options !== 'function') {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
this.refreshOptions();
|
|
86
|
+
}
|
|
87
|
+
onQueryClientChanged() {
|
|
88
|
+
if (!this.syncClient() || !this.connectedState) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
this.refreshOptions();
|
|
92
|
+
this.subscribe();
|
|
93
|
+
this.observer?.updateResult();
|
|
94
|
+
if (this.observer) {
|
|
95
|
+
this.setResult(this.observer.getCurrentResult());
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
refetch = (...args) => {
|
|
99
|
+
if (!this.refreshOptions()) {
|
|
100
|
+
return Promise.reject((0, context_js_1.createMissingQueryClientError)());
|
|
101
|
+
}
|
|
102
|
+
return this.result.refetch(...args);
|
|
103
|
+
};
|
|
104
|
+
fetchNextPage = (...args) => {
|
|
105
|
+
if (!this.refreshOptions()) {
|
|
106
|
+
return Promise.reject((0, context_js_1.createMissingQueryClientError)());
|
|
107
|
+
}
|
|
108
|
+
return this.result.fetchNextPage(...args);
|
|
109
|
+
};
|
|
110
|
+
fetchPreviousPage = (...args) => {
|
|
111
|
+
if (!this.refreshOptions()) {
|
|
112
|
+
return Promise.reject((0, context_js_1.createMissingQueryClientError)());
|
|
113
|
+
}
|
|
114
|
+
return this.result.fetchPreviousPage(...args);
|
|
115
|
+
};
|
|
116
|
+
subscribe() {
|
|
117
|
+
if (!this.observer) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (this.unsubscribe) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
this.unsubscribe = this.observer.subscribe((next) => {
|
|
124
|
+
this.setResult(next);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
unsubscribeObserver() {
|
|
128
|
+
this.unsubscribe?.();
|
|
129
|
+
this.unsubscribe = undefined;
|
|
130
|
+
}
|
|
131
|
+
syncClient() {
|
|
132
|
+
const nextClient = this.tryGetQueryClient();
|
|
133
|
+
if (!nextClient) {
|
|
134
|
+
this.unsubscribeObserver();
|
|
135
|
+
this.queryClient = undefined;
|
|
136
|
+
this.observer = undefined;
|
|
137
|
+
this.setResult(createPendingInfiniteQueryResult());
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
if (nextClient === this.queryClient) {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
this.unsubscribeObserver();
|
|
144
|
+
this.queryClient = nextClient;
|
|
145
|
+
const options = this.defaultOptions(this.queryClient);
|
|
146
|
+
this.observer = new query_core_1.InfiniteQueryObserver(this.queryClient, options);
|
|
147
|
+
this.setResult(this.observer.getOptimisticResult(options));
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
refreshOptions() {
|
|
151
|
+
if (!this.syncClient() || !this.observer || !this.queryClient) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
const options = this.defaultOptions(this.queryClient);
|
|
155
|
+
this.observer.setOptions(options);
|
|
156
|
+
this.setResult(this.observer.getOptimisticResult(options));
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
defaultOptions(client = this.queryClient) {
|
|
160
|
+
if (!client) {
|
|
161
|
+
throw (0, context_js_1.createMissingQueryClientError)();
|
|
162
|
+
}
|
|
163
|
+
const defaulted = client.defaultQueryOptions((0, accessor_js_1.readAccessor)(this.options));
|
|
164
|
+
defaulted._optimisticResults =
|
|
165
|
+
'optimistic';
|
|
166
|
+
return defaulted;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Creates a Lit reactive controller that subscribes the host to an infinite
|
|
171
|
+
* query.
|
|
172
|
+
*
|
|
173
|
+
* The returned accessor is callable and also exposes `current`, `refetch`,
|
|
174
|
+
* `fetchNextPage`, `fetchPreviousPage`, and `destroy`. When `options` is a
|
|
175
|
+
* function, it is re-read during host updates so query keys and options can
|
|
176
|
+
* follow reactive host state.
|
|
177
|
+
*
|
|
178
|
+
* If `queryClient` is omitted, the controller resolves the client from the
|
|
179
|
+
* nearest connected `QueryClientProvider`.
|
|
180
|
+
*
|
|
181
|
+
* @param host - The Lit reactive controller host that owns the infinite query
|
|
182
|
+
* subscription.
|
|
183
|
+
* @param options - Infinite query observer options, or a getter that returns
|
|
184
|
+
* options.
|
|
185
|
+
* @param queryClient - Optional explicit query client. Provide this for
|
|
186
|
+
* controllers that should not resolve a client from Lit context.
|
|
187
|
+
* @returns An accessor for the latest infinite query result with page helper
|
|
188
|
+
* methods.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```ts
|
|
192
|
+
* import { LitElement, html } from 'lit'
|
|
193
|
+
* import { createInfiniteQueryController } from '@tanstack/lit-query'
|
|
194
|
+
*
|
|
195
|
+
* class ProjectsView extends LitElement {
|
|
196
|
+
* private readonly projects = createInfiniteQueryController(this, {
|
|
197
|
+
* queryKey: ['projects'],
|
|
198
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
199
|
+
* initialPageParam: 0,
|
|
200
|
+
* getNextPageParam: (lastPage) => lastPage.nextCursor,
|
|
201
|
+
* })
|
|
202
|
+
*
|
|
203
|
+
* render() {
|
|
204
|
+
* const query = this.projects()
|
|
205
|
+
*
|
|
206
|
+
* return html`
|
|
207
|
+
* <button ?disabled=${!query.hasNextPage} @click=${() => this.projects.fetchNextPage()}>
|
|
208
|
+
* Load more
|
|
209
|
+
* </button>
|
|
210
|
+
* `
|
|
211
|
+
* }
|
|
212
|
+
* }
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
function createInfiniteQueryController(host, options, queryClient) {
|
|
216
|
+
const controller = new InfiniteQueryController(host, options, queryClient);
|
|
217
|
+
return Object.assign((0, accessor_js_1.createValueAccessor)(() => controller.current), {
|
|
218
|
+
refetch: controller.refetch,
|
|
219
|
+
fetchNextPage: controller.fetchNextPage,
|
|
220
|
+
fetchPreviousPage: controller.fetchPreviousPage,
|
|
221
|
+
destroy: () => controller.destroy(),
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
//# sourceMappingURL=createInfiniteQueryController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createInfiniteQueryController.js","sourceRoot":"","sources":["../src/createInfiniteQueryController.ts"],"names":[],"mappings":";;AA2WA,sEA8BC;AAzYD,qDAQ6B;AAG7B,+CAKsB;AACtB,6CAA4D;AAC5D,uEAAgE;AA6ChE,SAAS,gCAAgC;IAIvC,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;QACjB,OAAO,EAAE,CAAC,GAAG,EAAE,CACb,OAAO,CAAC,MAAM,CACZ,IAAA,0CAA6B,GAAE,CAChC,CAA0D;QAC7D,aAAa,EAAE,CAAC,GAAG,EAAE,CACnB,OAAO,CAAC,MAAM,CACZ,IAAA,0CAA6B,GAAE,CAChC,CAAgE;QACnE,iBAAiB,EAAE,CAAC,GAAG,EAAE,CACvB,OAAO,CAAC,MAAM,CACZ,IAAA,0CAA6B,GAAE,CAChC,CAAoE;QACvE,WAAW,EAAE,KAAK;QAClB,eAAe,EAAE,KAAK;QACtB,oBAAoB,EAAE,KAAK;QAC3B,kBAAkB,EAAE,KAAK;QACzB,wBAAwB,EAAE,KAAK;QAC/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C,EAC7C;QACE,OAAO,EAAE,UAAU,CAAC,OAAO;QAC3B,aAAa,EAAE,UAAU,CAAC,aAAa;QACvC,iBAAiB,EAAE,UAAU,CAAC,iBAAiB;QAC/C,OAAO,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE;KACpC,CACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { type DefaultError, type MutateOptions, type MutationObserverOptions, type MutationObserverResult } 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 `createMutationController`.
|
|
7
|
+
*
|
|
8
|
+
* This is the Lit adapter shape for `MutationObserverOptions`. Pass it directly
|
|
9
|
+
* or through an `Accessor` when the options depend on Lit host state.
|
|
10
|
+
*/
|
|
11
|
+
export type CreateMutationOptions<TData = unknown, TError = DefaultError, TVariables = void, TOnMutateResult = unknown> = MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>;
|
|
12
|
+
/**
|
|
13
|
+
* Accessor returned by `createMutationController`.
|
|
14
|
+
*
|
|
15
|
+
* Call the accessor or read its `current` property to get the latest mutation
|
|
16
|
+
* result. The attached methods delegate to the active mutation observer.
|
|
17
|
+
*/
|
|
18
|
+
export type MutationResultAccessor<TData, TError, TVariables, TOnMutateResult> = ValueAccessor<MutationObserverResult<TData, TError, TVariables, TOnMutateResult>> & {
|
|
19
|
+
/**
|
|
20
|
+
* Starts the mutation and swallows the returned promise.
|
|
21
|
+
*
|
|
22
|
+
* Throws synchronously if no `QueryClient` can be resolved.
|
|
23
|
+
*/
|
|
24
|
+
mutate: (variables: TVariables, options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>) => void;
|
|
25
|
+
/**
|
|
26
|
+
* Starts the mutation and returns the observer promise.
|
|
27
|
+
*
|
|
28
|
+
* Rejects if no `QueryClient` can be resolved.
|
|
29
|
+
*/
|
|
30
|
+
mutateAsync: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>['mutate'];
|
|
31
|
+
/** Resets the mutation observer to its idle state. */
|
|
32
|
+
reset: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>['reset'];
|
|
33
|
+
/** Removes the controller from its Lit host and unsubscribes observers. */
|
|
34
|
+
destroy: () => void;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Creates a Lit reactive controller that subscribes the host to a mutation.
|
|
38
|
+
*
|
|
39
|
+
* The returned accessor is callable and also exposes `current`, `mutate`,
|
|
40
|
+
* `mutateAsync`, `reset`, and `destroy`. When `options` is a function, it is
|
|
41
|
+
* re-read during host updates so mutation options can follow reactive host
|
|
42
|
+
* state.
|
|
43
|
+
*
|
|
44
|
+
* If `queryClient` is omitted, the controller resolves the client from the
|
|
45
|
+
* nearest connected `QueryClientProvider`.
|
|
46
|
+
*
|
|
47
|
+
* @param host - The Lit reactive controller host that owns the mutation
|
|
48
|
+
* subscription.
|
|
49
|
+
* @param options - Mutation observer options, or a getter that returns options.
|
|
50
|
+
* @param queryClient - Optional explicit query client. Provide this for
|
|
51
|
+
* controllers that should not resolve a client from Lit context.
|
|
52
|
+
* @returns An accessor for the latest mutation result with mutation helper
|
|
53
|
+
* methods.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* import { LitElement, html } from 'lit'
|
|
58
|
+
* import { createMutationController } from '@tanstack/lit-query'
|
|
59
|
+
*
|
|
60
|
+
* class AddTodoForm extends LitElement {
|
|
61
|
+
* private readonly addTodo = createMutationController(this, {
|
|
62
|
+
* mutationFn: (title: string) =>
|
|
63
|
+
* fetch('/api/todos', { method: 'POST', body: JSON.stringify({ title }) }),
|
|
64
|
+
* })
|
|
65
|
+
*
|
|
66
|
+
* render() {
|
|
67
|
+
* const mutation = this.addTodo()
|
|
68
|
+
*
|
|
69
|
+
* return html`
|
|
70
|
+
* <button ?disabled=${mutation.isPending} @click=${() => this.addTodo.mutate('Ship docs')}>
|
|
71
|
+
* Add todo
|
|
72
|
+
* </button>
|
|
73
|
+
* `
|
|
74
|
+
* }
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function createMutationController<TData = unknown, TError = DefaultError, TVariables = void, TOnMutateResult = unknown>(host: ReactiveControllerHost, options: Accessor<CreateMutationOptions<TData, TError, TVariables, TOnMutateResult>>, queryClient?: QueryClient): MutationResultAccessor<TData, TError, TVariables, TOnMutateResult>;
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMutationController = createMutationController;
|
|
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 createIdleMutationResult() {
|
|
9
|
+
return {
|
|
10
|
+
context: undefined,
|
|
11
|
+
data: undefined,
|
|
12
|
+
error: null,
|
|
13
|
+
failureCount: 0,
|
|
14
|
+
failureReason: null,
|
|
15
|
+
isError: false,
|
|
16
|
+
isIdle: true,
|
|
17
|
+
isPending: false,
|
|
18
|
+
isPaused: false,
|
|
19
|
+
isSuccess: false,
|
|
20
|
+
status: 'idle',
|
|
21
|
+
submittedAt: 0,
|
|
22
|
+
variables: undefined,
|
|
23
|
+
mutate: (() => Promise.reject((0, context_js_1.createMissingQueryClientError)())),
|
|
24
|
+
reset: (() => undefined),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
class MutationController extends BaseController_js_1.BaseController {
|
|
28
|
+
options;
|
|
29
|
+
observer;
|
|
30
|
+
unsubscribe;
|
|
31
|
+
queryClient;
|
|
32
|
+
constructor(host, options, queryClient) {
|
|
33
|
+
super(host, createIdleMutationResult(), queryClient);
|
|
34
|
+
this.options = options;
|
|
35
|
+
if (!queryClient) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (typeof options === 'function') {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const observer = new query_core_1.MutationObserver(queryClient, this.defaultOptions(queryClient));
|
|
42
|
+
this.queryClient = queryClient;
|
|
43
|
+
this.observer = observer;
|
|
44
|
+
this.result = observer.getCurrentResult();
|
|
45
|
+
}
|
|
46
|
+
onConnected() {
|
|
47
|
+
if (!this.syncClient()) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
this.refreshOptions();
|
|
51
|
+
this.subscribe();
|
|
52
|
+
if (this.observer) {
|
|
53
|
+
this.setResult(this.observer.getCurrentResult());
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
onDisconnected() {
|
|
57
|
+
this.unsubscribeObserver();
|
|
58
|
+
this.syncClient();
|
|
59
|
+
}
|
|
60
|
+
onHostUpdate() {
|
|
61
|
+
if (typeof this.options !== 'function') {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
this.refreshOptions();
|
|
65
|
+
}
|
|
66
|
+
onQueryClientChanged() {
|
|
67
|
+
if (!this.syncClient() || !this.connectedState) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.refreshOptions();
|
|
71
|
+
this.subscribe();
|
|
72
|
+
if (this.observer) {
|
|
73
|
+
this.setResult(this.observer.getCurrentResult());
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
mutate = (variables, mutateOptions) => {
|
|
77
|
+
if (!this.syncClient() || !this.observer) {
|
|
78
|
+
throw (0, context_js_1.createMissingQueryClientError)();
|
|
79
|
+
}
|
|
80
|
+
void this.observer.mutate(variables, mutateOptions).catch(() => {
|
|
81
|
+
// Intentionally swallow in sync mutate path.
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
mutateAsync = (...args) => {
|
|
85
|
+
if (!this.syncClient() || !this.observer) {
|
|
86
|
+
return Promise.reject((0, context_js_1.createMissingQueryClientError)());
|
|
87
|
+
}
|
|
88
|
+
return this.observer.mutate(...args);
|
|
89
|
+
};
|
|
90
|
+
reset = () => {
|
|
91
|
+
if (!this.syncClient() || !this.observer) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.observer.reset();
|
|
95
|
+
this.setResult(this.observer.getCurrentResult());
|
|
96
|
+
};
|
|
97
|
+
subscribe() {
|
|
98
|
+
if (!this.observer) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (this.unsubscribe) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this.unsubscribe = this.observer.subscribe((next) => {
|
|
105
|
+
this.setResult(next);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
unsubscribeObserver() {
|
|
109
|
+
this.unsubscribe?.();
|
|
110
|
+
this.unsubscribe = undefined;
|
|
111
|
+
}
|
|
112
|
+
syncClient() {
|
|
113
|
+
const nextClient = this.tryGetQueryClient();
|
|
114
|
+
if (!nextClient) {
|
|
115
|
+
this.unsubscribeObserver();
|
|
116
|
+
this.queryClient = undefined;
|
|
117
|
+
this.observer = undefined;
|
|
118
|
+
this.setResult(createIdleMutationResult());
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
if (nextClient === this.queryClient) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
this.unsubscribeObserver();
|
|
125
|
+
this.queryClient = nextClient;
|
|
126
|
+
this.observer = new query_core_1.MutationObserver(this.queryClient, this.defaultOptions(this.queryClient));
|
|
127
|
+
this.setResult(this.observer.getCurrentResult());
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
refreshOptions() {
|
|
131
|
+
if (!this.syncClient() || !this.observer || !this.queryClient) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
this.observer.setOptions(this.defaultOptions());
|
|
135
|
+
this.setResult(this.observer.getCurrentResult());
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
defaultOptions(client = this.queryClient) {
|
|
139
|
+
if (!client) {
|
|
140
|
+
throw (0, context_js_1.createMissingQueryClientError)();
|
|
141
|
+
}
|
|
142
|
+
return client.defaultMutationOptions((0, accessor_js_1.readAccessor)(this.options));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Creates a Lit reactive controller that subscribes the host to a mutation.
|
|
147
|
+
*
|
|
148
|
+
* The returned accessor is callable and also exposes `current`, `mutate`,
|
|
149
|
+
* `mutateAsync`, `reset`, and `destroy`. When `options` is a function, it is
|
|
150
|
+
* re-read during host updates so mutation options can follow reactive host
|
|
151
|
+
* state.
|
|
152
|
+
*
|
|
153
|
+
* If `queryClient` is omitted, the controller resolves the client from the
|
|
154
|
+
* nearest connected `QueryClientProvider`.
|
|
155
|
+
*
|
|
156
|
+
* @param host - The Lit reactive controller host that owns the mutation
|
|
157
|
+
* subscription.
|
|
158
|
+
* @param options - Mutation observer options, or a getter that returns options.
|
|
159
|
+
* @param queryClient - Optional explicit query client. Provide this for
|
|
160
|
+
* controllers that should not resolve a client from Lit context.
|
|
161
|
+
* @returns An accessor for the latest mutation result with mutation helper
|
|
162
|
+
* methods.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* import { LitElement, html } from 'lit'
|
|
167
|
+
* import { createMutationController } from '@tanstack/lit-query'
|
|
168
|
+
*
|
|
169
|
+
* class AddTodoForm extends LitElement {
|
|
170
|
+
* private readonly addTodo = createMutationController(this, {
|
|
171
|
+
* mutationFn: (title: string) =>
|
|
172
|
+
* fetch('/api/todos', { method: 'POST', body: JSON.stringify({ title }) }),
|
|
173
|
+
* })
|
|
174
|
+
*
|
|
175
|
+
* render() {
|
|
176
|
+
* const mutation = this.addTodo()
|
|
177
|
+
*
|
|
178
|
+
* return html`
|
|
179
|
+
* <button ?disabled=${mutation.isPending} @click=${() => this.addTodo.mutate('Ship docs')}>
|
|
180
|
+
* Add todo
|
|
181
|
+
* </button>
|
|
182
|
+
* `
|
|
183
|
+
* }
|
|
184
|
+
* }
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
function createMutationController(host, options, queryClient) {
|
|
188
|
+
const controller = new MutationController(host, options, queryClient);
|
|
189
|
+
return Object.assign((0, accessor_js_1.createValueAccessor)(() => controller.current), {
|
|
190
|
+
mutate: controller.mutate,
|
|
191
|
+
mutateAsync: controller.mutateAsync,
|
|
192
|
+
reset: controller.reset,
|
|
193
|
+
destroy: () => controller.destroy(),
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=createMutationController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createMutationController.js","sourceRoot":"","sources":["../src/createMutationController.ts"],"names":[],"mappings":";;AAiVA,4DAuBC;AAxWD,qDAM6B;AAG7B,+CAKsB;AACtB,6CAA4D;AAC5D,uEAAgE;AAwDhE,SAAS,wBAAwB;IAM/B,OAAO;QACL,OAAO,EAAE,SAAS;QAClB,IAAI,EAAE,SAAS;QACf,KAAK,EAAE,IAAI;QACX,YAAY,EAAE,CAAC;QACf,aAAa,EAAE,IAAI;QACnB,OAAO,EAAE,KAAK;QACd,MAAM,EAAE,IAAI;QACZ,SAAS,EAAE,KAAK;QAChB,QAAQ,EAAE,KAAK;QACf,SAAS,EAAE,KAAK;QAChB,MAAM,EAAE,MAAM;QACd,WAAW,EAAE,CAAC;QACd,SAAS,EAAE,SAAS;QACpB,MAAM,EAAE,CAAC,GAAG,EAAE,CACZ,OAAO,CAAC,MAAM,CACZ,IAAA,0CAA6B,GAAE,CAChC,CAKQ;QACX,KAAK,EAAE,CAAC,GAAG,EAAE,CAAC,SAAS,CAKb;KAC2D,CAAA;AACzE,CAAC;AAED,MAAM,kBAKJ,SAAQ,kCAET;IACkB,OAAO,CAEvB;IACO,QAAQ,CAEH;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