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