@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/index.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,251 +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
|
-
}
|
|
223
|
-
if (store.data !== void 0 && data !== void 0) {
|
|
224
|
-
reconcile(data, key)(store.data);
|
|
225
|
-
return { ...result, data: store.data };
|
|
226
|
-
}
|
|
227
|
-
return { ...result, data };
|
|
228
|
-
}
|
|
229
|
-
var hydratableObserverResult = (_query, result) => {
|
|
230
|
-
if (!isServer2) return result;
|
|
231
|
-
const obj = {
|
|
232
|
-
...snapshot(result),
|
|
233
|
-
// During SSR, functions cannot be serialized, so we need to remove them
|
|
234
|
-
// This is safe because we will add these functions back when the query is hydrated
|
|
235
|
-
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
|
|
236
81
|
};
|
|
237
|
-
|
|
238
|
-
obj.fetchNextPage = void 0;
|
|
239
|
-
obj.fetchPreviousPage = void 0;
|
|
240
|
-
}
|
|
241
|
-
return obj;
|
|
242
|
-
};
|
|
82
|
+
}
|
|
243
83
|
function useBaseQuery(options, Observer, queryClient) {
|
|
244
|
-
|
|
84
|
+
return useBaseQueryLayer(options, Observer, queryClient).result;
|
|
85
|
+
}
|
|
86
|
+
function useBaseQueryLayer(options, Observer, queryClient) {
|
|
87
|
+
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
245
88
|
const isRestoring = useIsRestoring();
|
|
246
|
-
let
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
89
|
+
let latestHash;
|
|
90
|
+
let latestOptions;
|
|
91
|
+
const defaultedOptions = createMemo(() => {
|
|
92
|
+
const defaulted = client().defaultQueryOptions(options());
|
|
93
|
+
defaulted._optimisticResults = isRestoring() ? "isRestoring" : "optimistic";
|
|
251
94
|
if (isServer2) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
defaultOptions.experimental_prefetchInRender = true;
|
|
95
|
+
defaulted.retry = false;
|
|
96
|
+
defaulted.throwOnError = true;
|
|
255
97
|
}
|
|
256
|
-
|
|
98
|
+
latestHash = defaulted.queryHash;
|
|
99
|
+
latestOptions = defaulted;
|
|
100
|
+
return defaulted;
|
|
257
101
|
});
|
|
258
|
-
|
|
259
|
-
const [trackedDefaultedOptions] = createSignal(() => defaultedOptions());
|
|
102
|
+
let observer = untrack(() => new Observer(client(), defaultedOptions()));
|
|
260
103
|
createRenderEffect(
|
|
261
|
-
() =>
|
|
262
|
-
(opts) =>
|
|
263
|
-
runWithOwner(null, () => observer.setOptions(opts));
|
|
264
|
-
}
|
|
265
|
-
);
|
|
266
|
-
let observerResult = untrack(
|
|
267
|
-
() => observer.getOptimisticResult(defaultedOptions())
|
|
104
|
+
() => defaultedOptions(),
|
|
105
|
+
(opts) => observer.setOptions(opts)
|
|
268
106
|
);
|
|
269
|
-
const
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
if (result.data !== void 0 && unwrappedResult.isError) {
|
|
278
|
-
reject(unwrappedResult.error);
|
|
279
|
-
unsubscribeIfQueued();
|
|
280
|
-
} else {
|
|
281
|
-
resolve(unwrappedResult);
|
|
282
|
-
unsubscribeIfQueued();
|
|
283
|
-
}
|
|
284
|
-
})();
|
|
285
|
-
});
|
|
286
|
-
};
|
|
287
|
-
const unsubscribeIfQueued = () => {
|
|
288
|
-
if (unsubscribeQueued) {
|
|
289
|
-
unsubscribe?.();
|
|
290
|
-
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);
|
|
291
115
|
}
|
|
292
116
|
};
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
const previousResult = observerResult;
|
|
296
|
-
observerResult = result;
|
|
297
|
-
runWithOwner(null, () => {
|
|
298
|
-
setStateWithReconciliation(result);
|
|
299
|
-
if (unsubscribe && !disposed && (previousResult.isLoading !== result.isLoading || previousResult.isError !== result.isError)) {
|
|
300
|
-
try {
|
|
301
|
-
refresh(queryResource);
|
|
302
|
-
} catch {
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
});
|
|
306
|
-
});
|
|
307
|
-
};
|
|
308
|
-
function setStateWithReconciliation(res) {
|
|
309
|
-
const opts = observer.options;
|
|
310
|
-
const reconcileOptions = opts.reconcile;
|
|
311
|
-
const sanitized = _stripFnsForSSR(res);
|
|
312
|
-
setState((store) => {
|
|
313
|
-
return reconcileFn(
|
|
314
|
-
store,
|
|
315
|
-
sanitized,
|
|
316
|
-
reconcileOptions === void 0 ? false : reconcileOptions,
|
|
317
|
-
opts.queryHash
|
|
318
|
-
);
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
let unsubscribe = null;
|
|
117
|
+
let observerSub = null;
|
|
118
|
+
let cacheSub = null;
|
|
322
119
|
let disposed = false;
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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);
|
|
327
126
|
}
|
|
328
127
|
};
|
|
329
|
-
const
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
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);
|
|
335
138
|
}
|
|
336
139
|
};
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
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
|
+
);
|
|
353
175
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
setStateWithReconciliation(currentResult);
|
|
377
|
-
});
|
|
378
|
-
if (currentResult.isError && !currentResult.isFetching && !restoring && shouldThrowError(opts.throwOnError, [
|
|
379
|
-
currentResult.error,
|
|
380
|
-
observer.getCurrentQuery()
|
|
381
|
-
])) {
|
|
382
|
-
return;
|
|
383
|
-
}
|
|
384
|
-
if (!currentResult.isLoading) {
|
|
385
|
-
resolver = null;
|
|
386
|
-
return resolve(
|
|
387
|
-
hydratableObserverResult(observer.getCurrentQuery(), currentResult)
|
|
388
|
-
);
|
|
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);
|
|
389
198
|
}
|
|
390
|
-
})
|
|
391
|
-
|
|
392
|
-
scheduleHydratedAttach();
|
|
199
|
+
} else if (hydratedMount) {
|
|
200
|
+
primeAndAttach();
|
|
393
201
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
202
|
+
if (!hydratedMount) {
|
|
203
|
+
createRenderEffect(
|
|
204
|
+
() => isRestoring(),
|
|
205
|
+
(restoring) => {
|
|
206
|
+
if (!restoring) attach();
|
|
207
|
+
}
|
|
208
|
+
);
|
|
401
209
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
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) };
|
|
405
238
|
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
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);
|
|
409
252
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
"failureCount",
|
|
415
|
-
"failureReason",
|
|
416
|
-
"errorUpdateCount",
|
|
417
|
-
"errorUpdatedAt"
|
|
418
|
-
]);
|
|
419
|
-
return new Proxy(state, {
|
|
420
|
-
get(target, prop, receiver) {
|
|
421
|
-
if (typeof prop === "symbol") {
|
|
422
|
-
return Reflect.get(target, prop, receiver);
|
|
423
|
-
}
|
|
424
|
-
if (isServer2) {
|
|
425
|
-
const resolved = queryResource();
|
|
426
|
-
if (prop in resolved) {
|
|
427
|
-
return Reflect.get(resolved, prop);
|
|
428
|
-
}
|
|
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);
|
|
429
257
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
if (state.isError && !state.isFetching && shouldThrowError(observer.options.throwOnError, [
|
|
258
|
+
}
|
|
259
|
+
if (state.status === "error") {
|
|
260
|
+
if (state.data === void 0 || shouldThrowError(opts.throwOnError, [
|
|
434
261
|
state.error,
|
|
435
|
-
|
|
262
|
+
q
|
|
436
263
|
])) {
|
|
437
264
|
throw state.error;
|
|
438
265
|
}
|
|
439
|
-
return Reflect.get(target, prop, receiver);
|
|
440
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
|
+
};
|
|
441
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
|
+
};
|
|
442
419
|
}
|
|
443
420
|
|
|
444
421
|
// src/useQuery.ts
|
|
@@ -449,157 +426,334 @@ function useQuery(options, queryClient) {
|
|
|
449
426
|
queryClient
|
|
450
427
|
);
|
|
451
428
|
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
queryClient
|
|
457
|
-
);
|
|
429
|
+
|
|
430
|
+
// src/queryOptions.ts
|
|
431
|
+
function queryOptions(options) {
|
|
432
|
+
return options;
|
|
458
433
|
}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
const
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
)
|
|
466
|
-
);
|
|
467
|
-
createRenderEffect(
|
|
468
|
-
() => options(),
|
|
469
|
-
(opts) => {
|
|
470
|
-
observer.setOptions(opts);
|
|
471
|
-
}
|
|
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 }
|
|
472
440
|
);
|
|
473
|
-
const
|
|
474
|
-
|
|
475
|
-
};
|
|
476
|
-
const initialResult = untrack(() => observer.getCurrentResult());
|
|
477
|
-
const [state, setState] = createStore({
|
|
478
|
-
...initialResult,
|
|
479
|
-
mutate,
|
|
480
|
-
mutateAsync: initialResult.mutate
|
|
481
|
-
});
|
|
482
|
-
const unsubscribe = observer.subscribe((result) => {
|
|
483
|
-
runWithOwner(null, () => {
|
|
484
|
-
setState(() => ({
|
|
485
|
-
...result,
|
|
486
|
-
mutate,
|
|
487
|
-
mutateAsync: result.mutate
|
|
488
|
-
}));
|
|
489
|
-
});
|
|
441
|
+
const [value, setValue] = createOptimistic(() => durable(), {
|
|
442
|
+
ownedWrite: true
|
|
490
443
|
});
|
|
491
|
-
|
|
492
|
-
|
|
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,
|
|
493
456
|
() => {
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
throw error;
|
|
457
|
+
if (hydratedMount) {
|
|
458
|
+
attach();
|
|
459
|
+
sync();
|
|
498
460
|
}
|
|
499
|
-
},
|
|
500
|
-
() => {
|
|
501
461
|
}
|
|
502
462
|
);
|
|
503
|
-
return
|
|
463
|
+
if (isServer3) return () => empty;
|
|
464
|
+
if (!hydratedMount) attach();
|
|
465
|
+
return value;
|
|
504
466
|
}
|
|
505
|
-
|
|
467
|
+
|
|
468
|
+
// src/useIsFetching.ts
|
|
469
|
+
function useIsFetching(filters, queryClient) {
|
|
506
470
|
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
() =>
|
|
510
|
-
|
|
511
|
-
get _optimisticResults() {
|
|
512
|
-
return isRestoring() ? "isRestoring" : "optimistic";
|
|
513
|
-
}
|
|
514
|
-
})
|
|
515
|
-
)
|
|
516
|
-
);
|
|
517
|
-
const observer = untrack(
|
|
518
|
-
() => new QueriesObserver(
|
|
519
|
-
client(),
|
|
520
|
-
defaultedQueries(),
|
|
521
|
-
queriesOptions().combine ? {
|
|
522
|
-
combine: queriesOptions().combine
|
|
523
|
-
} : void 0
|
|
524
|
-
)
|
|
525
|
-
);
|
|
526
|
-
const [, getCombinedResult] = untrack(
|
|
527
|
-
() => observer.getOptimisticResult(
|
|
528
|
-
defaultedQueries(),
|
|
529
|
-
queriesOptions().combine
|
|
530
|
-
)
|
|
471
|
+
return createCacheAggregate(
|
|
472
|
+
(onEvent) => client().getQueryCache().subscribe(onEvent),
|
|
473
|
+
() => client().isFetching(filters?.()),
|
|
474
|
+
0
|
|
531
475
|
);
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
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
|
|
535
509
|
);
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
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
|
|
552
563
|
}
|
|
553
|
-
},
|
|
554
|
-
() => {
|
|
555
564
|
}
|
|
556
565
|
);
|
|
557
|
-
|
|
558
|
-
unsubscribe();
|
|
559
|
-
});
|
|
560
|
-
createMemo(() => {
|
|
561
|
-
const queries = defaultedQueries();
|
|
562
|
-
runWithOwner(null, () => {
|
|
563
|
-
observer.setQueries(
|
|
564
|
-
queries,
|
|
565
|
-
queriesOptions().combine ? {
|
|
566
|
-
combine: queriesOptions().combine
|
|
567
|
-
} : void 0
|
|
568
|
-
);
|
|
569
|
-
});
|
|
570
|
-
return queries;
|
|
571
|
-
});
|
|
572
|
-
return state;
|
|
566
|
+
return result;
|
|
573
567
|
}
|
|
574
|
-
var QueryClient = class extends QueryClient$1 {
|
|
575
|
-
constructor(config = {}) {
|
|
576
|
-
super(config);
|
|
577
|
-
}
|
|
578
|
-
};
|
|
579
568
|
|
|
580
|
-
// src/
|
|
581
|
-
function
|
|
569
|
+
// src/infiniteQueryOptions.ts
|
|
570
|
+
function infiniteQueryOptions(options) {
|
|
582
571
|
return options;
|
|
583
572
|
}
|
|
584
|
-
|
|
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) {
|
|
585
590
|
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
586
|
-
const
|
|
587
|
-
const [
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
)
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
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
|
+
}
|
|
595
710
|
);
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
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;
|
|
603
757
|
}
|
|
604
758
|
|
|
605
759
|
// src/mutationOptions.ts
|
|
@@ -608,18 +762,11 @@ function mutationOptions(options) {
|
|
|
608
762
|
}
|
|
609
763
|
function useIsMutating(filters, queryClient) {
|
|
610
764
|
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
);
|
|
616
|
-
const unsubscribe = untrack(
|
|
617
|
-
() => mutationCache().subscribe((_result) => {
|
|
618
|
-
setMutations(client().isMutating(filters?.()));
|
|
619
|
-
})
|
|
765
|
+
return createCacheAggregate(
|
|
766
|
+
(onEvent) => client().getMutationCache().subscribe(onEvent),
|
|
767
|
+
() => client().isMutating(filters?.()),
|
|
768
|
+
0
|
|
620
769
|
);
|
|
621
|
-
onCleanup(unsubscribe);
|
|
622
|
-
return mutations;
|
|
623
770
|
}
|
|
624
771
|
function getResult(mutationCache, options) {
|
|
625
772
|
return mutationCache.findAll(options.filters).map(
|
|
@@ -628,30 +775,54 @@ function getResult(mutationCache, options) {
|
|
|
628
775
|
}
|
|
629
776
|
function useMutationState(options = () => ({}), queryClient) {
|
|
630
777
|
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
() => mutationCache().subscribe(() => {
|
|
638
|
-
setResult((prev) => {
|
|
639
|
-
const nextResult = replaceEqualDeep(
|
|
640
|
-
prev,
|
|
641
|
-
getResult(mutationCache(), options())
|
|
642
|
-
);
|
|
643
|
-
return prev === nextResult ? prev : nextResult;
|
|
644
|
-
});
|
|
645
|
-
})
|
|
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
|
+
[]
|
|
646
784
|
);
|
|
647
|
-
|
|
648
|
-
|
|
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
|
+
});
|
|
649
826
|
}
|
|
650
827
|
|
|
651
|
-
|
|
652
|
-
var createQuery = useQuery;
|
|
653
|
-
var createInfiniteQuery = useInfiniteQuery;
|
|
654
|
-
var createMutation = useMutation;
|
|
655
|
-
var createQueries = useQueries;
|
|
656
|
-
|
|
657
|
-
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 };
|