@tanstack/solid-query 5.101.4 → 5.102.1
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/README.md +15 -1
- package/build/dev.cjs +418 -506
- package/build/dev.js +400 -499
- package/build/index.cjs +1 -540
- package/build/index.d.cts +132 -150
- package/build/index.d.cts.map +1 -0
- package/build/index.d.ts +132 -150
- package/build/index.d.ts.map +1 -0
- package/build/index.js +2 -521
- package/build/index.js.map +1 -0
- package/package.json +6 -8
- package/src/QueryClientProvider.tsx +24 -2
- package/src/infiniteQueryOptions.ts +5 -7
- package/src/queryOptions.ts +9 -7
- package/src/useBaseQuery.ts +3 -5
- package/src/useIsFetching.ts +11 -6
- package/src/useIsMutating.ts +11 -6
- package/src/useMutation.ts +5 -4
- package/src/useMutationState.ts +35 -9
- package/src/useQueries.ts +3 -2
package/build/dev.js
CHANGED
|
@@ -1,530 +1,431 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (!client) {
|
|
15
|
-
throw new Error("No QueryClient set, use QueryClientProvider to set one");
|
|
16
|
-
}
|
|
17
|
-
return client();
|
|
1
|
+
import { InfiniteQueryObserver, MutationObserver, QueriesObserver, QueryClient as QueryClient$1, QueryObserver, hydrate, noop, notifyManager, replaceEqualDeep, shouldThrowError } from "@tanstack/query-core";
|
|
2
|
+
import { batch, createComputed, createContext, createEffect, createMemo, createRenderEffect, createResource, createSignal, mergeProps, on, onCleanup, onMount, useContext } from "solid-js";
|
|
3
|
+
import { createComponent, isServer } from "solid-js/web";
|
|
4
|
+
import { createStore, reconcile, unwrap } from "solid-js/store";
|
|
5
|
+
export * from "@tanstack/query-core";
|
|
6
|
+
//#region src/QueryClientProvider.tsx
|
|
7
|
+
const QueryClientContext = createContext(void 0);
|
|
8
|
+
const queryClientContextError = "No QueryClient set, use QueryClientProvider to set one";
|
|
9
|
+
const useQueryClient = (queryClient) => {
|
|
10
|
+
if (queryClient) return queryClient;
|
|
11
|
+
const client = useContext(QueryClientContext);
|
|
12
|
+
if (!client) throw new Error(queryClientContextError);
|
|
13
|
+
return client();
|
|
18
14
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
value: () => props.client,
|
|
28
|
-
get children() {
|
|
29
|
-
return props.children;
|
|
30
|
-
}
|
|
31
|
-
});
|
|
15
|
+
const useQueryClientResolver = (queryClient) => {
|
|
16
|
+
const contextClient = useContext(QueryClientContext);
|
|
17
|
+
return () => {
|
|
18
|
+
const resolvedClient = queryClient?.();
|
|
19
|
+
if (resolvedClient) return resolvedClient;
|
|
20
|
+
if (!contextClient) throw new Error(queryClientContextError);
|
|
21
|
+
return contextClient();
|
|
22
|
+
};
|
|
32
23
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
24
|
+
const QueryClientProvider = (props) => {
|
|
25
|
+
createRenderEffect((unmount) => {
|
|
26
|
+
unmount?.();
|
|
27
|
+
props.client.mount();
|
|
28
|
+
return props.client.unmount.bind(props.client);
|
|
29
|
+
});
|
|
30
|
+
onCleanup(() => props.client.unmount());
|
|
31
|
+
return createComponent(QueryClientContext.Provider, {
|
|
32
|
+
value: () => props.client,
|
|
33
|
+
get children() {
|
|
34
|
+
return props.children;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/isRestoring.ts
|
|
40
|
+
const IsRestoringContext = createContext(() => false);
|
|
41
|
+
const useIsRestoring = () => useContext(IsRestoringContext);
|
|
42
|
+
const IsRestoringProvider = IsRestoringContext.Provider;
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/useBaseQuery.ts
|
|
38
45
|
function reconcileFn(store, result, reconcileOption, queryHash) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
const newData = reconcile(data, { key: reconcileOption })(store.data);
|
|
61
|
-
return { ...result, data: newData };
|
|
46
|
+
if (reconcileOption === false) return result;
|
|
47
|
+
if (typeof reconcileOption === "function") {
|
|
48
|
+
const newData = reconcileOption(store.data, result.data);
|
|
49
|
+
return {
|
|
50
|
+
...result,
|
|
51
|
+
data: newData
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
let data = result.data;
|
|
55
|
+
if (store.data === void 0) try {
|
|
56
|
+
data = structuredClone(data);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (error instanceof Error) console.warn(`Unable to correctly reconcile data for query key: ${queryHash}. Possibly because the query data contains data structures that aren't supported by the 'structuredClone' algorithm. Consider using a callback function instead to manage the reconciliation manually.\n\n Error Received: ${error.name} - ${error.message}`);
|
|
59
|
+
}
|
|
60
|
+
const newData = reconcile(data, { key: reconcileOption })(store.data);
|
|
61
|
+
return {
|
|
62
|
+
...result,
|
|
63
|
+
data: newData
|
|
64
|
+
};
|
|
62
65
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
66
|
+
/**
|
|
67
|
+
* Solid's `onHydrated` functionality will silently "fail" (hydrate with an empty object)
|
|
68
|
+
* if the resource data is not serializable.
|
|
69
|
+
*/
|
|
70
|
+
const hydratableObserverResult = (query, result) => {
|
|
71
|
+
if (!isServer) return result;
|
|
72
|
+
const obj = {
|
|
73
|
+
...unwrap(result),
|
|
74
|
+
refetch: void 0
|
|
75
|
+
};
|
|
76
|
+
if ("fetchNextPage" in result) {
|
|
77
|
+
obj.fetchNextPage = void 0;
|
|
78
|
+
obj.fetchPreviousPage = void 0;
|
|
79
|
+
}
|
|
80
|
+
obj.hydrationData = {
|
|
81
|
+
state: query.state,
|
|
82
|
+
queryKey: query.queryKey,
|
|
83
|
+
queryHash: query.queryHash,
|
|
84
|
+
...query.meta && { meta: query.meta }
|
|
85
|
+
};
|
|
86
|
+
return obj;
|
|
82
87
|
};
|
|
83
88
|
function useBaseQuery(options, Observer, queryClient) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
setObserver(newObserver);
|
|
234
|
-
},
|
|
235
|
-
{
|
|
236
|
-
defer: true
|
|
237
|
-
}
|
|
238
|
-
)
|
|
239
|
-
);
|
|
240
|
-
createComputed(
|
|
241
|
-
on(
|
|
242
|
-
isRestoring,
|
|
243
|
-
(restoring) => {
|
|
244
|
-
if (!restoring && !isServer) {
|
|
245
|
-
refetch();
|
|
246
|
-
}
|
|
247
|
-
},
|
|
248
|
-
{ defer: true }
|
|
249
|
-
)
|
|
250
|
-
);
|
|
251
|
-
onCleanup(() => {
|
|
252
|
-
if (isServer && queryResource.loading) {
|
|
253
|
-
unsubscribeQueued = true;
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
if (unsubscribe) {
|
|
257
|
-
unsubscribe();
|
|
258
|
-
unsubscribe = null;
|
|
259
|
-
}
|
|
260
|
-
if (resolver && !isServer) {
|
|
261
|
-
resolver(observerResult);
|
|
262
|
-
resolver = null;
|
|
263
|
-
}
|
|
264
|
-
});
|
|
265
|
-
createComputed(
|
|
266
|
-
on(
|
|
267
|
-
[observer, defaultedOptions],
|
|
268
|
-
([obs, opts]) => {
|
|
269
|
-
obs.setOptions(opts);
|
|
270
|
-
setStateWithReconciliation(obs.getOptimisticResult(opts));
|
|
271
|
-
refetch();
|
|
272
|
-
},
|
|
273
|
-
{ defer: true }
|
|
274
|
-
)
|
|
275
|
-
);
|
|
276
|
-
const handler = {
|
|
277
|
-
get(target, prop) {
|
|
278
|
-
if (prop === "data") {
|
|
279
|
-
if (state.data !== void 0) {
|
|
280
|
-
return queryResource.latest?.data;
|
|
281
|
-
}
|
|
282
|
-
return queryResource()?.data;
|
|
283
|
-
}
|
|
284
|
-
return Reflect.get(target, prop);
|
|
285
|
-
}
|
|
286
|
-
};
|
|
287
|
-
return new Proxy(state, handler);
|
|
89
|
+
const resolveClient = useQueryClientResolver(queryClient);
|
|
90
|
+
const client = createMemo(() => resolveClient());
|
|
91
|
+
const isRestoring = useIsRestoring();
|
|
92
|
+
let unsubscribeQueued = false;
|
|
93
|
+
const defaultedOptions = createMemo(() => {
|
|
94
|
+
const defaultOptions = client().defaultQueryOptions(options());
|
|
95
|
+
defaultOptions._optimisticResults = isRestoring() ? "isRestoring" : "optimistic";
|
|
96
|
+
defaultOptions.structuralSharing = false;
|
|
97
|
+
if (isServer) {
|
|
98
|
+
defaultOptions.retry = false;
|
|
99
|
+
defaultOptions.throwOnError = true;
|
|
100
|
+
}
|
|
101
|
+
return defaultOptions;
|
|
102
|
+
});
|
|
103
|
+
const initialOptions = defaultedOptions();
|
|
104
|
+
const [observer, setObserver] = createSignal(new Observer(client(), defaultedOptions()));
|
|
105
|
+
let observerResult = observer().getOptimisticResult(defaultedOptions());
|
|
106
|
+
const [state, setState] = createStore(observerResult);
|
|
107
|
+
const createServerSubscriber = (resolve, reject) => {
|
|
108
|
+
return observer().subscribe((result) => {
|
|
109
|
+
notifyManager.batchCalls(() => {
|
|
110
|
+
const query = observer().getCurrentQuery();
|
|
111
|
+
const unwrappedResult = hydratableObserverResult(query, result);
|
|
112
|
+
if (result.data !== void 0 && unwrappedResult.isError) {
|
|
113
|
+
reject(unwrappedResult.error);
|
|
114
|
+
unsubscribeIfQueued();
|
|
115
|
+
} else {
|
|
116
|
+
resolve(unwrappedResult);
|
|
117
|
+
unsubscribeIfQueued();
|
|
118
|
+
}
|
|
119
|
+
})();
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
const unsubscribeIfQueued = () => {
|
|
123
|
+
if (unsubscribeQueued) {
|
|
124
|
+
unsubscribe?.();
|
|
125
|
+
unsubscribeQueued = false;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
const createClientSubscriber = () => {
|
|
129
|
+
return observer().subscribe((result) => {
|
|
130
|
+
observerResult = result;
|
|
131
|
+
queueMicrotask(() => {
|
|
132
|
+
if (unsubscribe) refetch();
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
};
|
|
136
|
+
function setStateWithReconciliation(res) {
|
|
137
|
+
const opts = observer().options;
|
|
138
|
+
const reconcileOptions = opts.reconcile;
|
|
139
|
+
setState((store) => {
|
|
140
|
+
return reconcileFn(store, res, reconcileOptions === void 0 ? false : reconcileOptions, opts.queryHash);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
function createDeepSignal() {
|
|
144
|
+
return [() => state, (v) => {
|
|
145
|
+
const unwrapped = unwrap(state);
|
|
146
|
+
if (typeof v === "function") v = v(unwrapped);
|
|
147
|
+
if (v?.hydrationData) {
|
|
148
|
+
const { hydrationData, ...rest } = v;
|
|
149
|
+
v = rest;
|
|
150
|
+
}
|
|
151
|
+
setStateWithReconciliation(v);
|
|
152
|
+
}];
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Unsubscribe is set lazily, so that we can subscribe after hydration when needed.
|
|
156
|
+
*/
|
|
157
|
+
let unsubscribe = null;
|
|
158
|
+
let resolver = null;
|
|
159
|
+
const [queryResource, { refetch }] = createResource(() => {
|
|
160
|
+
const obs = observer();
|
|
161
|
+
return new Promise((resolve, reject) => {
|
|
162
|
+
resolver = resolve;
|
|
163
|
+
if (isServer) unsubscribe = createServerSubscriber(resolve, reject);
|
|
164
|
+
else if (!unsubscribe && !isRestoring()) unsubscribe = createClientSubscriber();
|
|
165
|
+
obs.updateResult();
|
|
166
|
+
if (observerResult.isError && !observerResult.isFetching && !isRestoring() && shouldThrowError(obs.options.throwOnError, [observerResult.error, obs.getCurrentQuery()])) {
|
|
167
|
+
setStateWithReconciliation(observerResult);
|
|
168
|
+
return reject(observerResult.error);
|
|
169
|
+
}
|
|
170
|
+
if (!observerResult.isLoading) {
|
|
171
|
+
resolver = null;
|
|
172
|
+
return resolve(hydratableObserverResult(obs.getCurrentQuery(), observerResult));
|
|
173
|
+
}
|
|
174
|
+
setStateWithReconciliation(observerResult);
|
|
175
|
+
});
|
|
176
|
+
}, {
|
|
177
|
+
storage: createDeepSignal,
|
|
178
|
+
get deferStream() {
|
|
179
|
+
return options().deferStream;
|
|
180
|
+
},
|
|
181
|
+
/**
|
|
182
|
+
* If this resource was populated on the server (either sync render, or streamed in over time), onHydrated
|
|
183
|
+
* will be called. This is the point at which we can hydrate the query cache state, and setup the query subscriber.
|
|
184
|
+
*
|
|
185
|
+
* Leveraging onHydrated allows us to plug into the async and streaming support that solidjs resources already support.
|
|
186
|
+
*
|
|
187
|
+
* Note that this is only invoked on the client, for queries that were originally run on the server.
|
|
188
|
+
*/
|
|
189
|
+
onHydrated(_k, info) {
|
|
190
|
+
if (info.value && "hydrationData" in info.value) hydrate(client(), { queries: [{ ...info.value.hydrationData }] });
|
|
191
|
+
if (unsubscribe) return;
|
|
192
|
+
/**
|
|
193
|
+
* Do not refetch query on mount if query was fetched on server,
|
|
194
|
+
* even if `staleTime` is not set.
|
|
195
|
+
*/
|
|
196
|
+
const newOptions = { ...initialOptions };
|
|
197
|
+
if ((initialOptions.staleTime || !initialOptions.initialData) && info.value) newOptions.refetchOnMount = false;
|
|
198
|
+
observer().setOptions(newOptions);
|
|
199
|
+
setStateWithReconciliation(observer().getOptimisticResult(newOptions));
|
|
200
|
+
unsubscribe = createClientSubscriber();
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
createComputed(on(client, (c) => {
|
|
204
|
+
if (unsubscribe) unsubscribe();
|
|
205
|
+
const newObserver = new Observer(c, defaultedOptions());
|
|
206
|
+
unsubscribe = createClientSubscriber();
|
|
207
|
+
setObserver(newObserver);
|
|
208
|
+
}, { defer: true }));
|
|
209
|
+
createComputed(on(isRestoring, (restoring) => {
|
|
210
|
+
if (!restoring && !isServer) refetch();
|
|
211
|
+
}, { defer: true }));
|
|
212
|
+
onCleanup(() => {
|
|
213
|
+
if (isServer && queryResource.loading) {
|
|
214
|
+
unsubscribeQueued = true;
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (unsubscribe) {
|
|
218
|
+
unsubscribe();
|
|
219
|
+
unsubscribe = null;
|
|
220
|
+
}
|
|
221
|
+
if (resolver && !isServer) {
|
|
222
|
+
resolver(observerResult);
|
|
223
|
+
resolver = null;
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
createComputed(on([observer, defaultedOptions], ([obs, opts]) => {
|
|
227
|
+
obs.setOptions(opts);
|
|
228
|
+
setStateWithReconciliation(obs.getOptimisticResult(opts));
|
|
229
|
+
refetch();
|
|
230
|
+
}, { defer: true }));
|
|
231
|
+
return new Proxy(state, { get(target, prop) {
|
|
232
|
+
if (prop === "data") {
|
|
233
|
+
if (state.data !== void 0) return queryResource.latest?.data;
|
|
234
|
+
return queryResource()?.data;
|
|
235
|
+
}
|
|
236
|
+
return Reflect.get(target, prop);
|
|
237
|
+
} });
|
|
288
238
|
}
|
|
289
|
-
|
|
290
|
-
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/useQuery.ts
|
|
291
241
|
function useQuery(options, queryClient) {
|
|
292
|
-
|
|
293
|
-
createMemo(() => options()),
|
|
294
|
-
QueryObserver,
|
|
295
|
-
queryClient
|
|
296
|
-
);
|
|
242
|
+
return useBaseQuery(createMemo(() => options()), QueryObserver, queryClient);
|
|
297
243
|
}
|
|
244
|
+
//#endregion
|
|
245
|
+
//#region src/useInfiniteQuery.ts
|
|
298
246
|
function useInfiniteQuery(options, queryClient) {
|
|
299
|
-
|
|
300
|
-
createMemo(() => options()),
|
|
301
|
-
InfiniteQueryObserver,
|
|
302
|
-
queryClient
|
|
303
|
-
);
|
|
247
|
+
return useBaseQuery(createMemo(() => options()), InfiniteQueryObserver, queryClient);
|
|
304
248
|
}
|
|
249
|
+
//#endregion
|
|
250
|
+
//#region src/useMutation.ts
|
|
305
251
|
function useMutation(options, queryClient) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
mutate,
|
|
333
|
-
mutateAsync: result.mutate
|
|
334
|
-
});
|
|
335
|
-
});
|
|
336
|
-
onCleanup(unsubscribe);
|
|
337
|
-
return state;
|
|
252
|
+
const resolveClient = useQueryClientResolver(queryClient);
|
|
253
|
+
const client = createMemo(() => resolveClient());
|
|
254
|
+
const observer = new MutationObserver(client(), options());
|
|
255
|
+
const mutate = (...args) => {
|
|
256
|
+
observer.mutate(args[0], args[1]).catch(noop);
|
|
257
|
+
};
|
|
258
|
+
const [state, setState] = createStore({
|
|
259
|
+
...observer.getCurrentResult(),
|
|
260
|
+
mutate,
|
|
261
|
+
mutateAsync: observer.getCurrentResult().mutate
|
|
262
|
+
});
|
|
263
|
+
createComputed(() => {
|
|
264
|
+
observer.setOptions(options());
|
|
265
|
+
});
|
|
266
|
+
createComputed(on(() => state.status, () => {
|
|
267
|
+
if (state.isError && shouldThrowError(observer.options.throwOnError, [state.error])) throw state.error;
|
|
268
|
+
}));
|
|
269
|
+
const unsubscribe = observer.subscribe((result) => {
|
|
270
|
+
setState({
|
|
271
|
+
...result,
|
|
272
|
+
mutate,
|
|
273
|
+
mutateAsync: result.mutate
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
onCleanup(unsubscribe);
|
|
277
|
+
return state;
|
|
338
278
|
}
|
|
279
|
+
//#endregion
|
|
280
|
+
//#region src/useQueries.ts
|
|
339
281
|
function useQueries(queriesOptions, queryClient) {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
dataResource[1].mutate(() => unwrap(state[index].data));
|
|
408
|
-
dataResource[1].refetch();
|
|
409
|
-
}
|
|
410
|
-
});
|
|
411
|
-
});
|
|
412
|
-
queueMicrotask(() => {
|
|
413
|
-
const taskToRun = taskQueue.pop();
|
|
414
|
-
if (taskToRun) taskToRun();
|
|
415
|
-
taskQueue = [];
|
|
416
|
-
});
|
|
417
|
-
});
|
|
418
|
-
let unsubscribe = noop;
|
|
419
|
-
createComputed((cleanup) => {
|
|
420
|
-
cleanup?.();
|
|
421
|
-
unsubscribe = isRestoring() ? noop : subscribeToObserver();
|
|
422
|
-
return () => queueMicrotask(unsubscribe);
|
|
423
|
-
});
|
|
424
|
-
onCleanup(unsubscribe);
|
|
425
|
-
onMount(() => {
|
|
426
|
-
observer.setQueries(
|
|
427
|
-
defaultedQueries(),
|
|
428
|
-
queriesOptions().combine ? {
|
|
429
|
-
combine: queriesOptions().combine
|
|
430
|
-
} : void 0
|
|
431
|
-
);
|
|
432
|
-
});
|
|
433
|
-
createComputed(() => {
|
|
434
|
-
observer.setQueries(
|
|
435
|
-
defaultedQueries(),
|
|
436
|
-
queriesOptions().combine ? {
|
|
437
|
-
combine: queriesOptions().combine
|
|
438
|
-
} : void 0
|
|
439
|
-
);
|
|
440
|
-
});
|
|
441
|
-
const handler = (index) => ({
|
|
442
|
-
get(target, prop) {
|
|
443
|
-
if (prop === "data") {
|
|
444
|
-
return dataResources()[index][0]();
|
|
445
|
-
}
|
|
446
|
-
return Reflect.get(target, prop);
|
|
447
|
-
}
|
|
448
|
-
});
|
|
449
|
-
const getProxies = () => state.map((s, index) => {
|
|
450
|
-
return new Proxy(s, handler(index));
|
|
451
|
-
});
|
|
452
|
-
const [proxyState, setProxyState] = createStore(getProxies());
|
|
453
|
-
createRenderEffect(() => setProxyState(getProxies()));
|
|
454
|
-
return proxyState;
|
|
282
|
+
const resolveClient = useQueryClientResolver(queryClient);
|
|
283
|
+
const client = createMemo(() => resolveClient());
|
|
284
|
+
const isRestoring = useIsRestoring();
|
|
285
|
+
const defaultedQueries = createMemo(() => queriesOptions().queries.map((options) => mergeProps(client().defaultQueryOptions(options), { get _optimisticResults() {
|
|
286
|
+
return isRestoring() ? "isRestoring" : "optimistic";
|
|
287
|
+
} })));
|
|
288
|
+
const observer = new QueriesObserver(client(), defaultedQueries(), queriesOptions().combine ? { combine: queriesOptions().combine } : void 0);
|
|
289
|
+
const [state, setState] = createStore(observer.getOptimisticResult(defaultedQueries(), queriesOptions().combine)[1]());
|
|
290
|
+
createRenderEffect(on(() => queriesOptions().queries.length, () => setState(observer.getOptimisticResult(defaultedQueries(), queriesOptions().combine)[1]())));
|
|
291
|
+
const dataResources = createMemo(on(() => state.length, () => state.map((queryRes) => {
|
|
292
|
+
const dataPromise = () => new Promise((resolve) => {
|
|
293
|
+
if (queryRes.isFetching && queryRes.isLoading) return;
|
|
294
|
+
resolve(unwrap(queryRes.data));
|
|
295
|
+
});
|
|
296
|
+
return createResource(dataPromise);
|
|
297
|
+
})));
|
|
298
|
+
batch(() => {
|
|
299
|
+
const dataResources_ = dataResources();
|
|
300
|
+
for (let index = 0; index < dataResources_.length; index++) {
|
|
301
|
+
const dataResource = dataResources_[index];
|
|
302
|
+
dataResource[1].mutate(() => unwrap(state[index].data));
|
|
303
|
+
dataResource[1].refetch();
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
let taskQueue = [];
|
|
307
|
+
const subscribeToObserver = () => observer.subscribe((result) => {
|
|
308
|
+
taskQueue.push(() => {
|
|
309
|
+
batch(() => {
|
|
310
|
+
const dataResources_ = dataResources();
|
|
311
|
+
for (let index = 0; index < dataResources_.length; index++) {
|
|
312
|
+
const dataResource = dataResources_[index];
|
|
313
|
+
const unwrappedResult = { ...unwrap(result[index]) };
|
|
314
|
+
setState(index, unwrap(unwrappedResult));
|
|
315
|
+
dataResource[1].mutate(() => unwrap(state[index].data));
|
|
316
|
+
dataResource[1].refetch();
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
queueMicrotask(() => {
|
|
321
|
+
const taskToRun = taskQueue.pop();
|
|
322
|
+
if (taskToRun) taskToRun();
|
|
323
|
+
taskQueue = [];
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
let unsubscribe = noop;
|
|
327
|
+
createComputed((cleanup) => {
|
|
328
|
+
cleanup?.();
|
|
329
|
+
unsubscribe = isRestoring() ? noop : subscribeToObserver();
|
|
330
|
+
return () => queueMicrotask(unsubscribe);
|
|
331
|
+
});
|
|
332
|
+
onCleanup(unsubscribe);
|
|
333
|
+
onMount(() => {
|
|
334
|
+
observer.setQueries(defaultedQueries(), queriesOptions().combine ? { combine: queriesOptions().combine } : void 0);
|
|
335
|
+
});
|
|
336
|
+
createComputed(() => {
|
|
337
|
+
observer.setQueries(defaultedQueries(), queriesOptions().combine ? { combine: queriesOptions().combine } : void 0);
|
|
338
|
+
});
|
|
339
|
+
const handler = (index) => ({ get(target, prop) {
|
|
340
|
+
if (prop === "data") return dataResources()[index][0]();
|
|
341
|
+
return Reflect.get(target, prop);
|
|
342
|
+
} });
|
|
343
|
+
const getProxies = () => state.map((s, index) => {
|
|
344
|
+
return new Proxy(s, handler(index));
|
|
345
|
+
});
|
|
346
|
+
const [proxyState, setProxyState] = createStore(getProxies());
|
|
347
|
+
createRenderEffect(() => setProxyState(getProxies()));
|
|
348
|
+
return proxyState;
|
|
455
349
|
}
|
|
350
|
+
//#endregion
|
|
351
|
+
//#region src/QueryClient.ts
|
|
456
352
|
var QueryClient = class extends QueryClient$1 {
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
353
|
+
constructor(config = {}) {
|
|
354
|
+
super(config);
|
|
355
|
+
}
|
|
460
356
|
};
|
|
461
|
-
|
|
462
|
-
|
|
357
|
+
//#endregion
|
|
358
|
+
//#region src/queryOptions.ts
|
|
463
359
|
function queryOptions(options) {
|
|
464
|
-
|
|
360
|
+
return options;
|
|
465
361
|
}
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region src/useIsFetching.ts
|
|
466
364
|
function useIsFetching(filters, queryClient) {
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
365
|
+
const resolveClient = useQueryClientResolver(queryClient);
|
|
366
|
+
const client = createMemo(() => resolveClient());
|
|
367
|
+
const queryCache = createMemo(() => client().getQueryCache());
|
|
368
|
+
const [fetches, setFetches] = createSignal(client().isFetching(filters?.()));
|
|
369
|
+
createEffect(() => {
|
|
370
|
+
setFetches(client().isFetching(filters?.()));
|
|
371
|
+
const unsubscribe = queryCache().subscribe(() => {
|
|
372
|
+
setFetches(client().isFetching(filters?.()));
|
|
373
|
+
});
|
|
374
|
+
onCleanup(unsubscribe);
|
|
375
|
+
});
|
|
376
|
+
return fetches;
|
|
475
377
|
}
|
|
476
|
-
|
|
477
|
-
|
|
378
|
+
//#endregion
|
|
379
|
+
//#region src/infiniteQueryOptions.ts
|
|
478
380
|
function infiniteQueryOptions(options) {
|
|
479
|
-
|
|
381
|
+
return options;
|
|
480
382
|
}
|
|
481
|
-
|
|
482
|
-
|
|
383
|
+
//#endregion
|
|
384
|
+
//#region src/mutationOptions.ts
|
|
483
385
|
function mutationOptions(options) {
|
|
484
|
-
|
|
386
|
+
return options;
|
|
485
387
|
}
|
|
388
|
+
//#endregion
|
|
389
|
+
//#region src/useIsMutating.ts
|
|
486
390
|
function useIsMutating(filters, queryClient) {
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
391
|
+
const resolveClient = useQueryClientResolver(queryClient);
|
|
392
|
+
const client = createMemo(() => resolveClient());
|
|
393
|
+
const mutationCache = createMemo(() => client().getMutationCache());
|
|
394
|
+
const [mutations, setMutations] = createSignal(client().isMutating(filters?.()));
|
|
395
|
+
createEffect(() => {
|
|
396
|
+
setMutations(client().isMutating(filters?.()));
|
|
397
|
+
const unsubscribe = mutationCache().subscribe(() => {
|
|
398
|
+
setMutations(client().isMutating(filters?.()));
|
|
399
|
+
});
|
|
400
|
+
onCleanup(unsubscribe);
|
|
401
|
+
});
|
|
402
|
+
return mutations;
|
|
497
403
|
}
|
|
404
|
+
//#endregion
|
|
405
|
+
//#region src/useMutationState.ts
|
|
498
406
|
function getResult(mutationCache, options) {
|
|
499
|
-
|
|
500
|
-
(mutation) => options.select ? options.select(mutation) : mutation.state
|
|
501
|
-
);
|
|
407
|
+
return mutationCache.findAll(options.filters).map((mutation) => options.select ? options.select(mutation) : mutation.state);
|
|
502
408
|
}
|
|
503
409
|
function useMutationState(options = () => ({}), queryClient) {
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
setResult(nextResult);
|
|
517
|
-
}
|
|
518
|
-
});
|
|
519
|
-
onCleanup(unsubscribe);
|
|
520
|
-
});
|
|
521
|
-
return result;
|
|
410
|
+
const resolveClient = useQueryClientResolver(queryClient);
|
|
411
|
+
const client = createMemo(() => resolveClient());
|
|
412
|
+
const mutationCache = createMemo(() => client().getMutationCache());
|
|
413
|
+
const [result, setResult] = createSignal(getResult(mutationCache(), options()));
|
|
414
|
+
createEffect(() => {
|
|
415
|
+
const unsubscribe = mutationCache().subscribe(() => {
|
|
416
|
+
const nextResult = replaceEqualDeep(result(), getResult(mutationCache(), options()));
|
|
417
|
+
if (result() !== nextResult) setResult(nextResult);
|
|
418
|
+
});
|
|
419
|
+
onCleanup(unsubscribe);
|
|
420
|
+
});
|
|
421
|
+
return result;
|
|
522
422
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
423
|
+
//#endregion
|
|
424
|
+
//#region src/index.ts
|
|
425
|
+
/* istanbul ignore file */
|
|
426
|
+
const createQuery = useQuery;
|
|
427
|
+
const createInfiniteQuery = useInfiniteQuery;
|
|
428
|
+
const createMutation = useMutation;
|
|
429
|
+
const createQueries = useQueries;
|
|
430
|
+
//#endregion
|
|
431
|
+
export { IsRestoringProvider, QueryClient, QueryClientContext, QueryClientProvider, createInfiniteQuery, useIsFetching as createIsFetching, useIsFetching, useIsMutating as createIsMutating, useIsMutating, createMutation, useMutationState as createMutationState, useMutationState, createQueries, createQuery, infiniteQueryOptions, mutationOptions, queryOptions, useInfiniteQuery, useIsRestoring, useMutation, useQueries, useQuery, useQueryClient };
|