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