@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/index.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,251 +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
|
-
}
|
|
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
|
|
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
|
|
236
95
|
};
|
|
237
|
-
|
|
238
|
-
obj.fetchNextPage = void 0;
|
|
239
|
-
obj.fetchPreviousPage = void 0;
|
|
240
|
-
}
|
|
241
|
-
return obj;
|
|
242
|
-
};
|
|
96
|
+
}
|
|
243
97
|
function useBaseQuery(options, Observer, queryClient) {
|
|
244
|
-
|
|
98
|
+
return useBaseQueryLayer(options, Observer, queryClient).result;
|
|
99
|
+
}
|
|
100
|
+
function useBaseQueryLayer(options, Observer, queryClient) {
|
|
101
|
+
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
245
102
|
const isRestoring = useIsRestoring();
|
|
246
|
-
let
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
103
|
+
let latestHash;
|
|
104
|
+
let latestOptions;
|
|
105
|
+
const defaultedOptions = createMemo(() => {
|
|
106
|
+
const defaulted = client().defaultQueryOptions(options());
|
|
107
|
+
defaulted._optimisticResults = isRestoring() ? "isRestoring" : "optimistic";
|
|
251
108
|
if (isServer2) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
defaultOptions.experimental_prefetchInRender = true;
|
|
109
|
+
defaulted.retry = false;
|
|
110
|
+
defaulted.throwOnError = true;
|
|
255
111
|
}
|
|
256
|
-
|
|
112
|
+
latestHash = defaulted.queryHash;
|
|
113
|
+
latestOptions = defaulted;
|
|
114
|
+
return defaulted;
|
|
257
115
|
});
|
|
258
|
-
|
|
259
|
-
const [trackedDefaultedOptions] = createSignal(() => defaultedOptions());
|
|
116
|
+
let observer = untrack(() => new Observer(client(), defaultedOptions()));
|
|
260
117
|
createRenderEffect(
|
|
261
|
-
() =>
|
|
262
|
-
(opts) =>
|
|
263
|
-
runWithOwner(null, () => observer.setOptions(opts));
|
|
264
|
-
}
|
|
118
|
+
() => defaultedOptions(),
|
|
119
|
+
(opts) => observer.setOptions(opts)
|
|
265
120
|
);
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
)
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
notifyManager.batchCalls(() => {
|
|
275
|
-
const query = observer.getCurrentQuery();
|
|
276
|
-
const unwrappedResult = hydratableObserverResult(query, result);
|
|
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;
|
|
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);
|
|
291
129
|
}
|
|
292
130
|
};
|
|
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;
|
|
131
|
+
let observerSub = null;
|
|
132
|
+
let cacheSub = null;
|
|
322
133
|
let disposed = false;
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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);
|
|
327
140
|
}
|
|
328
141
|
};
|
|
329
|
-
const
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
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);
|
|
335
152
|
}
|
|
336
153
|
};
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
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
|
+
);
|
|
353
189
|
}
|
|
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
|
-
);
|
|
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);
|
|
389
212
|
}
|
|
390
|
-
})
|
|
391
|
-
|
|
392
|
-
scheduleHydratedAttach();
|
|
213
|
+
} else if (hydratedMount) {
|
|
214
|
+
primeAndAttach();
|
|
393
215
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
216
|
+
if (!hydratedMount) {
|
|
217
|
+
createRenderEffect(
|
|
218
|
+
() => isRestoring(),
|
|
219
|
+
(restoring) => {
|
|
220
|
+
if (!restoring) attach();
|
|
221
|
+
}
|
|
222
|
+
);
|
|
401
223
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
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) };
|
|
405
252
|
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
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);
|
|
409
266
|
}
|
|
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
|
-
}
|
|
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);
|
|
429
271
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
if (state.isError && !state.isFetching && shouldThrowError(observer.options.throwOnError, [
|
|
272
|
+
}
|
|
273
|
+
if (state.status === "error") {
|
|
274
|
+
if (state.data === void 0 || shouldThrowError(opts.throwOnError, [
|
|
434
275
|
state.error,
|
|
435
|
-
|
|
276
|
+
q
|
|
436
277
|
])) {
|
|
437
278
|
throw state.error;
|
|
438
279
|
}
|
|
439
|
-
return Reflect.get(target, prop, receiver);
|
|
440
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
|
+
};
|
|
441
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
|
+
};
|
|
442
433
|
}
|
|
443
434
|
|
|
444
435
|
// src/useQuery.ts
|
|
@@ -449,157 +440,356 @@ function useQuery(options, queryClient) {
|
|
|
449
440
|
queryClient
|
|
450
441
|
);
|
|
451
442
|
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
queryClient
|
|
457
|
-
);
|
|
443
|
+
|
|
444
|
+
// src/queryOptions.ts
|
|
445
|
+
function queryOptions(options) {
|
|
446
|
+
return options;
|
|
458
447
|
}
|
|
459
|
-
function
|
|
460
|
-
const
|
|
461
|
-
|
|
462
|
-
() =>
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
);
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
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 }
|
|
472
463
|
);
|
|
473
|
-
const
|
|
474
|
-
|
|
475
|
-
};
|
|
476
|
-
const initialResult = untrack(() => observer.getCurrentResult());
|
|
477
|
-
const [state, setState] = createStore({
|
|
478
|
-
...initialResult,
|
|
479
|
-
mutate,
|
|
480
|
-
mutateAsync: initialResult.mutate
|
|
464
|
+
const [value, setValue] = createOptimistic(() => durable(), {
|
|
465
|
+
ownedWrite: true
|
|
481
466
|
});
|
|
482
|
-
const
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
}
|
|
491
|
-
onCleanup(unsubscribe);
|
|
492
|
-
|
|
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,
|
|
493
479
|
() => {
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
throw error;
|
|
480
|
+
if (hydratedMount) {
|
|
481
|
+
attach();
|
|
482
|
+
sync();
|
|
498
483
|
}
|
|
499
|
-
},
|
|
500
|
-
() => {
|
|
501
484
|
}
|
|
502
485
|
);
|
|
503
|
-
return
|
|
486
|
+
if (isServer3) return () => empty;
|
|
487
|
+
if (!hydratedMount) attach();
|
|
488
|
+
return value;
|
|
504
489
|
}
|
|
505
|
-
|
|
490
|
+
|
|
491
|
+
// src/useIsFetching.ts
|
|
492
|
+
function useIsFetching(filters, queryClient) {
|
|
506
493
|
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
() =>
|
|
510
|
-
|
|
511
|
-
get _optimisticResults() {
|
|
512
|
-
return isRestoring() ? "isRestoring" : "optimistic";
|
|
513
|
-
}
|
|
514
|
-
})
|
|
515
|
-
)
|
|
494
|
+
return createCacheAggregate(
|
|
495
|
+
(onEvent) => client().getQueryCache().subscribe(onEvent),
|
|
496
|
+
() => client().isFetching(filters?.()),
|
|
497
|
+
0
|
|
516
498
|
);
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
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
|
|
535
532
|
);
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
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
|
|
552
586
|
}
|
|
553
|
-
},
|
|
554
|
-
() => {
|
|
555
587
|
}
|
|
556
588
|
);
|
|
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;
|
|
589
|
+
return result;
|
|
573
590
|
}
|
|
574
|
-
var QueryClient = class extends QueryClient$1 {
|
|
575
|
-
constructor(config = {}) {
|
|
576
|
-
super(config);
|
|
577
|
-
}
|
|
578
|
-
};
|
|
579
591
|
|
|
580
|
-
// src/
|
|
581
|
-
function
|
|
592
|
+
// src/infiniteQueryOptions.ts
|
|
593
|
+
function infiniteQueryOptions(options) {
|
|
582
594
|
return options;
|
|
583
595
|
}
|
|
584
|
-
|
|
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) {
|
|
585
613
|
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
586
|
-
const
|
|
587
|
-
const [
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
);
|
|
591
|
-
|
|
592
|
-
(
|
|
593
|
-
|
|
594
|
-
|
|
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
|
+
}
|
|
595
746
|
);
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
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;
|
|
603
793
|
}
|
|
604
794
|
|
|
605
795
|
// src/mutationOptions.ts
|
|
@@ -608,18 +798,11 @@ function mutationOptions(options) {
|
|
|
608
798
|
}
|
|
609
799
|
function useIsMutating(filters, queryClient) {
|
|
610
800
|
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
);
|
|
616
|
-
const unsubscribe = untrack(
|
|
617
|
-
() => mutationCache().subscribe((_result) => {
|
|
618
|
-
setMutations(client().isMutating(filters?.()));
|
|
619
|
-
})
|
|
801
|
+
return createCacheAggregate(
|
|
802
|
+
(onEvent) => client().getMutationCache().subscribe(onEvent),
|
|
803
|
+
() => client().isMutating(filters?.()),
|
|
804
|
+
0
|
|
620
805
|
);
|
|
621
|
-
onCleanup(unsubscribe);
|
|
622
|
-
return mutations;
|
|
623
806
|
}
|
|
624
807
|
function getResult(mutationCache, options) {
|
|
625
808
|
return mutationCache.findAll(options.filters).map(
|
|
@@ -628,30 +811,54 @@ function getResult(mutationCache, options) {
|
|
|
628
811
|
}
|
|
629
812
|
function useMutationState(options = () => ({}), queryClient) {
|
|
630
813
|
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
|
-
})
|
|
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
|
+
[]
|
|
646
820
|
);
|
|
647
|
-
|
|
648
|
-
|
|
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
|
+
});
|
|
649
862
|
}
|
|
650
863
|
|
|
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 };
|
|
864
|
+
export { FLIGHT_DATA_SOURCE, IsRestoringContext, QueryClient, QueryClientContext, QueryClientProvider, dehydrateSettled, infiniteQueryOptions, mutationOptions, queryOptions, useInfiniteQuery, useIsFetching, useIsMutating, useIsRestoring, useMutation, useMutationState, useQueries, useQuery, useQueryClient };
|