@tanstack/solid-query 6.0.0-rc.0 → 6.0.0-rc.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/_tsup-dts-rollup.d.cts +150 -157
- package/build/_tsup-dts-rollup.d.ts +150 -157
- package/build/dev.cjs +718 -559
- package/build/dev.d.cts +0 -22
- package/build/dev.d.ts +0 -22
- package/build/dev.js +721 -559
- package/build/index.cjs +718 -550
- package/build/index.d.cts +0 -22
- package/build/index.d.ts +0 -22
- package/build/index.js +721 -550
- package/package.json +6 -6
- package/src/QueryClient.ts +24 -45
- package/src/QueryClientProvider.tsx +77 -58
- package/src/cacheAggregate.ts +90 -0
- package/src/index.ts +1 -29
- package/src/types.ts +62 -44
- package/src/useBaseQuery.ts +674 -412
- package/src/useInfiniteQuery.ts +156 -4
- package/src/useIsFetching.ts +6 -16
- package/src/useIsMutating.ts +6 -16
- package/src/useMutation.ts +304 -57
- package/src/useMutationState.ts +9 -22
- package/src/useQueries.ts +61 -110
- package/src/hydrationChannel.ts +0 -260
package/build/dev.js
CHANGED
|
@@ -1,161 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { noop, QueryClient as QueryClient$1, shouldThrowError, InfiniteQueryObserver, hydrate, QueryObserver, replaceEqualDeep } from '@tanstack/query-core';
|
|
2
2
|
export * from '@tanstack/query-core';
|
|
3
|
-
import { createContext, useContext, onCleanup, createSignal,
|
|
3
|
+
import { createContext, useContext, onCleanup, createMemo, createOptimistic, createSignal, action, createRenderEffect, untrack, repeat, $TRACK, sharedConfig, createEffect, createProjection, runWithOwner, isPending, resolve } from 'solid-js';
|
|
4
4
|
import { createComponent } from '@solidjs/web';
|
|
5
5
|
|
|
6
|
-
// src/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
for (const query of cache.getAll()) {
|
|
13
|
-
if (query.state.status !== "success") continue;
|
|
14
|
-
let cached = entryCache.get(query.queryHash);
|
|
15
|
-
if (!cached || cached.state !== query.state) {
|
|
16
|
-
cached = {
|
|
17
|
-
state: query.state,
|
|
18
|
-
entry: {
|
|
19
|
-
dehydratedAt: Date.now(),
|
|
20
|
-
state: query.state,
|
|
21
|
-
queryKey: query.queryKey,
|
|
22
|
-
queryHash: query.queryHash,
|
|
23
|
-
...query.meta && { meta: query.meta },
|
|
24
|
-
...query.queryType && { queryType: query.queryType }
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
entryCache.set(query.queryHash, cached);
|
|
28
|
-
}
|
|
29
|
-
entries.push(cached.entry);
|
|
30
|
-
}
|
|
31
|
-
return entries;
|
|
32
|
-
};
|
|
33
|
-
let closed = false;
|
|
34
|
-
let pull = null;
|
|
35
|
-
const buffered = [];
|
|
36
|
-
const emit = (value) => {
|
|
37
|
-
if (closed) return;
|
|
38
|
-
if (value.done) closed = true;
|
|
39
|
-
if (pull) {
|
|
40
|
-
const resolve = pull;
|
|
41
|
-
pull = null;
|
|
42
|
-
resolve({ done: false, value });
|
|
43
|
-
} else {
|
|
44
|
-
buffered.push(value);
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
let closeTimer = null;
|
|
48
|
-
const scheduleCloseCheck = () => {
|
|
49
|
-
if (closed || closeTimer !== null) return;
|
|
50
|
-
closeTimer = setTimeout(() => {
|
|
51
|
-
closeTimer = null;
|
|
52
|
-
if (closed) return;
|
|
53
|
-
if (client.isFetching() === 0) {
|
|
54
|
-
unsubscribe();
|
|
55
|
-
emit({ entries: snapshot2(), done: true });
|
|
56
|
-
}
|
|
57
|
-
}, 0);
|
|
58
|
-
};
|
|
59
|
-
const unsubscribe = cache.subscribe((event) => {
|
|
60
|
-
if (closed) return;
|
|
61
|
-
if (event.type === "updated" && event.action.type === "success") {
|
|
62
|
-
emit({ entries: snapshot2(), done: false });
|
|
63
|
-
}
|
|
64
|
-
scheduleCloseCheck();
|
|
65
|
-
});
|
|
66
|
-
scheduleCloseCheck();
|
|
67
|
-
return {
|
|
68
|
-
[Symbol.asyncIterator]() {
|
|
69
|
-
return {
|
|
70
|
-
next() {
|
|
71
|
-
if (buffered.length > 0) {
|
|
72
|
-
return Promise.resolve({ done: false, value: buffered.shift() });
|
|
73
|
-
}
|
|
74
|
-
if (closed) {
|
|
75
|
-
return Promise.resolve({
|
|
76
|
-
done: true,
|
|
77
|
-
value: void 0
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
return new Promise(
|
|
81
|
-
(resolve) => {
|
|
82
|
-
pull = resolve;
|
|
83
|
-
}
|
|
84
|
-
);
|
|
85
|
-
},
|
|
86
|
-
return(value) {
|
|
87
|
-
if (!closed) {
|
|
88
|
-
closed = true;
|
|
89
|
-
unsubscribe();
|
|
90
|
-
if (closeTimer !== null) {
|
|
91
|
-
clearTimeout(closeTimer);
|
|
92
|
-
closeTimer = null;
|
|
93
|
-
}
|
|
94
|
-
const resolve = pull;
|
|
95
|
-
pull = null;
|
|
96
|
-
resolve?.({ done: true, value: void 0 });
|
|
97
|
-
}
|
|
98
|
-
return Promise.resolve({
|
|
99
|
-
done: true,
|
|
100
|
-
value
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
function createHydrationCoordinator(client) {
|
|
108
|
-
const applied = /* @__PURE__ */ new Map();
|
|
109
|
-
const waiters = /* @__PURE__ */ new Map();
|
|
110
|
-
let channelDone = false;
|
|
111
|
-
const fireWaiters = (queryHash) => {
|
|
112
|
-
const callbacks = waiters.get(queryHash);
|
|
113
|
-
if (!callbacks) return;
|
|
114
|
-
waiters.delete(queryHash);
|
|
115
|
-
for (const callback of callbacks) queueMicrotask(callback);
|
|
116
|
-
};
|
|
117
|
-
return {
|
|
118
|
-
applyYield(value) {
|
|
119
|
-
const fresh = value.entries.filter(
|
|
120
|
-
(entry) => applied.get(entry.queryHash) !== entry.state.dataUpdatedAt
|
|
121
|
-
);
|
|
122
|
-
if (fresh.length > 0) {
|
|
123
|
-
runWithOwner(
|
|
124
|
-
null,
|
|
125
|
-
() => hydrate(client(), { queries: fresh, mutations: [] })
|
|
126
|
-
);
|
|
127
|
-
for (const entry of fresh) {
|
|
128
|
-
applied.set(entry.queryHash, entry.state.dataUpdatedAt);
|
|
129
|
-
fireWaiters(entry.queryHash);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
if (value.done && !channelDone) {
|
|
133
|
-
channelDone = true;
|
|
134
|
-
const remaining = [...waiters.values()];
|
|
135
|
-
waiters.clear();
|
|
136
|
-
for (const callbacks of remaining) {
|
|
137
|
-
for (const callback of callbacks) queueMicrotask(callback);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
},
|
|
141
|
-
whenQueryPrimed(queryHash, callback) {
|
|
142
|
-
if (channelDone || applied.has(queryHash)) {
|
|
143
|
-
queueMicrotask(callback);
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
let list = waiters.get(queryHash);
|
|
147
|
-
if (!list) {
|
|
148
|
-
list = [];
|
|
149
|
-
waiters.set(queryHash, list);
|
|
150
|
-
}
|
|
151
|
-
list.push(callback);
|
|
152
|
-
}
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
var HydrationCoordinatorContext = createContext(null);
|
|
156
|
-
|
|
157
|
-
// src/QueryClientProvider.tsx
|
|
6
|
+
// src/index.ts
|
|
7
|
+
var QueryClient = class extends QueryClient$1 {
|
|
8
|
+
constructor(config = {}) {
|
|
9
|
+
super(config);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
158
12
|
var isServer = typeof window === "undefined";
|
|
13
|
+
var HYDRATION_KEY_PREFIX = "sq:";
|
|
159
14
|
var QueryClientContext = createContext(null);
|
|
160
15
|
var useQueryClient = (queryClient) => {
|
|
161
16
|
if (queryClient) {
|
|
@@ -167,25 +22,41 @@ var useQueryClient = (queryClient) => {
|
|
|
167
22
|
}
|
|
168
23
|
return client();
|
|
169
24
|
};
|
|
25
|
+
function serializeCacheOnServer(client) {
|
|
26
|
+
const ctx = sharedConfig.context;
|
|
27
|
+
if (!ctx || !ctx.async || ctx.noHydrate) return;
|
|
28
|
+
const cache = client.getQueryCache();
|
|
29
|
+
const seen = /* @__PURE__ */ new Set();
|
|
30
|
+
const serializeQuery = (query) => {
|
|
31
|
+
if (seen.has(query.queryHash)) return;
|
|
32
|
+
const state = query.state;
|
|
33
|
+
if (state.status === "success") {
|
|
34
|
+
seen.add(query.queryHash);
|
|
35
|
+
ctx.serialize(HYDRATION_KEY_PREFIX + query.queryHash, {
|
|
36
|
+
data: state.data,
|
|
37
|
+
t: state.dataUpdatedAt
|
|
38
|
+
});
|
|
39
|
+
} else if (state.fetchStatus !== "idle") {
|
|
40
|
+
const promise = query.promise;
|
|
41
|
+
if (!promise) return;
|
|
42
|
+
seen.add(query.queryHash);
|
|
43
|
+
ctx.serialize(HYDRATION_KEY_PREFIX + query.queryHash, promise.then(() => ({
|
|
44
|
+
data: query.state.data,
|
|
45
|
+
t: query.state.dataUpdatedAt
|
|
46
|
+
})));
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
for (const query of cache.getAll()) serializeQuery(query);
|
|
50
|
+
onCleanup(cache.subscribe((event) => serializeQuery(event.query)));
|
|
51
|
+
}
|
|
170
52
|
var QueryClientProvider = (props) => {
|
|
171
53
|
props.client.mount();
|
|
172
54
|
onCleanup(() => props.client.unmount());
|
|
173
|
-
|
|
174
|
-
const coordinator = isServer ? null : createHydrationCoordinator(() => props.client);
|
|
175
|
-
createRenderEffect(() => isServer ? void 0 : channelValue(), (value) => {
|
|
176
|
-
if (value && coordinator) {
|
|
177
|
-
coordinator.applyYield(value);
|
|
178
|
-
}
|
|
179
|
-
});
|
|
55
|
+
if (isServer) serializeCacheOnServer(props.client);
|
|
180
56
|
return createComponent(QueryClientContext, {
|
|
181
57
|
value: () => props.client,
|
|
182
58
|
get children() {
|
|
183
|
-
return
|
|
184
|
-
value: coordinator,
|
|
185
|
-
get children() {
|
|
186
|
-
return props.children;
|
|
187
|
-
}
|
|
188
|
-
});
|
|
59
|
+
return props.children;
|
|
189
60
|
}
|
|
190
61
|
});
|
|
191
62
|
};
|
|
@@ -194,260 +65,357 @@ var useIsRestoring = () => useContext(IsRestoringContext);
|
|
|
194
65
|
|
|
195
66
|
// src/useBaseQuery.ts
|
|
196
67
|
var isServer2 = typeof window === "undefined";
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
if (typeof reconcileOption === "function") {
|
|
211
|
-
const newData = reconcileOption(store.data, result.data);
|
|
212
|
-
return { ...result, data: newData };
|
|
213
|
-
}
|
|
214
|
-
if (reconcileOption === false) return result;
|
|
215
|
-
const key = reconcileOption;
|
|
216
|
-
let data = result.data;
|
|
217
|
-
if (store.data === void 0) {
|
|
218
|
-
try {
|
|
219
|
-
data = structuredClone(data);
|
|
220
|
-
} catch (error) {
|
|
221
|
-
{
|
|
222
|
-
if (error instanceof Error) {
|
|
223
|
-
console.warn(
|
|
224
|
-
`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.
|
|
225
|
-
|
|
226
|
-
Error Received: ${error.name} - ${error.message}`
|
|
227
|
-
);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
if (store.data !== void 0 && data !== void 0) {
|
|
233
|
-
reconcile(data, key)(store.data);
|
|
234
|
-
return { ...result, data: store.data };
|
|
235
|
-
}
|
|
236
|
-
return { ...result, data };
|
|
237
|
-
}
|
|
238
|
-
var hydratableObserverResult = (_query, result) => {
|
|
239
|
-
if (!isServer2) return result;
|
|
240
|
-
const obj = {
|
|
241
|
-
...snapshot(result),
|
|
242
|
-
// During SSR, functions cannot be serialized, so we need to remove them
|
|
243
|
-
// This is safe because we will add these functions back when the query is hydrated
|
|
244
|
-
refetch: void 0
|
|
68
|
+
var NEVER = new Promise(noop);
|
|
69
|
+
function metaFrom(state) {
|
|
70
|
+
return {
|
|
71
|
+
dataUpdateCount: state.dataUpdateCount,
|
|
72
|
+
dataUpdatedAt: state.dataUpdatedAt,
|
|
73
|
+
error: state.error,
|
|
74
|
+
errorUpdateCount: state.errorUpdateCount,
|
|
75
|
+
errorUpdatedAt: state.errorUpdatedAt,
|
|
76
|
+
fetchFailureCount: state.fetchFailureCount,
|
|
77
|
+
fetchFailureReason: state.fetchFailureReason,
|
|
78
|
+
fetchStatus: state.fetchStatus,
|
|
79
|
+
isInvalidated: state.isInvalidated,
|
|
80
|
+
status: state.status
|
|
245
81
|
};
|
|
246
|
-
|
|
247
|
-
obj.fetchNextPage = void 0;
|
|
248
|
-
obj.fetchPreviousPage = void 0;
|
|
249
|
-
}
|
|
250
|
-
return obj;
|
|
251
|
-
};
|
|
82
|
+
}
|
|
252
83
|
function useBaseQuery(options, Observer, queryClient) {
|
|
253
|
-
|
|
84
|
+
return useBaseQueryLayer(options, Observer, queryClient).result;
|
|
85
|
+
}
|
|
86
|
+
function useBaseQueryLayer(options, Observer, queryClient) {
|
|
87
|
+
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
254
88
|
const isRestoring = useIsRestoring();
|
|
255
|
-
let
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
89
|
+
let latestHash;
|
|
90
|
+
let latestOptions;
|
|
91
|
+
const defaultedOptions = createMemo(() => {
|
|
92
|
+
const defaulted = client().defaultQueryOptions(options());
|
|
93
|
+
defaulted._optimisticResults = isRestoring() ? "isRestoring" : "optimistic";
|
|
260
94
|
if (isServer2) {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
defaultOptions.experimental_prefetchInRender = true;
|
|
95
|
+
defaulted.retry = false;
|
|
96
|
+
defaulted.throwOnError = true;
|
|
264
97
|
}
|
|
265
|
-
|
|
98
|
+
latestHash = defaulted.queryHash;
|
|
99
|
+
latestOptions = defaulted;
|
|
100
|
+
return defaulted;
|
|
266
101
|
});
|
|
267
|
-
|
|
268
|
-
const [trackedDefaultedOptions] = createSignal(() => defaultedOptions());
|
|
102
|
+
let observer = untrack(() => new Observer(client(), defaultedOptions()));
|
|
269
103
|
createRenderEffect(
|
|
270
|
-
() =>
|
|
271
|
-
(opts) =>
|
|
272
|
-
runWithOwner(null, () => observer.setOptions(opts));
|
|
273
|
-
}
|
|
274
|
-
);
|
|
275
|
-
let observerResult = untrack(
|
|
276
|
-
() => observer.getOptimisticResult(defaultedOptions())
|
|
277
|
-
);
|
|
278
|
-
const [state, setState] = createStore(
|
|
279
|
-
_stripFnsForSSR(observerResult)
|
|
104
|
+
() => defaultedOptions(),
|
|
105
|
+
(opts) => observer.setOptions(opts)
|
|
280
106
|
);
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
} else {
|
|
290
|
-
resolve(unwrappedResult);
|
|
291
|
-
unsubscribeIfQueued();
|
|
292
|
-
}
|
|
293
|
-
})();
|
|
294
|
-
});
|
|
295
|
-
};
|
|
296
|
-
const unsubscribeIfQueued = () => {
|
|
297
|
-
if (unsubscribeQueued) {
|
|
298
|
-
unsubscribe?.();
|
|
299
|
-
unsubscribeQueued = false;
|
|
107
|
+
const hydratedMount = !isServer2 && sharedConfig.hydrating === true;
|
|
108
|
+
const [version, setVersion] = createSignal(0, { ownedWrite: true });
|
|
109
|
+
const [primed, setPrimed] = createSignal(!hydratedMount, {
|
|
110
|
+
ownedWrite: true
|
|
111
|
+
});
|
|
112
|
+
const onCacheEvent = (event) => {
|
|
113
|
+
if (event.query.queryHash === untrack(defaultedOptions).queryHash || event.query.queryHash === latestHash) {
|
|
114
|
+
setVersion((v) => v + 1);
|
|
300
115
|
}
|
|
301
116
|
};
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
const previousResult = observerResult;
|
|
305
|
-
observerResult = result;
|
|
306
|
-
runWithOwner(null, () => {
|
|
307
|
-
setStateWithReconciliation(result);
|
|
308
|
-
if (unsubscribe && !disposed && (previousResult.isLoading !== result.isLoading || previousResult.isError !== result.isError)) {
|
|
309
|
-
try {
|
|
310
|
-
refresh(queryResource);
|
|
311
|
-
} catch {
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
});
|
|
315
|
-
});
|
|
316
|
-
};
|
|
317
|
-
function setStateWithReconciliation(res) {
|
|
318
|
-
const opts = observer.options;
|
|
319
|
-
const reconcileOptions = opts.reconcile;
|
|
320
|
-
const sanitized = _stripFnsForSSR(res);
|
|
321
|
-
setState((store) => {
|
|
322
|
-
return reconcileFn(
|
|
323
|
-
store,
|
|
324
|
-
sanitized,
|
|
325
|
-
reconcileOptions === void 0 ? false : reconcileOptions,
|
|
326
|
-
opts.queryHash
|
|
327
|
-
);
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
let unsubscribe = null;
|
|
117
|
+
let observerSub = null;
|
|
118
|
+
let cacheSub = null;
|
|
331
119
|
let disposed = false;
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
120
|
+
let shouldAttach = false;
|
|
121
|
+
let activeClient = untrack(client);
|
|
122
|
+
const attach = () => {
|
|
123
|
+
if (!disposed && !observerSub && !untrack(isRestoring)) {
|
|
124
|
+
shouldAttach = true;
|
|
125
|
+
observerSub = observer.subscribe(noop);
|
|
336
126
|
}
|
|
337
127
|
};
|
|
338
|
-
const
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
128
|
+
const syncClient = (c) => {
|
|
129
|
+
if (isServer2 || c === activeClient) return;
|
|
130
|
+
activeClient = c;
|
|
131
|
+
cacheSub?.();
|
|
132
|
+
cacheSub = c.getQueryCache().subscribe(onCacheEvent);
|
|
133
|
+
observerSub?.();
|
|
134
|
+
observerSub = null;
|
|
135
|
+
observer = new Observer(c, untrack(defaultedOptions));
|
|
136
|
+
if (shouldAttach && !disposed && !untrack(isRestoring)) {
|
|
137
|
+
observerSub = observer.subscribe(noop);
|
|
344
138
|
}
|
|
345
139
|
};
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
140
|
+
const prime = (entry) => {
|
|
141
|
+
if (entry != null && typeof entry === "object" && (entry.t ?? 0) > 0) {
|
|
142
|
+
const c = untrack(client);
|
|
143
|
+
const opts = untrack(defaultedOptions);
|
|
144
|
+
runWithOwner(
|
|
145
|
+
null,
|
|
146
|
+
() => hydrate(c, {
|
|
147
|
+
mutations: [],
|
|
148
|
+
queries: [
|
|
149
|
+
{
|
|
150
|
+
queryKey: opts.queryKey,
|
|
151
|
+
queryHash: opts.queryHash,
|
|
152
|
+
dehydratedAt: entry.t,
|
|
153
|
+
...opts.meta && { meta: opts.meta },
|
|
154
|
+
...opts._type && {
|
|
155
|
+
queryType: opts._type
|
|
156
|
+
},
|
|
157
|
+
state: {
|
|
158
|
+
data: entry.data,
|
|
159
|
+
dataUpdatedAt: entry.t,
|
|
160
|
+
dataUpdateCount: 1,
|
|
161
|
+
error: null,
|
|
162
|
+
errorUpdateCount: 0,
|
|
163
|
+
errorUpdatedAt: 0,
|
|
164
|
+
fetchFailureCount: 0,
|
|
165
|
+
fetchFailureReason: null,
|
|
166
|
+
fetchMeta: null,
|
|
167
|
+
isInvalidated: false,
|
|
168
|
+
status: "success",
|
|
169
|
+
fetchStatus: "idle"
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
]
|
|
173
|
+
})
|
|
174
|
+
);
|
|
362
175
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
setStateWithReconciliation(currentResult);
|
|
386
|
-
});
|
|
387
|
-
if (currentResult.isError && !currentResult.isFetching && !restoring && shouldThrowError(opts.throwOnError, [
|
|
388
|
-
currentResult.error,
|
|
389
|
-
observer.getCurrentQuery()
|
|
390
|
-
])) {
|
|
391
|
-
return;
|
|
392
|
-
}
|
|
393
|
-
if (!currentResult.isLoading) {
|
|
394
|
-
resolver = null;
|
|
395
|
-
return resolve(
|
|
396
|
-
hydratableObserverResult(observer.getCurrentQuery(), currentResult)
|
|
397
|
-
);
|
|
176
|
+
};
|
|
177
|
+
if (!isServer2) {
|
|
178
|
+
cacheSub = activeClient.getQueryCache().subscribe(onCacheEvent);
|
|
179
|
+
const sc = sharedConfig;
|
|
180
|
+
const registryKey = HYDRATION_KEY_PREFIX + untrack(defaultedOptions).queryHash;
|
|
181
|
+
const primeAndAttach = (entry) => {
|
|
182
|
+
prime(entry);
|
|
183
|
+
setPrimed(true);
|
|
184
|
+
attach();
|
|
185
|
+
};
|
|
186
|
+
const settle = hydratedMount ? primeAndAttach : prime;
|
|
187
|
+
if (sc.has?.(registryKey)) {
|
|
188
|
+
const entry = sc.load(registryKey);
|
|
189
|
+
delete globalThis._$HY.r[registryKey];
|
|
190
|
+
if (entry != null && typeof entry === "object") {
|
|
191
|
+
if (entry.s === 1) settle(entry.v);
|
|
192
|
+
else if (entry.s === 2) settle();
|
|
193
|
+
else if (typeof entry.then === "function")
|
|
194
|
+
entry.then(settle, () => settle());
|
|
195
|
+
else settle(entry);
|
|
196
|
+
} else {
|
|
197
|
+
settle(entry);
|
|
398
198
|
}
|
|
399
|
-
})
|
|
400
|
-
|
|
401
|
-
scheduleHydratedAttach();
|
|
199
|
+
} else if (hydratedMount) {
|
|
200
|
+
primeAndAttach();
|
|
402
201
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
202
|
+
if (!hydratedMount) {
|
|
203
|
+
createRenderEffect(
|
|
204
|
+
() => isRestoring(),
|
|
205
|
+
(restoring) => {
|
|
206
|
+
if (!restoring) attach();
|
|
207
|
+
}
|
|
208
|
+
);
|
|
410
209
|
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
210
|
+
onCleanup(() => {
|
|
211
|
+
disposed = true;
|
|
212
|
+
observerSub?.();
|
|
213
|
+
observerSub = null;
|
|
214
|
+
cacheSub?.();
|
|
215
|
+
cacheSub = null;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
const query = () => {
|
|
219
|
+
version();
|
|
220
|
+
const c = client();
|
|
221
|
+
syncClient(c);
|
|
222
|
+
return c.getQueryCache().build(c, defaultedOptions());
|
|
223
|
+
};
|
|
224
|
+
const isEnabled = () => {
|
|
225
|
+
const opts = defaultedOptions();
|
|
226
|
+
const enabled = typeof opts.enabled === "function" ? opts.enabled(untrack(query)) : opts.enabled;
|
|
227
|
+
return enabled !== false;
|
|
228
|
+
};
|
|
229
|
+
const resolvePlaceholder = (opts) => {
|
|
230
|
+
const placeholder = opts.placeholderData;
|
|
231
|
+
if (placeholder === void 0) return void 0;
|
|
232
|
+
return typeof placeholder === "function" ? placeholder() : placeholder;
|
|
233
|
+
};
|
|
234
|
+
let chained = null;
|
|
235
|
+
const chainOnce = (base, select, wrap) => {
|
|
236
|
+
if (chained?.base !== base || chained.select !== select) {
|
|
237
|
+
chained = { base, select, value: base.then(wrap) };
|
|
414
238
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
239
|
+
return chained.value;
|
|
240
|
+
};
|
|
241
|
+
const computeData = (prev) => {
|
|
242
|
+
const opts = defaultedOptions();
|
|
243
|
+
const q = query();
|
|
244
|
+
const state = q.state;
|
|
245
|
+
const select = opts.select;
|
|
246
|
+
const wrap = (d) => ({
|
|
247
|
+
value: select ? select(d) : d
|
|
248
|
+
});
|
|
249
|
+
if (state.data === void 0 && state.status === "pending") {
|
|
250
|
+
const placeholder = resolvePlaceholder(opts);
|
|
251
|
+
if (placeholder !== void 0) return wrap(placeholder);
|
|
418
252
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
"failureCount",
|
|
424
|
-
"failureReason",
|
|
425
|
-
"errorUpdateCount",
|
|
426
|
-
"errorUpdatedAt"
|
|
427
|
-
]);
|
|
428
|
-
return new Proxy(state, {
|
|
429
|
-
get(target, prop, receiver) {
|
|
430
|
-
if (typeof prop === "symbol") {
|
|
431
|
-
return Reflect.get(target, prop, receiver);
|
|
253
|
+
if (state.fetchStatus !== "idle") {
|
|
254
|
+
if (state.data === void 0 || prev !== void 0) {
|
|
255
|
+
const promise = q.promise;
|
|
256
|
+
if (promise) return chainOnce(promise, select, wrap);
|
|
432
257
|
}
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
return Reflect.get(resolved, prop);
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
if (errorPassthroughProps.has(prop)) {
|
|
440
|
-
return Reflect.get(target, prop, receiver);
|
|
441
|
-
}
|
|
442
|
-
if (state.isError && !state.isFetching && shouldThrowError(observer.options.throwOnError, [
|
|
258
|
+
}
|
|
259
|
+
if (state.status === "error") {
|
|
260
|
+
if (state.data === void 0 || shouldThrowError(opts.throwOnError, [
|
|
443
261
|
state.error,
|
|
444
|
-
|
|
262
|
+
q
|
|
445
263
|
])) {
|
|
446
264
|
throw state.error;
|
|
447
265
|
}
|
|
448
|
-
return Reflect.get(target, prop, receiver);
|
|
449
266
|
}
|
|
267
|
+
if (state.data !== void 0) return wrap(state.data);
|
|
268
|
+
if (isEnabled()) {
|
|
269
|
+
if (!isServer2 && sharedConfig.hydrating) {
|
|
270
|
+
return NEVER;
|
|
271
|
+
}
|
|
272
|
+
if (!primed()) {
|
|
273
|
+
return NEVER;
|
|
274
|
+
}
|
|
275
|
+
if (isRestoring()) {
|
|
276
|
+
return NEVER;
|
|
277
|
+
}
|
|
278
|
+
if (!isServer2) observer.setOptions(opts);
|
|
279
|
+
return chainOnce(q.fetch(opts), select, wrap);
|
|
280
|
+
}
|
|
281
|
+
return NEVER;
|
|
282
|
+
};
|
|
283
|
+
const dataStore = createProjection(
|
|
284
|
+
(draft) => computeData(draft.value),
|
|
285
|
+
{},
|
|
286
|
+
{
|
|
287
|
+
key: untrack(() => options().reconcile),
|
|
288
|
+
deferStream: untrack(() => options().deferStream)
|
|
289
|
+
}
|
|
290
|
+
);
|
|
291
|
+
const data = () => dataStore.value;
|
|
292
|
+
const serverMeta = () => {
|
|
293
|
+
if (query().state.status === "pending" && isEnabled()) data();
|
|
294
|
+
return metaFrom(query().state);
|
|
295
|
+
};
|
|
296
|
+
const metaProjection = createProjection(
|
|
297
|
+
(draft) => {
|
|
298
|
+
Object.assign(draft, metaFrom(query().state));
|
|
299
|
+
},
|
|
300
|
+
untrack(() => metaFrom(query().state))
|
|
301
|
+
);
|
|
302
|
+
const meta = isServer2 ? new Proxy({}, {
|
|
303
|
+
get: (_, key) => serverMeta()[key]
|
|
304
|
+
}) : metaProjection;
|
|
305
|
+
const mountedAt = isServer2 ? {
|
|
306
|
+
get dataUpdateCount() {
|
|
307
|
+
return untrack(query).state.dataUpdateCount;
|
|
308
|
+
},
|
|
309
|
+
get errorUpdateCount() {
|
|
310
|
+
return untrack(query).state.errorUpdateCount;
|
|
311
|
+
}
|
|
312
|
+
} : untrack(() => {
|
|
313
|
+
const state = query().state;
|
|
314
|
+
return {
|
|
315
|
+
dataUpdateCount: state.dataUpdateCount,
|
|
316
|
+
errorUpdateCount: state.errorUpdateCount
|
|
317
|
+
};
|
|
450
318
|
});
|
|
319
|
+
const hasPlaceholder = () => meta.status === "pending" && untrack(() => resolvePlaceholder(defaultedOptions())) !== void 0;
|
|
320
|
+
const status = () => hasPlaceholder() ? "success" : meta.status;
|
|
321
|
+
const isPending$1 = () => status() === "pending";
|
|
322
|
+
const isFetching = () => meta.fetchStatus === "fetching" || isPending(() => data());
|
|
323
|
+
const isError = () => status() === "error";
|
|
324
|
+
const isStale = () => {
|
|
325
|
+
const opts = defaultedOptions();
|
|
326
|
+
const q = untrack(query);
|
|
327
|
+
const staleTime = typeof opts.staleTime === "function" ? opts.staleTime(q) : opts.staleTime;
|
|
328
|
+
version();
|
|
329
|
+
return q.isStaleByTime(staleTime);
|
|
330
|
+
};
|
|
331
|
+
const result = {
|
|
332
|
+
get data() {
|
|
333
|
+
return data();
|
|
334
|
+
},
|
|
335
|
+
get error() {
|
|
336
|
+
return meta.error;
|
|
337
|
+
},
|
|
338
|
+
get status() {
|
|
339
|
+
return status();
|
|
340
|
+
},
|
|
341
|
+
get fetchStatus() {
|
|
342
|
+
return meta.fetchStatus;
|
|
343
|
+
},
|
|
344
|
+
get isPending() {
|
|
345
|
+
return isPending$1();
|
|
346
|
+
},
|
|
347
|
+
get isSuccess() {
|
|
348
|
+
return status() === "success";
|
|
349
|
+
},
|
|
350
|
+
get isError() {
|
|
351
|
+
return isError();
|
|
352
|
+
},
|
|
353
|
+
get isLoading() {
|
|
354
|
+
return isPending$1() && isFetching();
|
|
355
|
+
},
|
|
356
|
+
get isFetching() {
|
|
357
|
+
return isFetching();
|
|
358
|
+
},
|
|
359
|
+
get isRefetching() {
|
|
360
|
+
return isFetching() && !isPending$1();
|
|
361
|
+
},
|
|
362
|
+
get isPaused() {
|
|
363
|
+
return meta.fetchStatus === "paused";
|
|
364
|
+
},
|
|
365
|
+
get isEnabled() {
|
|
366
|
+
return isEnabled();
|
|
367
|
+
},
|
|
368
|
+
get isLoadingError() {
|
|
369
|
+
return isError() && meta.dataUpdatedAt === 0;
|
|
370
|
+
},
|
|
371
|
+
get isRefetchError() {
|
|
372
|
+
return isError() && meta.dataUpdatedAt !== 0;
|
|
373
|
+
},
|
|
374
|
+
get isPlaceholderData() {
|
|
375
|
+
return hasPlaceholder();
|
|
376
|
+
},
|
|
377
|
+
get isStale() {
|
|
378
|
+
return isStale();
|
|
379
|
+
},
|
|
380
|
+
get isFetched() {
|
|
381
|
+
return meta.dataUpdateCount > 0 || meta.errorUpdateCount > 0;
|
|
382
|
+
},
|
|
383
|
+
get isFetchedAfterMount() {
|
|
384
|
+
return meta.dataUpdateCount > mountedAt.dataUpdateCount || meta.errorUpdateCount > mountedAt.errorUpdateCount;
|
|
385
|
+
},
|
|
386
|
+
get dataUpdatedAt() {
|
|
387
|
+
return meta.dataUpdatedAt;
|
|
388
|
+
},
|
|
389
|
+
get errorUpdatedAt() {
|
|
390
|
+
return meta.errorUpdatedAt;
|
|
391
|
+
},
|
|
392
|
+
get failureCount() {
|
|
393
|
+
return meta.fetchFailureCount;
|
|
394
|
+
},
|
|
395
|
+
get failureReason() {
|
|
396
|
+
return meta.fetchFailureReason;
|
|
397
|
+
},
|
|
398
|
+
get errorUpdateCount() {
|
|
399
|
+
return meta.errorUpdateCount;
|
|
400
|
+
},
|
|
401
|
+
get promise() {
|
|
402
|
+
return resolve(() => data());
|
|
403
|
+
},
|
|
404
|
+
refetch: ((refetchOptions) => {
|
|
405
|
+
if (!isServer2 && latestOptions) {
|
|
406
|
+
observer.setOptions(latestOptions);
|
|
407
|
+
}
|
|
408
|
+
return observer.refetch(refetchOptions);
|
|
409
|
+
})
|
|
410
|
+
};
|
|
411
|
+
return {
|
|
412
|
+
result,
|
|
413
|
+
observer,
|
|
414
|
+
query,
|
|
415
|
+
defaultedOptions,
|
|
416
|
+
isFetching,
|
|
417
|
+
status
|
|
418
|
+
};
|
|
451
419
|
}
|
|
452
420
|
|
|
453
421
|
// src/useQuery.ts
|
|
@@ -458,157 +426,334 @@ function useQuery(options, queryClient) {
|
|
|
458
426
|
queryClient
|
|
459
427
|
);
|
|
460
428
|
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
queryClient
|
|
466
|
-
);
|
|
429
|
+
|
|
430
|
+
// src/queryOptions.ts
|
|
431
|
+
function queryOptions(options) {
|
|
432
|
+
return options;
|
|
467
433
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
const
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
)
|
|
475
|
-
);
|
|
476
|
-
createRenderEffect(
|
|
477
|
-
() => options(),
|
|
478
|
-
(opts) => {
|
|
479
|
-
observer.setOptions(opts);
|
|
480
|
-
}
|
|
434
|
+
var isServer3 = typeof window === "undefined";
|
|
435
|
+
function createCacheAggregate(subscribe, read, empty) {
|
|
436
|
+
const hydratedMount = !isServer3 && sharedConfig.hydrating === true;
|
|
437
|
+
const [durable, setDurable] = createSignal(
|
|
438
|
+
hydratedMount || isServer3 ? empty : untrack(() => read(empty)),
|
|
439
|
+
{ ownedWrite: true }
|
|
481
440
|
);
|
|
482
|
-
const
|
|
483
|
-
|
|
484
|
-
};
|
|
485
|
-
const initialResult = untrack(() => observer.getCurrentResult());
|
|
486
|
-
const [state, setState] = createStore({
|
|
487
|
-
...initialResult,
|
|
488
|
-
mutate,
|
|
489
|
-
mutateAsync: initialResult.mutate
|
|
441
|
+
const [value, setValue] = createOptimistic(() => durable(), {
|
|
442
|
+
ownedWrite: true
|
|
490
443
|
});
|
|
491
|
-
const
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
}
|
|
500
|
-
onCleanup(unsubscribe);
|
|
501
|
-
|
|
444
|
+
const sync = () => {
|
|
445
|
+
const next = untrack(() => read(untrack(durable)));
|
|
446
|
+
setDurable(() => next);
|
|
447
|
+
setValue(() => next);
|
|
448
|
+
};
|
|
449
|
+
let unsubscribe = null;
|
|
450
|
+
const attach = () => {
|
|
451
|
+
unsubscribe ??= untrack(() => subscribe(sync));
|
|
452
|
+
};
|
|
453
|
+
onCleanup(() => unsubscribe?.());
|
|
454
|
+
createEffect(
|
|
455
|
+
() => void 0,
|
|
502
456
|
() => {
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
throw error;
|
|
457
|
+
if (hydratedMount) {
|
|
458
|
+
attach();
|
|
459
|
+
sync();
|
|
507
460
|
}
|
|
508
|
-
},
|
|
509
|
-
() => {
|
|
510
461
|
}
|
|
511
462
|
);
|
|
512
|
-
return
|
|
463
|
+
if (isServer3) return () => empty;
|
|
464
|
+
if (!hydratedMount) attach();
|
|
465
|
+
return value;
|
|
513
466
|
}
|
|
514
|
-
|
|
467
|
+
|
|
468
|
+
// src/useIsFetching.ts
|
|
469
|
+
function useIsFetching(filters, queryClient) {
|
|
515
470
|
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
() =>
|
|
519
|
-
|
|
520
|
-
get _optimisticResults() {
|
|
521
|
-
return isRestoring() ? "isRestoring" : "optimistic";
|
|
522
|
-
}
|
|
523
|
-
})
|
|
524
|
-
)
|
|
471
|
+
return createCacheAggregate(
|
|
472
|
+
(onEvent) => client().getQueryCache().subscribe(onEvent),
|
|
473
|
+
() => client().isFetching(filters?.()),
|
|
474
|
+
0
|
|
525
475
|
);
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
476
|
+
}
|
|
477
|
+
function hasNextPage(options, data) {
|
|
478
|
+
if (!data || data.pages.length === 0) return false;
|
|
479
|
+
const lastIndex = data.pages.length - 1;
|
|
480
|
+
return options.getNextPageParam(
|
|
481
|
+
data.pages[lastIndex],
|
|
482
|
+
data.pages,
|
|
483
|
+
data.pageParams[lastIndex],
|
|
484
|
+
data.pageParams
|
|
485
|
+
) != null;
|
|
486
|
+
}
|
|
487
|
+
function hasPreviousPage(options, data) {
|
|
488
|
+
if (!data || data.pages.length === 0 || !options.getPreviousPageParam)
|
|
489
|
+
return false;
|
|
490
|
+
return options.getPreviousPageParam(
|
|
491
|
+
data.pages[0],
|
|
492
|
+
data.pages,
|
|
493
|
+
data.pageParams[0],
|
|
494
|
+
data.pageParams
|
|
495
|
+
) != null;
|
|
496
|
+
}
|
|
497
|
+
function useInfiniteQuery(options, queryClient) {
|
|
498
|
+
const layer = useBaseQueryLayer(
|
|
499
|
+
// `_type` is how query-core stamps a cache entry as infinite
|
|
500
|
+
// (`Query.setOptions` → `#queryType`), which makes every fetch attach
|
|
501
|
+
// `infiniteQueryBehavior`. Core only sets it inside
|
|
502
|
+
// `InfiniteQueryObserver.setOptions`, but the adapter's pull path can
|
|
503
|
+
// build and fetch the entry first (reactive key change recomputes the
|
|
504
|
+
// data memo before the deferred observer effect runs) — without the
|
|
505
|
+
// stamp that fetch would run as a plain query and corrupt the entry.
|
|
506
|
+
createMemo(() => ({ ...options(), _type: "infinite" })),
|
|
507
|
+
InfiniteQueryObserver,
|
|
508
|
+
queryClient
|
|
544
509
|
);
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
510
|
+
const observer = layer.observer;
|
|
511
|
+
const direction = () => layer.query().state.fetchMeta?.fetchMore?.direction;
|
|
512
|
+
const pageOptions = () => layer.defaultedOptions();
|
|
513
|
+
const infiniteData = () => layer.query().state.data;
|
|
514
|
+
const isFetchingNextPage = () => layer.isFetching() && direction() === "forward";
|
|
515
|
+
const isFetchingPreviousPage = () => layer.isFetching() && direction() === "backward";
|
|
516
|
+
const isError = () => layer.status() === "error";
|
|
517
|
+
const result = Object.defineProperties(
|
|
518
|
+
{},
|
|
519
|
+
{
|
|
520
|
+
...Object.getOwnPropertyDescriptors(layer.result),
|
|
521
|
+
fetchNextPage: {
|
|
522
|
+
value: (fetchOptions) => observer.fetchNextPage(fetchOptions),
|
|
523
|
+
enumerable: true
|
|
524
|
+
},
|
|
525
|
+
fetchPreviousPage: {
|
|
526
|
+
value: (fetchOptions) => observer.fetchPreviousPage(fetchOptions),
|
|
527
|
+
enumerable: true
|
|
528
|
+
},
|
|
529
|
+
hasNextPage: {
|
|
530
|
+
get: () => hasNextPage(pageOptions(), infiniteData()),
|
|
531
|
+
enumerable: true
|
|
532
|
+
},
|
|
533
|
+
hasPreviousPage: {
|
|
534
|
+
get: () => hasPreviousPage(pageOptions(), infiniteData()),
|
|
535
|
+
enumerable: true
|
|
536
|
+
},
|
|
537
|
+
isFetchingNextPage: {
|
|
538
|
+
get: isFetchingNextPage,
|
|
539
|
+
enumerable: true
|
|
540
|
+
},
|
|
541
|
+
isFetchingPreviousPage: {
|
|
542
|
+
get: isFetchingPreviousPage,
|
|
543
|
+
enumerable: true
|
|
544
|
+
},
|
|
545
|
+
isFetchNextPageError: {
|
|
546
|
+
get: () => isError() && direction() === "forward",
|
|
547
|
+
enumerable: true
|
|
548
|
+
},
|
|
549
|
+
isFetchPreviousPageError: {
|
|
550
|
+
get: () => isError() && direction() === "backward",
|
|
551
|
+
enumerable: true
|
|
552
|
+
},
|
|
553
|
+
isRefetching: {
|
|
554
|
+
get: () => layer.isFetching() && layer.status() !== "pending" && !isFetchingNextPage() && !isFetchingPreviousPage(),
|
|
555
|
+
enumerable: true
|
|
556
|
+
},
|
|
557
|
+
isRefetchError: {
|
|
558
|
+
get: () => {
|
|
559
|
+
const errored = isError() && layer.query().state.dataUpdatedAt !== 0;
|
|
560
|
+
return errored && direction() === void 0;
|
|
561
|
+
},
|
|
562
|
+
enumerable: true
|
|
561
563
|
}
|
|
562
|
-
},
|
|
563
|
-
() => {
|
|
564
564
|
}
|
|
565
565
|
);
|
|
566
|
-
|
|
567
|
-
unsubscribe();
|
|
568
|
-
});
|
|
569
|
-
createMemo(() => {
|
|
570
|
-
const queries = defaultedQueries();
|
|
571
|
-
runWithOwner(null, () => {
|
|
572
|
-
observer.setQueries(
|
|
573
|
-
queries,
|
|
574
|
-
queriesOptions().combine ? {
|
|
575
|
-
combine: queriesOptions().combine
|
|
576
|
-
} : void 0
|
|
577
|
-
);
|
|
578
|
-
});
|
|
579
|
-
return queries;
|
|
580
|
-
});
|
|
581
|
-
return state;
|
|
566
|
+
return result;
|
|
582
567
|
}
|
|
583
|
-
var QueryClient = class extends QueryClient$1 {
|
|
584
|
-
constructor(config = {}) {
|
|
585
|
-
super(config);
|
|
586
|
-
}
|
|
587
|
-
};
|
|
588
568
|
|
|
589
|
-
// src/
|
|
590
|
-
function
|
|
569
|
+
// src/infiniteQueryOptions.ts
|
|
570
|
+
function infiniteQueryOptions(options) {
|
|
591
571
|
return options;
|
|
592
572
|
}
|
|
593
|
-
|
|
573
|
+
var isServer4 = typeof window === "undefined";
|
|
574
|
+
var IDLE = {
|
|
575
|
+
status: "idle",
|
|
576
|
+
data: void 0,
|
|
577
|
+
error: null,
|
|
578
|
+
variables: void 0,
|
|
579
|
+
submittedAt: 0
|
|
580
|
+
};
|
|
581
|
+
async function drain(iterator) {
|
|
582
|
+
let input;
|
|
583
|
+
for (; ; ) {
|
|
584
|
+
const result = await iterator.next(input);
|
|
585
|
+
if (result.done) return result.value;
|
|
586
|
+
input = result.value != null ? await result.value : void 0;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
function useMutation(options, queryClient) {
|
|
594
590
|
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
595
|
-
const
|
|
596
|
-
const [
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
)
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
591
|
+
const [flight, setFlight] = createOptimistic(null);
|
|
592
|
+
const [settled, setSettled] = createSignal(IDLE);
|
|
593
|
+
let activeMutation = null;
|
|
594
|
+
const [flightVersion, setFlightVersion] = createSignal(0);
|
|
595
|
+
if (!isServer4) {
|
|
596
|
+
const unsubscribe = client().getMutationCache().subscribe((event) => {
|
|
597
|
+
if ("mutation" in event && event.mutation === activeMutation) {
|
|
598
|
+
setFlightVersion((v) => v + 1);
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
onCleanup(unsubscribe);
|
|
602
|
+
}
|
|
603
|
+
async function* run(variables) {
|
|
604
|
+
const opts = untrack(options);
|
|
605
|
+
const callbackContext = {
|
|
606
|
+
client: untrack(client),
|
|
607
|
+
meta: opts.meta,
|
|
608
|
+
mutationKey: opts.mutationKey
|
|
609
|
+
};
|
|
610
|
+
if (!isServer4) setFlight({ variables });
|
|
611
|
+
opts.onMutate?.(variables, callbackContext);
|
|
612
|
+
const mutation = client().getMutationCache().build(client(), {
|
|
613
|
+
...opts,
|
|
614
|
+
// Options-level callbacks re-run below, inside the transaction —
|
|
615
|
+
// executing them here (inside the await gap) would let their
|
|
616
|
+
// cache writes and invalidations escape the atomic settle.
|
|
617
|
+
onMutate: void 0,
|
|
618
|
+
onSuccess: void 0,
|
|
619
|
+
onError: void 0,
|
|
620
|
+
onSettled: void 0
|
|
621
|
+
});
|
|
622
|
+
activeMutation = mutation;
|
|
623
|
+
async function* settleFailure(error) {
|
|
624
|
+
try {
|
|
625
|
+
if (opts.onError)
|
|
626
|
+
yield opts.onError(error, variables, void 0, callbackContext);
|
|
627
|
+
} catch (callbackError) {
|
|
628
|
+
void Promise.reject(callbackError);
|
|
629
|
+
}
|
|
630
|
+
try {
|
|
631
|
+
if (opts.onSettled)
|
|
632
|
+
yield opts.onSettled(
|
|
633
|
+
void 0,
|
|
634
|
+
error,
|
|
635
|
+
variables,
|
|
636
|
+
void 0,
|
|
637
|
+
callbackContext
|
|
638
|
+
);
|
|
639
|
+
} catch (callbackError) {
|
|
640
|
+
void Promise.reject(callbackError);
|
|
641
|
+
}
|
|
642
|
+
if (!isServer4)
|
|
643
|
+
setSettled({
|
|
644
|
+
status: "error",
|
|
645
|
+
data: void 0,
|
|
646
|
+
error,
|
|
647
|
+
variables,
|
|
648
|
+
submittedAt: mutation.state.submittedAt
|
|
649
|
+
});
|
|
650
|
+
activeMutation = null;
|
|
651
|
+
throw error;
|
|
652
|
+
}
|
|
653
|
+
let result2;
|
|
654
|
+
try {
|
|
655
|
+
result2 = await mutation.execute(variables);
|
|
656
|
+
} catch (error) {
|
|
657
|
+
yield;
|
|
658
|
+
yield* settleFailure(error);
|
|
659
|
+
throw error;
|
|
660
|
+
}
|
|
661
|
+
yield;
|
|
662
|
+
try {
|
|
663
|
+
if (opts.onSuccess)
|
|
664
|
+
yield opts.onSuccess(
|
|
665
|
+
result2,
|
|
666
|
+
variables,
|
|
667
|
+
void 0,
|
|
668
|
+
callbackContext
|
|
669
|
+
);
|
|
670
|
+
if (opts.onSettled)
|
|
671
|
+
yield opts.onSettled(
|
|
672
|
+
result2,
|
|
673
|
+
null,
|
|
674
|
+
variables,
|
|
675
|
+
void 0,
|
|
676
|
+
callbackContext
|
|
677
|
+
);
|
|
678
|
+
} catch (callbackError) {
|
|
679
|
+
yield* settleFailure(callbackError);
|
|
680
|
+
throw callbackError;
|
|
681
|
+
}
|
|
682
|
+
if (!isServer4)
|
|
683
|
+
setSettled({
|
|
684
|
+
status: "success",
|
|
685
|
+
data: result2,
|
|
686
|
+
error: null,
|
|
687
|
+
variables,
|
|
688
|
+
submittedAt: mutation.state.submittedAt
|
|
689
|
+
});
|
|
690
|
+
activeMutation = null;
|
|
691
|
+
return result2;
|
|
692
|
+
}
|
|
693
|
+
const invoke = isServer4 ? (variables) => drain(run(variables)) : action(run);
|
|
694
|
+
const mutate = (variables) => {
|
|
695
|
+
const promise = invoke(variables);
|
|
696
|
+
promise.catch(noop);
|
|
697
|
+
return promise;
|
|
698
|
+
};
|
|
699
|
+
const status = () => flight() !== null ? "pending" : settled().status;
|
|
700
|
+
const isPending = () => flight() !== null;
|
|
701
|
+
createRenderEffect(
|
|
702
|
+
() => {
|
|
703
|
+
const state = settled();
|
|
704
|
+
if (state.status === "error" && shouldThrowError(untrack(options).throwOnError, [state.error])) {
|
|
705
|
+
throw state.error;
|
|
706
|
+
}
|
|
707
|
+
},
|
|
708
|
+
() => {
|
|
709
|
+
}
|
|
604
710
|
);
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
711
|
+
const result = {
|
|
712
|
+
mutate,
|
|
713
|
+
get data() {
|
|
714
|
+
return settled().data;
|
|
715
|
+
},
|
|
716
|
+
get error() {
|
|
717
|
+
return flight() !== null ? null : settled().error;
|
|
718
|
+
},
|
|
719
|
+
get variables() {
|
|
720
|
+
return flight()?.variables ?? settled().variables;
|
|
721
|
+
},
|
|
722
|
+
get status() {
|
|
723
|
+
return status();
|
|
724
|
+
},
|
|
725
|
+
get isPending() {
|
|
726
|
+
return isPending();
|
|
727
|
+
},
|
|
728
|
+
get isIdle() {
|
|
729
|
+
return status() === "idle";
|
|
730
|
+
},
|
|
731
|
+
get isSuccess() {
|
|
732
|
+
return status() === "success";
|
|
733
|
+
},
|
|
734
|
+
get isError() {
|
|
735
|
+
return status() === "error";
|
|
736
|
+
},
|
|
737
|
+
get submittedAt() {
|
|
738
|
+
return flight() !== null ? (flightVersion(), activeMutation?.state.submittedAt ?? 0) : settled().submittedAt;
|
|
739
|
+
},
|
|
740
|
+
get failureCount() {
|
|
741
|
+
flightVersion();
|
|
742
|
+
return activeMutation?.state.failureCount ?? 0;
|
|
743
|
+
},
|
|
744
|
+
get failureReason() {
|
|
745
|
+
flightVersion();
|
|
746
|
+
return activeMutation?.state.failureReason ?? null;
|
|
747
|
+
},
|
|
748
|
+
get isPaused() {
|
|
749
|
+
flightVersion();
|
|
750
|
+
return activeMutation?.state.isPaused ?? false;
|
|
751
|
+
},
|
|
752
|
+
reset: () => {
|
|
753
|
+
setSettled(IDLE);
|
|
754
|
+
}
|
|
755
|
+
};
|
|
756
|
+
return result;
|
|
612
757
|
}
|
|
613
758
|
|
|
614
759
|
// src/mutationOptions.ts
|
|
@@ -617,18 +762,11 @@ function mutationOptions(options) {
|
|
|
617
762
|
}
|
|
618
763
|
function useIsMutating(filters, queryClient) {
|
|
619
764
|
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
);
|
|
625
|
-
const unsubscribe = untrack(
|
|
626
|
-
() => mutationCache().subscribe((_result) => {
|
|
627
|
-
setMutations(client().isMutating(filters?.()));
|
|
628
|
-
})
|
|
765
|
+
return createCacheAggregate(
|
|
766
|
+
(onEvent) => client().getMutationCache().subscribe(onEvent),
|
|
767
|
+
() => client().isMutating(filters?.()),
|
|
768
|
+
0
|
|
629
769
|
);
|
|
630
|
-
onCleanup(unsubscribe);
|
|
631
|
-
return mutations;
|
|
632
770
|
}
|
|
633
771
|
function getResult(mutationCache, options) {
|
|
634
772
|
return mutationCache.findAll(options.filters).map(
|
|
@@ -637,30 +775,54 @@ function getResult(mutationCache, options) {
|
|
|
637
775
|
}
|
|
638
776
|
function useMutationState(options = () => ({}), queryClient) {
|
|
639
777
|
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
() => mutationCache().subscribe(() => {
|
|
647
|
-
setResult((prev) => {
|
|
648
|
-
const nextResult = replaceEqualDeep(
|
|
649
|
-
prev,
|
|
650
|
-
getResult(mutationCache(), options())
|
|
651
|
-
);
|
|
652
|
-
return prev === nextResult ? prev : nextResult;
|
|
653
|
-
});
|
|
654
|
-
})
|
|
778
|
+
return createCacheAggregate(
|
|
779
|
+
(onEvent) => client().getMutationCache().subscribe(onEvent),
|
|
780
|
+
// Structural sharing against the previous committed value keeps
|
|
781
|
+
// unchanged mutation states referentially stable across syncs.
|
|
782
|
+
(prev) => replaceEqualDeep(prev, getResult(client().getMutationCache(), options())),
|
|
783
|
+
[]
|
|
655
784
|
);
|
|
656
|
-
|
|
657
|
-
|
|
785
|
+
}
|
|
786
|
+
function useQueries(queriesOptions, queryClient) {
|
|
787
|
+
const length = createMemo(() => queriesOptions().queries.length);
|
|
788
|
+
const results = repeat(length, (index) => {
|
|
789
|
+
let lastOptions;
|
|
790
|
+
return useBaseQuery(
|
|
791
|
+
() => {
|
|
792
|
+
const next = queriesOptions().queries[index];
|
|
793
|
+
if (next !== void 0) lastOptions = next;
|
|
794
|
+
return lastOptions;
|
|
795
|
+
},
|
|
796
|
+
QueryObserver,
|
|
797
|
+
queryClient
|
|
798
|
+
);
|
|
799
|
+
});
|
|
800
|
+
const combined = createMemo(() => {
|
|
801
|
+
const combine = queriesOptions().combine;
|
|
802
|
+
const list = results();
|
|
803
|
+
return combine ? combine(list) : list;
|
|
804
|
+
});
|
|
805
|
+
return new Proxy([], {
|
|
806
|
+
get(_, prop) {
|
|
807
|
+
if (prop === $TRACK) return combined();
|
|
808
|
+
return Reflect.get(combined(), prop);
|
|
809
|
+
},
|
|
810
|
+
has(_, prop) {
|
|
811
|
+
if (prop === $TRACK) return true;
|
|
812
|
+
return Reflect.has(combined(), prop);
|
|
813
|
+
},
|
|
814
|
+
ownKeys() {
|
|
815
|
+
return Reflect.ownKeys(combined());
|
|
816
|
+
},
|
|
817
|
+
getOwnPropertyDescriptor(_, prop) {
|
|
818
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(
|
|
819
|
+
combined(),
|
|
820
|
+
prop
|
|
821
|
+
);
|
|
822
|
+
if (descriptor) descriptor.configurable = true;
|
|
823
|
+
return descriptor;
|
|
824
|
+
}
|
|
825
|
+
});
|
|
658
826
|
}
|
|
659
827
|
|
|
660
|
-
|
|
661
|
-
var createQuery = useQuery;
|
|
662
|
-
var createInfiniteQuery = useInfiniteQuery;
|
|
663
|
-
var createMutation = useMutation;
|
|
664
|
-
var createQueries = useQueries;
|
|
665
|
-
|
|
666
|
-
export { IsRestoringContext, 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 };
|
|
828
|
+
export { IsRestoringContext, QueryClient, QueryClientContext, QueryClientProvider, infiniteQueryOptions, mutationOptions, queryOptions, useInfiniteQuery, useIsFetching, useIsMutating, useIsRestoring, useMutation, useMutationState, useQueries, useQuery, useQueryClient };
|