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