@tanstack/solid-query 5.91.4 → 5.94.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/dev.js DELETED
@@ -1,530 +0,0 @@
1
- import { MutationObserver, shouldThrowError, QueriesObserver, noop, QueryClient as QueryClient$1, replaceEqualDeep, hydrate, notifyManager, QueryObserver, InfiniteQueryObserver } from '@tanstack/query-core';
2
- export * from '@tanstack/query-core';
3
- import { createContext, useContext, createRenderEffect, onCleanup, createMemo, createComputed, on, mergeProps, createResource, batch, onMount, createSignal, createEffect } from 'solid-js';
4
- import { createComponent, isServer } from 'solid-js/web';
5
- import { createStore, unwrap, reconcile } from 'solid-js/store';
6
-
7
- // src/useQuery.ts
8
- var QueryClientContext = createContext(void 0);
9
- var useQueryClient = (queryClient) => {
10
- if (queryClient) {
11
- return queryClient;
12
- }
13
- const client = useContext(QueryClientContext);
14
- if (!client) {
15
- throw new Error("No QueryClient set, use QueryClientProvider to set one");
16
- }
17
- return client();
18
- };
19
- var QueryClientProvider = (props) => {
20
- createRenderEffect((unmount) => {
21
- unmount?.();
22
- props.client.mount();
23
- return props.client.unmount.bind(props.client);
24
- });
25
- onCleanup(() => props.client.unmount());
26
- return createComponent(QueryClientContext.Provider, {
27
- value: () => props.client,
28
- get children() {
29
- return props.children;
30
- }
31
- });
32
- };
33
- var IsRestoringContext = createContext(() => false);
34
- var useIsRestoring = () => useContext(IsRestoringContext);
35
- var IsRestoringProvider = IsRestoringContext.Provider;
36
-
37
- // src/useBaseQuery.ts
38
- function reconcileFn(store, result, reconcileOption, queryHash) {
39
- if (reconcileOption === false) return result;
40
- if (typeof reconcileOption === "function") {
41
- const newData2 = reconcileOption(store.data, result.data);
42
- return { ...result, data: newData2 };
43
- }
44
- let data = result.data;
45
- if (store.data === void 0) {
46
- try {
47
- data = structuredClone(data);
48
- } catch (error) {
49
- {
50
- if (error instanceof Error) {
51
- console.warn(
52
- `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.
53
-
54
- Error Received: ${error.name} - ${error.message}`
55
- );
56
- }
57
- }
58
- }
59
- }
60
- const newData = reconcile(data, { key: reconcileOption })(store.data);
61
- return { ...result, data: newData };
62
- }
63
- var hydratableObserverResult = (query, result) => {
64
- if (!isServer) return result;
65
- const obj = {
66
- ...unwrap(result),
67
- // During SSR, functions cannot be serialized, so we need to remove them
68
- // This is safe because we will add these functions back when the query is hydrated
69
- refetch: void 0
70
- };
71
- if ("fetchNextPage" in result) {
72
- obj.fetchNextPage = void 0;
73
- obj.fetchPreviousPage = void 0;
74
- }
75
- obj.hydrationData = {
76
- state: query.state,
77
- queryKey: query.queryKey,
78
- queryHash: query.queryHash,
79
- ...query.meta && { meta: query.meta }
80
- };
81
- return obj;
82
- };
83
- function useBaseQuery(options, Observer, queryClient) {
84
- const client = createMemo(() => useQueryClient(queryClient?.()));
85
- const isRestoring = useIsRestoring();
86
- let unsubscribeQueued = false;
87
- const defaultedOptions = createMemo(() => {
88
- const defaultOptions = client().defaultQueryOptions(options());
89
- defaultOptions._optimisticResults = isRestoring() ? "isRestoring" : "optimistic";
90
- defaultOptions.structuralSharing = false;
91
- if (isServer) {
92
- defaultOptions.retry = false;
93
- defaultOptions.throwOnError = true;
94
- defaultOptions.experimental_prefetchInRender = true;
95
- }
96
- return defaultOptions;
97
- });
98
- const initialOptions = defaultedOptions();
99
- const [observer, setObserver] = createSignal(
100
- new Observer(client(), defaultedOptions())
101
- );
102
- let observerResult = observer().getOptimisticResult(defaultedOptions());
103
- const [state, setState] = createStore(observerResult);
104
- const createServerSubscriber = (resolve, reject) => {
105
- return observer().subscribe((result) => {
106
- notifyManager.batchCalls(() => {
107
- const query = observer().getCurrentQuery();
108
- const unwrappedResult = hydratableObserverResult(query, result);
109
- if (result.data !== void 0 && unwrappedResult.isError) {
110
- reject(unwrappedResult.error);
111
- unsubscribeIfQueued();
112
- } else {
113
- resolve(unwrappedResult);
114
- unsubscribeIfQueued();
115
- }
116
- })();
117
- });
118
- };
119
- const unsubscribeIfQueued = () => {
120
- if (unsubscribeQueued) {
121
- unsubscribe?.();
122
- unsubscribeQueued = false;
123
- }
124
- };
125
- const createClientSubscriber = () => {
126
- const obs = observer();
127
- return obs.subscribe((result) => {
128
- observerResult = result;
129
- queueMicrotask(() => {
130
- if (unsubscribe) {
131
- refetch();
132
- }
133
- });
134
- });
135
- };
136
- function setStateWithReconciliation(res) {
137
- const opts = observer().options;
138
- const reconcileOptions = opts.reconcile;
139
- setState((store) => {
140
- return reconcileFn(
141
- store,
142
- res,
143
- reconcileOptions === void 0 ? false : reconcileOptions,
144
- opts.queryHash
145
- );
146
- });
147
- }
148
- function createDeepSignal() {
149
- return [
150
- () => state,
151
- (v) => {
152
- const unwrapped = unwrap(state);
153
- if (typeof v === "function") {
154
- v = v(unwrapped);
155
- }
156
- if (v?.hydrationData) {
157
- const { hydrationData, ...rest } = v;
158
- v = rest;
159
- }
160
- setStateWithReconciliation(v);
161
- }
162
- ];
163
- }
164
- let unsubscribe = null;
165
- let resolver = null;
166
- const [queryResource, { refetch }] = createResource(
167
- () => {
168
- const obs = observer();
169
- return new Promise((resolve, reject) => {
170
- resolver = resolve;
171
- if (isServer) {
172
- unsubscribe = createServerSubscriber(resolve, reject);
173
- } else if (!unsubscribe && !isRestoring()) {
174
- unsubscribe = createClientSubscriber();
175
- }
176
- obs.updateResult();
177
- if (observerResult.isError && !observerResult.isFetching && !isRestoring() && shouldThrowError(obs.options.throwOnError, [
178
- observerResult.error,
179
- obs.getCurrentQuery()
180
- ])) {
181
- setStateWithReconciliation(observerResult);
182
- return reject(observerResult.error);
183
- }
184
- if (!observerResult.isLoading) {
185
- resolver = null;
186
- return resolve(
187
- hydratableObserverResult(obs.getCurrentQuery(), observerResult)
188
- );
189
- }
190
- setStateWithReconciliation(observerResult);
191
- });
192
- },
193
- {
194
- storage: createDeepSignal,
195
- get deferStream() {
196
- return options().deferStream;
197
- },
198
- /**
199
- * If this resource was populated on the server (either sync render, or streamed in over time), onHydrated
200
- * will be called. This is the point at which we can hydrate the query cache state, and setup the query subscriber.
201
- *
202
- * Leveraging onHydrated allows us to plug into the async and streaming support that solidjs resources already support.
203
- *
204
- * Note that this is only invoked on the client, for queries that were originally run on the server.
205
- */
206
- onHydrated(_k, info) {
207
- if (info.value && "hydrationData" in info.value) {
208
- hydrate(client(), {
209
- // @ts-expect-error - hydrationData is not correctly typed internally
210
- queries: [{ ...info.value.hydrationData }]
211
- });
212
- }
213
- if (unsubscribe) return;
214
- const newOptions = { ...initialOptions };
215
- if ((initialOptions.staleTime || !initialOptions.initialData) && info.value) {
216
- newOptions.refetchOnMount = false;
217
- }
218
- observer().setOptions(newOptions);
219
- setStateWithReconciliation(observer().getOptimisticResult(newOptions));
220
- unsubscribe = createClientSubscriber();
221
- }
222
- }
223
- );
224
- createComputed(
225
- on(
226
- client,
227
- (c) => {
228
- if (unsubscribe) {
229
- unsubscribe();
230
- }
231
- const newObserver = new Observer(c, defaultedOptions());
232
- unsubscribe = createClientSubscriber();
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);
288
- }
289
-
290
- // src/useQuery.ts
291
- function useQuery(options, queryClient) {
292
- return useBaseQuery(
293
- createMemo(() => options()),
294
- QueryObserver,
295
- queryClient
296
- );
297
- }
298
- function useInfiniteQuery(options, queryClient) {
299
- return useBaseQuery(
300
- createMemo(() => options()),
301
- InfiniteQueryObserver,
302
- queryClient
303
- );
304
- }
305
- function useMutation(options, queryClient) {
306
- const client = createMemo(() => useQueryClient(queryClient?.()));
307
- const observer = new MutationObserver(client(), options());
308
- const mutate = (variables, mutateOptions) => {
309
- observer.mutate(variables, mutateOptions).catch(noop);
310
- };
311
- const [state, setState] = createStore({
312
- ...observer.getCurrentResult(),
313
- mutate,
314
- mutateAsync: observer.getCurrentResult().mutate
315
- });
316
- createComputed(() => {
317
- observer.setOptions(options());
318
- });
319
- createComputed(
320
- on(
321
- () => state.status,
322
- () => {
323
- if (state.isError && shouldThrowError(observer.options.throwOnError, [state.error])) {
324
- throw state.error;
325
- }
326
- }
327
- )
328
- );
329
- const unsubscribe = observer.subscribe((result) => {
330
- setState({
331
- ...result,
332
- mutate,
333
- mutateAsync: result.mutate
334
- });
335
- });
336
- onCleanup(unsubscribe);
337
- return state;
338
- }
339
- function useQueries(queriesOptions, queryClient) {
340
- const client = createMemo(() => useQueryClient(queryClient?.()));
341
- const isRestoring = useIsRestoring();
342
- const defaultedQueries = createMemo(
343
- () => queriesOptions().queries.map(
344
- (options) => mergeProps(
345
- client().defaultQueryOptions(options),
346
- {
347
- get _optimisticResults() {
348
- return isRestoring() ? "isRestoring" : "optimistic";
349
- }
350
- }
351
- )
352
- )
353
- );
354
- const observer = new QueriesObserver(
355
- client(),
356
- defaultedQueries(),
357
- queriesOptions().combine ? {
358
- combine: queriesOptions().combine
359
- } : void 0
360
- );
361
- const [state, setState] = createStore(
362
- observer.getOptimisticResult(
363
- defaultedQueries(),
364
- queriesOptions().combine
365
- )[1]()
366
- );
367
- createRenderEffect(
368
- on(
369
- () => queriesOptions().queries.length,
370
- () => setState(
371
- observer.getOptimisticResult(
372
- defaultedQueries(),
373
- queriesOptions().combine
374
- )[1]()
375
- )
376
- )
377
- );
378
- const dataResources = createMemo(
379
- on(
380
- () => state.length,
381
- () => state.map((queryRes) => {
382
- const dataPromise = () => new Promise((resolve) => {
383
- if (queryRes.isFetching && queryRes.isLoading) return;
384
- resolve(unwrap(queryRes.data));
385
- });
386
- return createResource(dataPromise);
387
- })
388
- )
389
- );
390
- batch(() => {
391
- const dataResources_ = dataResources();
392
- for (let index = 0; index < dataResources_.length; index++) {
393
- const dataResource = dataResources_[index];
394
- dataResource[1].mutate(() => unwrap(state[index].data));
395
- dataResource[1].refetch();
396
- }
397
- });
398
- let taskQueue = [];
399
- const subscribeToObserver = () => observer.subscribe((result) => {
400
- taskQueue.push(() => {
401
- batch(() => {
402
- const dataResources_ = dataResources();
403
- for (let index = 0; index < dataResources_.length; index++) {
404
- const dataResource = dataResources_[index];
405
- const unwrappedResult = { ...unwrap(result[index]) };
406
- setState(index, unwrap(unwrappedResult));
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;
455
- }
456
- var QueryClient = class extends QueryClient$1 {
457
- constructor(config = {}) {
458
- super(config);
459
- }
460
- };
461
-
462
- // src/queryOptions.ts
463
- function queryOptions(options) {
464
- return options;
465
- }
466
- function useIsFetching(filters, queryClient) {
467
- const client = createMemo(() => useQueryClient(queryClient?.()));
468
- const queryCache = createMemo(() => client().getQueryCache());
469
- const [fetches, setFetches] = createSignal(client().isFetching(filters?.()));
470
- const unsubscribe = queryCache().subscribe(() => {
471
- setFetches(client().isFetching(filters?.()));
472
- });
473
- onCleanup(unsubscribe);
474
- return fetches;
475
- }
476
-
477
- // src/infiniteQueryOptions.ts
478
- function infiniteQueryOptions(options) {
479
- return options;
480
- }
481
-
482
- // src/mutationOptions.ts
483
- function mutationOptions(options) {
484
- return options;
485
- }
486
- function useIsMutating(filters, queryClient) {
487
- const client = createMemo(() => useQueryClient(queryClient?.()));
488
- const mutationCache = createMemo(() => client().getMutationCache());
489
- const [mutations, setMutations] = createSignal(
490
- client().isMutating(filters?.())
491
- );
492
- const unsubscribe = mutationCache().subscribe((_result) => {
493
- setMutations(client().isMutating(filters?.()));
494
- });
495
- onCleanup(unsubscribe);
496
- return mutations;
497
- }
498
- function getResult(mutationCache, options) {
499
- return mutationCache.findAll(options.filters).map(
500
- (mutation) => options.select ? options.select(mutation) : mutation.state
501
- );
502
- }
503
- function useMutationState(options = () => ({}), queryClient) {
504
- const client = createMemo(() => useQueryClient(queryClient?.()));
505
- const mutationCache = createMemo(() => client().getMutationCache());
506
- const [result, setResult] = createSignal(
507
- getResult(mutationCache(), options())
508
- );
509
- createEffect(() => {
510
- const unsubscribe = mutationCache().subscribe(() => {
511
- const nextResult = replaceEqualDeep(
512
- result(),
513
- getResult(mutationCache(), options())
514
- );
515
- if (result() !== nextResult) {
516
- setResult(nextResult);
517
- }
518
- });
519
- onCleanup(unsubscribe);
520
- });
521
- return result;
522
- }
523
-
524
- // src/index.ts
525
- var createQuery = useQuery;
526
- var createInfiniteQuery = useInfiniteQuery;
527
- var createMutation = useMutation;
528
- var createQueries = useQueries;
529
-
530
- export { IsRestoringProvider, QueryClient, QueryClientContext, QueryClientProvider, createInfiniteQuery, useIsFetching as createIsFetching, useIsMutating as createIsMutating, createMutation, useMutationState as createMutationState, createQueries, createQuery, infiniteQueryOptions, mutationOptions, queryOptions, useInfiniteQuery, useIsFetching, useIsMutating, useIsRestoring, useMutation, useMutationState, useQueries, useQuery, useQueryClient };