@tanstack/solid-query 5.0.0-alpha.86 → 5.0.0-alpha.88
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/build/dev.cjs +354 -0
- package/build/dev.js +341 -0
- package/build/index.cjs +351 -0
- package/build/index.d.cts +142 -0
- package/build/index.d.ts +142 -0
- package/build/index.js +338 -0
- package/package.json +25 -19
- package/dist/cjs/index.cjs +0 -359
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/esm/index.js +0 -342
- package/dist/esm/index.js.map +0 -1
- package/dist/source/QueryClient.js +0 -6
- package/dist/source/QueryClientProvider.jsx +0 -21
- package/dist/source/createBaseQuery.js +0 -177
- package/dist/source/createInfiniteQuery.js +0 -8
- package/dist/source/createMutation.js +0 -38
- package/dist/source/createQueries.js +0 -38
- package/dist/source/createQuery.js +0 -9
- package/dist/source/index.js +0 -15
- package/dist/source/setBatchUpdatesFn.js +0 -3
- package/dist/source/types.js +0 -2
- package/dist/source/useIsFetching.js +0 -12
- package/dist/source/useIsMutating.js +0 -12
- package/dist/source/utils.js +0 -7
- package/dist/types/QueryClient.d.ts +0 -29
- package/dist/types/QueryClientProvider.d.ts +0 -9
- package/dist/types/createBaseQuery.d.ts +0 -5
- package/dist/types/createInfiniteQuery.d.ts +0 -5
- package/dist/types/createMutation.d.ts +0 -5
- package/dist/types/createQueries.d.ts +0 -53
- package/dist/types/createQuery.d.ts +0 -14
- package/dist/types/index.d.ts +0 -13
- package/dist/types/setBatchUpdatesFn.d.ts +0 -1
- package/dist/types/types.d.ts +0 -34
- package/dist/types/useIsFetching.d.ts +0 -4
- package/dist/types/useIsMutating.d.ts +0 -4
- package/dist/types/utils.d.ts +0 -1
package/build/dev.cjs
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var queryCore = require('@tanstack/query-core');
|
|
4
|
+
var solidJs = require('solid-js');
|
|
5
|
+
var web = require('solid-js/web');
|
|
6
|
+
var store = require('solid-js/store');
|
|
7
|
+
|
|
8
|
+
// src/setBatchUpdatesFn.ts
|
|
9
|
+
queryCore.notifyManager.setBatchNotifyFunction(solidJs.batch);
|
|
10
|
+
exports.QueryClient = class QueryClient extends queryCore.QueryClient {
|
|
11
|
+
constructor(config = {}) {
|
|
12
|
+
super(config);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
exports.QueryClientContext = solidJs.createContext(void 0);
|
|
16
|
+
exports.useQueryClient = (queryClient) => {
|
|
17
|
+
if (queryClient) {
|
|
18
|
+
return queryClient;
|
|
19
|
+
}
|
|
20
|
+
const client = solidJs.useContext(exports.QueryClientContext);
|
|
21
|
+
if (!client) {
|
|
22
|
+
throw new Error("No QueryClient set, use QueryClientProvider to set one");
|
|
23
|
+
}
|
|
24
|
+
return client;
|
|
25
|
+
};
|
|
26
|
+
exports.QueryClientProvider = (props) => {
|
|
27
|
+
solidJs.onMount(() => {
|
|
28
|
+
props.client.mount();
|
|
29
|
+
});
|
|
30
|
+
solidJs.onCleanup(() => props.client.unmount());
|
|
31
|
+
return web.createComponent(exports.QueryClientContext.Provider, {
|
|
32
|
+
get value() {
|
|
33
|
+
return props.client;
|
|
34
|
+
},
|
|
35
|
+
get children() {
|
|
36
|
+
return props.children;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/utils.ts
|
|
42
|
+
function shouldThrowError(throwError, params) {
|
|
43
|
+
if (typeof throwError === "function") {
|
|
44
|
+
return throwError(...params);
|
|
45
|
+
}
|
|
46
|
+
return !!throwError;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/createBaseQuery.ts
|
|
50
|
+
function reconcileFn(store$1, result, reconcileOption) {
|
|
51
|
+
if (reconcileOption === false)
|
|
52
|
+
return result;
|
|
53
|
+
if (typeof reconcileOption === "function") {
|
|
54
|
+
const newData2 = reconcileOption(store$1.data, result.data);
|
|
55
|
+
return { ...result, data: newData2 };
|
|
56
|
+
}
|
|
57
|
+
const newData = store.reconcile(result.data, { key: reconcileOption })(store$1.data);
|
|
58
|
+
return { ...result, data: newData };
|
|
59
|
+
}
|
|
60
|
+
function createBaseQuery(options, Observer, queryClient) {
|
|
61
|
+
const client = solidJs.createMemo(() => exports.useQueryClient(queryClient?.()));
|
|
62
|
+
const defaultedOptions = client().defaultQueryOptions(options());
|
|
63
|
+
defaultedOptions._optimisticResults = "optimistic";
|
|
64
|
+
defaultedOptions.structuralSharing = false;
|
|
65
|
+
if (web.isServer) {
|
|
66
|
+
defaultedOptions.retry = false;
|
|
67
|
+
defaultedOptions.throwOnError = true;
|
|
68
|
+
}
|
|
69
|
+
const observer = new Observer(client(), defaultedOptions);
|
|
70
|
+
const [state, setState] = store.createStore(
|
|
71
|
+
observer.getOptimisticResult(defaultedOptions)
|
|
72
|
+
);
|
|
73
|
+
const createServerSubscriber = (resolve, reject) => {
|
|
74
|
+
return observer.subscribe((result) => {
|
|
75
|
+
queryCore.notifyManager.batchCalls(() => {
|
|
76
|
+
const query = observer.getCurrentQuery();
|
|
77
|
+
const { refetch: refetch2, ...rest } = store.unwrap(result);
|
|
78
|
+
const unwrappedResult = {
|
|
79
|
+
...rest,
|
|
80
|
+
// hydrate() expects a QueryState object, which is similar but not
|
|
81
|
+
// quite the same as a QueryObserverResult object. Thus, for now, we're
|
|
82
|
+
// copying over the missing properties from state in order to support hydration
|
|
83
|
+
dataUpdateCount: query.state.dataUpdateCount,
|
|
84
|
+
fetchFailureCount: query.state.fetchFailureCount,
|
|
85
|
+
// Removing these properties since they might not be serializable
|
|
86
|
+
// fetchFailureReason: query.state.fetchFailureReason,
|
|
87
|
+
// fetchMeta: query.state.fetchMeta,
|
|
88
|
+
isInvalidated: query.state.isInvalidated
|
|
89
|
+
};
|
|
90
|
+
if (unwrappedResult.isError) {
|
|
91
|
+
{
|
|
92
|
+
console.error(unwrappedResult.error);
|
|
93
|
+
}
|
|
94
|
+
reject(unwrappedResult.error);
|
|
95
|
+
}
|
|
96
|
+
if (unwrappedResult.isSuccess) {
|
|
97
|
+
resolve(unwrappedResult);
|
|
98
|
+
}
|
|
99
|
+
})();
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
const createClientSubscriber = () => {
|
|
103
|
+
return observer.subscribe((result) => {
|
|
104
|
+
queryCore.notifyManager.batchCalls(() => {
|
|
105
|
+
const reconcileOptions = observer.options.reconcile;
|
|
106
|
+
if (queryResource()?.data && result.data && !queryResource.loading) {
|
|
107
|
+
setState((store) => {
|
|
108
|
+
return reconcileFn(
|
|
109
|
+
store,
|
|
110
|
+
result,
|
|
111
|
+
reconcileOptions === void 0 ? "id" : reconcileOptions
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
mutate(state);
|
|
115
|
+
} else {
|
|
116
|
+
setState((store) => {
|
|
117
|
+
return reconcileFn(
|
|
118
|
+
store,
|
|
119
|
+
result,
|
|
120
|
+
reconcileOptions === void 0 ? "id" : reconcileOptions
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
refetch();
|
|
124
|
+
}
|
|
125
|
+
})();
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
let unsubscribe = null;
|
|
129
|
+
const [queryResource, { refetch, mutate }] = solidJs.createResource(
|
|
130
|
+
() => {
|
|
131
|
+
return new Promise((resolve, reject) => {
|
|
132
|
+
if (web.isServer) {
|
|
133
|
+
unsubscribe = createServerSubscriber(resolve, reject);
|
|
134
|
+
} else {
|
|
135
|
+
if (!unsubscribe) {
|
|
136
|
+
unsubscribe = createClientSubscriber();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (!state.isLoading) {
|
|
140
|
+
resolve(state);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
initialValue: state,
|
|
146
|
+
// If initialData is provided, we resolve the resource immediately
|
|
147
|
+
ssrLoadFrom: options().initialData ? "initial" : "server",
|
|
148
|
+
get deferStream() {
|
|
149
|
+
return options().deferStream;
|
|
150
|
+
},
|
|
151
|
+
/**
|
|
152
|
+
* If this resource was populated on the server (either sync render, or streamed in over time), onHydrated
|
|
153
|
+
* will be called. This is the point at which we can hydrate the query cache state, and setup the query subscriber.
|
|
154
|
+
*
|
|
155
|
+
* Leveraging onHydrated allows us to plug into the async and streaming support that solidjs resources already support.
|
|
156
|
+
*
|
|
157
|
+
* Note that this is only invoked on the client, for queries that were originally run on the server.
|
|
158
|
+
*/
|
|
159
|
+
onHydrated(_k, info) {
|
|
160
|
+
if (info.value) {
|
|
161
|
+
queryCore.hydrate(client(), {
|
|
162
|
+
queries: [
|
|
163
|
+
{
|
|
164
|
+
queryKey: defaultedOptions.queryKey,
|
|
165
|
+
queryHash: defaultedOptions.queryHash,
|
|
166
|
+
state: info.value
|
|
167
|
+
}
|
|
168
|
+
]
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
if (!unsubscribe) {
|
|
172
|
+
const newOptions = { ...defaultedOptions };
|
|
173
|
+
if (defaultedOptions.staleTime || !defaultedOptions.initialData) {
|
|
174
|
+
newOptions.refetchOnMount = false;
|
|
175
|
+
}
|
|
176
|
+
observer.setOptions(newOptions);
|
|
177
|
+
setState(observer.getOptimisticResult(newOptions));
|
|
178
|
+
unsubscribe = createClientSubscriber();
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
);
|
|
183
|
+
solidJs.onCleanup(() => {
|
|
184
|
+
if (unsubscribe) {
|
|
185
|
+
unsubscribe();
|
|
186
|
+
unsubscribe = null;
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
solidJs.createComputed(
|
|
190
|
+
solidJs.on(
|
|
191
|
+
() => client().defaultQueryOptions(options()),
|
|
192
|
+
() => observer.setOptions(client().defaultQueryOptions(options())),
|
|
193
|
+
{
|
|
194
|
+
// Defer because we don't need to trigger on first render
|
|
195
|
+
// This only cares about changes to options after the observer is created
|
|
196
|
+
defer: true
|
|
197
|
+
}
|
|
198
|
+
)
|
|
199
|
+
);
|
|
200
|
+
solidJs.createComputed(
|
|
201
|
+
solidJs.on(
|
|
202
|
+
() => state.status,
|
|
203
|
+
() => {
|
|
204
|
+
if (state.isError && !state.isFetching && shouldThrowError(observer.options.throwOnError, [
|
|
205
|
+
state.error,
|
|
206
|
+
observer.getCurrentQuery()
|
|
207
|
+
])) {
|
|
208
|
+
throw state.error;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
)
|
|
212
|
+
);
|
|
213
|
+
const handler = {
|
|
214
|
+
get(target, prop) {
|
|
215
|
+
const val = queryResource()?.[prop];
|
|
216
|
+
return val !== void 0 ? val : Reflect.get(target, prop);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
return new Proxy(state, handler);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// src/createQuery.ts
|
|
223
|
+
function queryOptions(options) {
|
|
224
|
+
return options;
|
|
225
|
+
}
|
|
226
|
+
function createQuery(options, queryClient) {
|
|
227
|
+
return createBaseQuery(
|
|
228
|
+
solidJs.createMemo(() => options()),
|
|
229
|
+
queryCore.QueryObserver,
|
|
230
|
+
queryClient
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
function useIsFetching(filters, queryClient) {
|
|
234
|
+
const client = solidJs.createMemo(() => exports.useQueryClient(queryClient?.()));
|
|
235
|
+
const queryCache = solidJs.createMemo(() => client().getQueryCache());
|
|
236
|
+
const [fetches, setFetches] = solidJs.createSignal(client().isFetching(filters?.()));
|
|
237
|
+
const unsubscribe = queryCache().subscribe(() => {
|
|
238
|
+
setFetches(client().isFetching(filters?.()));
|
|
239
|
+
});
|
|
240
|
+
solidJs.onCleanup(unsubscribe);
|
|
241
|
+
return fetches;
|
|
242
|
+
}
|
|
243
|
+
function createInfiniteQuery(options, queryClient) {
|
|
244
|
+
return createBaseQuery(
|
|
245
|
+
solidJs.createMemo(() => options()),
|
|
246
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
247
|
+
queryCore.InfiniteQueryObserver,
|
|
248
|
+
queryClient
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
function createMutation(options, queryClient) {
|
|
252
|
+
const client = exports.useQueryClient(queryClient?.());
|
|
253
|
+
const observer = new queryCore.MutationObserver(
|
|
254
|
+
client,
|
|
255
|
+
options()
|
|
256
|
+
);
|
|
257
|
+
const mutate = (variables, mutateOptions) => {
|
|
258
|
+
observer.mutate(variables, mutateOptions).catch(noop);
|
|
259
|
+
};
|
|
260
|
+
const [state, setState] = store.createStore({
|
|
261
|
+
...observer.getCurrentResult(),
|
|
262
|
+
mutate,
|
|
263
|
+
mutateAsync: observer.getCurrentResult().mutate
|
|
264
|
+
});
|
|
265
|
+
solidJs.createComputed(() => {
|
|
266
|
+
observer.setOptions(options());
|
|
267
|
+
});
|
|
268
|
+
solidJs.createComputed(
|
|
269
|
+
solidJs.on(
|
|
270
|
+
() => state.status,
|
|
271
|
+
() => {
|
|
272
|
+
if (state.isError && shouldThrowError(observer.options.throwOnError, [state.error])) {
|
|
273
|
+
throw state.error;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
)
|
|
277
|
+
);
|
|
278
|
+
const unsubscribe = observer.subscribe((result) => {
|
|
279
|
+
setState({
|
|
280
|
+
...result,
|
|
281
|
+
mutate,
|
|
282
|
+
mutateAsync: result.mutate
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
solidJs.onCleanup(unsubscribe);
|
|
286
|
+
return state;
|
|
287
|
+
}
|
|
288
|
+
function noop() {
|
|
289
|
+
}
|
|
290
|
+
function useIsMutating(filters, queryClient) {
|
|
291
|
+
const client = solidJs.createMemo(() => exports.useQueryClient(queryClient?.()));
|
|
292
|
+
const mutationCache = solidJs.createMemo(() => client().getMutationCache());
|
|
293
|
+
const [mutations, setMutations] = solidJs.createSignal(
|
|
294
|
+
client().isMutating(filters?.())
|
|
295
|
+
);
|
|
296
|
+
const unsubscribe = mutationCache().subscribe((_result) => {
|
|
297
|
+
setMutations(client().isMutating(filters?.()));
|
|
298
|
+
});
|
|
299
|
+
solidJs.onCleanup(unsubscribe);
|
|
300
|
+
return mutations;
|
|
301
|
+
}
|
|
302
|
+
function createQueries(queriesOptions, queryClient) {
|
|
303
|
+
const client = exports.useQueryClient(queryClient?.());
|
|
304
|
+
const defaultedQueries = queriesOptions().queries.map((options) => {
|
|
305
|
+
const defaultedOptions = client.defaultQueryOptions(options);
|
|
306
|
+
defaultedOptions._optimisticResults = "optimistic";
|
|
307
|
+
return defaultedOptions;
|
|
308
|
+
});
|
|
309
|
+
const observer = new queryCore.QueriesObserver(
|
|
310
|
+
client,
|
|
311
|
+
defaultedQueries,
|
|
312
|
+
queriesOptions().combine ? {
|
|
313
|
+
combine: queriesOptions().combine
|
|
314
|
+
} : void 0
|
|
315
|
+
);
|
|
316
|
+
const [state, setState] = store.createStore(
|
|
317
|
+
observer.getOptimisticResult(defaultedQueries)[1]()
|
|
318
|
+
);
|
|
319
|
+
const unsubscribe = observer.subscribe((result) => {
|
|
320
|
+
queryCore.notifyManager.batchCalls(() => {
|
|
321
|
+
setState(store.unwrap(result));
|
|
322
|
+
})();
|
|
323
|
+
});
|
|
324
|
+
solidJs.onCleanup(unsubscribe);
|
|
325
|
+
solidJs.createComputed(() => {
|
|
326
|
+
const updatedQueries = queriesOptions().queries.map((options) => {
|
|
327
|
+
const defaultedOptions = client.defaultQueryOptions(options);
|
|
328
|
+
defaultedOptions._optimisticResults = "optimistic";
|
|
329
|
+
return defaultedOptions;
|
|
330
|
+
});
|
|
331
|
+
observer.setQueries(
|
|
332
|
+
updatedQueries,
|
|
333
|
+
queriesOptions().combine ? {
|
|
334
|
+
combine: queriesOptions().combine
|
|
335
|
+
} : void 0,
|
|
336
|
+
{ listeners: false }
|
|
337
|
+
);
|
|
338
|
+
});
|
|
339
|
+
return state;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
exports.createInfiniteQuery = createInfiniteQuery;
|
|
343
|
+
exports.createMutation = createMutation;
|
|
344
|
+
exports.createQueries = createQueries;
|
|
345
|
+
exports.createQuery = createQuery;
|
|
346
|
+
exports.queryOptions = queryOptions;
|
|
347
|
+
exports.useIsFetching = useIsFetching;
|
|
348
|
+
exports.useIsMutating = useIsMutating;
|
|
349
|
+
Object.keys(queryCore).forEach(function (k) {
|
|
350
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
351
|
+
enumerable: true,
|
|
352
|
+
get: function () { return queryCore[k]; }
|
|
353
|
+
});
|
|
354
|
+
});
|
package/build/dev.js
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { notifyManager, QueryClient as QueryClient$1, MutationObserver, QueriesObserver, hydrate, QueryObserver, InfiniteQueryObserver } from '@tanstack/query-core';
|
|
2
|
+
export * from '@tanstack/query-core';
|
|
3
|
+
import { batch, createContext, useContext, onMount, onCleanup, createMemo, createSignal, createComputed, on, createResource } from 'solid-js';
|
|
4
|
+
import { createComponent, isServer } from 'solid-js/web';
|
|
5
|
+
import { createStore, unwrap, reconcile } from 'solid-js/store';
|
|
6
|
+
|
|
7
|
+
// src/setBatchUpdatesFn.ts
|
|
8
|
+
notifyManager.setBatchNotifyFunction(batch);
|
|
9
|
+
var QueryClient = class extends QueryClient$1 {
|
|
10
|
+
constructor(config = {}) {
|
|
11
|
+
super(config);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var QueryClientContext = createContext(void 0);
|
|
15
|
+
var useQueryClient = (queryClient) => {
|
|
16
|
+
if (queryClient) {
|
|
17
|
+
return queryClient;
|
|
18
|
+
}
|
|
19
|
+
const client = useContext(QueryClientContext);
|
|
20
|
+
if (!client) {
|
|
21
|
+
throw new Error("No QueryClient set, use QueryClientProvider to set one");
|
|
22
|
+
}
|
|
23
|
+
return client;
|
|
24
|
+
};
|
|
25
|
+
var QueryClientProvider = (props) => {
|
|
26
|
+
onMount(() => {
|
|
27
|
+
props.client.mount();
|
|
28
|
+
});
|
|
29
|
+
onCleanup(() => props.client.unmount());
|
|
30
|
+
return createComponent(QueryClientContext.Provider, {
|
|
31
|
+
get value() {
|
|
32
|
+
return props.client;
|
|
33
|
+
},
|
|
34
|
+
get children() {
|
|
35
|
+
return props.children;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/utils.ts
|
|
41
|
+
function shouldThrowError(throwError, params) {
|
|
42
|
+
if (typeof throwError === "function") {
|
|
43
|
+
return throwError(...params);
|
|
44
|
+
}
|
|
45
|
+
return !!throwError;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/createBaseQuery.ts
|
|
49
|
+
function reconcileFn(store, result, reconcileOption) {
|
|
50
|
+
if (reconcileOption === false)
|
|
51
|
+
return result;
|
|
52
|
+
if (typeof reconcileOption === "function") {
|
|
53
|
+
const newData2 = reconcileOption(store.data, result.data);
|
|
54
|
+
return { ...result, data: newData2 };
|
|
55
|
+
}
|
|
56
|
+
const newData = reconcile(result.data, { key: reconcileOption })(store.data);
|
|
57
|
+
return { ...result, data: newData };
|
|
58
|
+
}
|
|
59
|
+
function createBaseQuery(options, Observer, queryClient) {
|
|
60
|
+
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
61
|
+
const defaultedOptions = client().defaultQueryOptions(options());
|
|
62
|
+
defaultedOptions._optimisticResults = "optimistic";
|
|
63
|
+
defaultedOptions.structuralSharing = false;
|
|
64
|
+
if (isServer) {
|
|
65
|
+
defaultedOptions.retry = false;
|
|
66
|
+
defaultedOptions.throwOnError = true;
|
|
67
|
+
}
|
|
68
|
+
const observer = new Observer(client(), defaultedOptions);
|
|
69
|
+
const [state, setState] = createStore(
|
|
70
|
+
observer.getOptimisticResult(defaultedOptions)
|
|
71
|
+
);
|
|
72
|
+
const createServerSubscriber = (resolve, reject) => {
|
|
73
|
+
return observer.subscribe((result) => {
|
|
74
|
+
notifyManager.batchCalls(() => {
|
|
75
|
+
const query = observer.getCurrentQuery();
|
|
76
|
+
const { refetch: refetch2, ...rest } = unwrap(result);
|
|
77
|
+
const unwrappedResult = {
|
|
78
|
+
...rest,
|
|
79
|
+
// hydrate() expects a QueryState object, which is similar but not
|
|
80
|
+
// quite the same as a QueryObserverResult object. Thus, for now, we're
|
|
81
|
+
// copying over the missing properties from state in order to support hydration
|
|
82
|
+
dataUpdateCount: query.state.dataUpdateCount,
|
|
83
|
+
fetchFailureCount: query.state.fetchFailureCount,
|
|
84
|
+
// Removing these properties since they might not be serializable
|
|
85
|
+
// fetchFailureReason: query.state.fetchFailureReason,
|
|
86
|
+
// fetchMeta: query.state.fetchMeta,
|
|
87
|
+
isInvalidated: query.state.isInvalidated
|
|
88
|
+
};
|
|
89
|
+
if (unwrappedResult.isError) {
|
|
90
|
+
{
|
|
91
|
+
console.error(unwrappedResult.error);
|
|
92
|
+
}
|
|
93
|
+
reject(unwrappedResult.error);
|
|
94
|
+
}
|
|
95
|
+
if (unwrappedResult.isSuccess) {
|
|
96
|
+
resolve(unwrappedResult);
|
|
97
|
+
}
|
|
98
|
+
})();
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
const createClientSubscriber = () => {
|
|
102
|
+
return observer.subscribe((result) => {
|
|
103
|
+
notifyManager.batchCalls(() => {
|
|
104
|
+
const reconcileOptions = observer.options.reconcile;
|
|
105
|
+
if (queryResource()?.data && result.data && !queryResource.loading) {
|
|
106
|
+
setState((store) => {
|
|
107
|
+
return reconcileFn(
|
|
108
|
+
store,
|
|
109
|
+
result,
|
|
110
|
+
reconcileOptions === void 0 ? "id" : reconcileOptions
|
|
111
|
+
);
|
|
112
|
+
});
|
|
113
|
+
mutate(state);
|
|
114
|
+
} else {
|
|
115
|
+
setState((store) => {
|
|
116
|
+
return reconcileFn(
|
|
117
|
+
store,
|
|
118
|
+
result,
|
|
119
|
+
reconcileOptions === void 0 ? "id" : reconcileOptions
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
refetch();
|
|
123
|
+
}
|
|
124
|
+
})();
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
let unsubscribe = null;
|
|
128
|
+
const [queryResource, { refetch, mutate }] = createResource(
|
|
129
|
+
() => {
|
|
130
|
+
return new Promise((resolve, reject) => {
|
|
131
|
+
if (isServer) {
|
|
132
|
+
unsubscribe = createServerSubscriber(resolve, reject);
|
|
133
|
+
} else {
|
|
134
|
+
if (!unsubscribe) {
|
|
135
|
+
unsubscribe = createClientSubscriber();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (!state.isLoading) {
|
|
139
|
+
resolve(state);
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
initialValue: state,
|
|
145
|
+
// If initialData is provided, we resolve the resource immediately
|
|
146
|
+
ssrLoadFrom: options().initialData ? "initial" : "server",
|
|
147
|
+
get deferStream() {
|
|
148
|
+
return options().deferStream;
|
|
149
|
+
},
|
|
150
|
+
/**
|
|
151
|
+
* If this resource was populated on the server (either sync render, or streamed in over time), onHydrated
|
|
152
|
+
* will be called. This is the point at which we can hydrate the query cache state, and setup the query subscriber.
|
|
153
|
+
*
|
|
154
|
+
* Leveraging onHydrated allows us to plug into the async and streaming support that solidjs resources already support.
|
|
155
|
+
*
|
|
156
|
+
* Note that this is only invoked on the client, for queries that were originally run on the server.
|
|
157
|
+
*/
|
|
158
|
+
onHydrated(_k, info) {
|
|
159
|
+
if (info.value) {
|
|
160
|
+
hydrate(client(), {
|
|
161
|
+
queries: [
|
|
162
|
+
{
|
|
163
|
+
queryKey: defaultedOptions.queryKey,
|
|
164
|
+
queryHash: defaultedOptions.queryHash,
|
|
165
|
+
state: info.value
|
|
166
|
+
}
|
|
167
|
+
]
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
if (!unsubscribe) {
|
|
171
|
+
const newOptions = { ...defaultedOptions };
|
|
172
|
+
if (defaultedOptions.staleTime || !defaultedOptions.initialData) {
|
|
173
|
+
newOptions.refetchOnMount = false;
|
|
174
|
+
}
|
|
175
|
+
observer.setOptions(newOptions);
|
|
176
|
+
setState(observer.getOptimisticResult(newOptions));
|
|
177
|
+
unsubscribe = createClientSubscriber();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
);
|
|
182
|
+
onCleanup(() => {
|
|
183
|
+
if (unsubscribe) {
|
|
184
|
+
unsubscribe();
|
|
185
|
+
unsubscribe = null;
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
createComputed(
|
|
189
|
+
on(
|
|
190
|
+
() => client().defaultQueryOptions(options()),
|
|
191
|
+
() => observer.setOptions(client().defaultQueryOptions(options())),
|
|
192
|
+
{
|
|
193
|
+
// Defer because we don't need to trigger on first render
|
|
194
|
+
// This only cares about changes to options after the observer is created
|
|
195
|
+
defer: true
|
|
196
|
+
}
|
|
197
|
+
)
|
|
198
|
+
);
|
|
199
|
+
createComputed(
|
|
200
|
+
on(
|
|
201
|
+
() => state.status,
|
|
202
|
+
() => {
|
|
203
|
+
if (state.isError && !state.isFetching && shouldThrowError(observer.options.throwOnError, [
|
|
204
|
+
state.error,
|
|
205
|
+
observer.getCurrentQuery()
|
|
206
|
+
])) {
|
|
207
|
+
throw state.error;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
)
|
|
211
|
+
);
|
|
212
|
+
const handler = {
|
|
213
|
+
get(target, prop) {
|
|
214
|
+
const val = queryResource()?.[prop];
|
|
215
|
+
return val !== void 0 ? val : Reflect.get(target, prop);
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
return new Proxy(state, handler);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// src/createQuery.ts
|
|
222
|
+
function queryOptions(options) {
|
|
223
|
+
return options;
|
|
224
|
+
}
|
|
225
|
+
function createQuery(options, queryClient) {
|
|
226
|
+
return createBaseQuery(
|
|
227
|
+
createMemo(() => options()),
|
|
228
|
+
QueryObserver,
|
|
229
|
+
queryClient
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
function useIsFetching(filters, queryClient) {
|
|
233
|
+
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
234
|
+
const queryCache = createMemo(() => client().getQueryCache());
|
|
235
|
+
const [fetches, setFetches] = createSignal(client().isFetching(filters?.()));
|
|
236
|
+
const unsubscribe = queryCache().subscribe(() => {
|
|
237
|
+
setFetches(client().isFetching(filters?.()));
|
|
238
|
+
});
|
|
239
|
+
onCleanup(unsubscribe);
|
|
240
|
+
return fetches;
|
|
241
|
+
}
|
|
242
|
+
function createInfiniteQuery(options, queryClient) {
|
|
243
|
+
return createBaseQuery(
|
|
244
|
+
createMemo(() => options()),
|
|
245
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
246
|
+
InfiniteQueryObserver,
|
|
247
|
+
queryClient
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
function createMutation(options, queryClient) {
|
|
251
|
+
const client = useQueryClient(queryClient?.());
|
|
252
|
+
const observer = new MutationObserver(
|
|
253
|
+
client,
|
|
254
|
+
options()
|
|
255
|
+
);
|
|
256
|
+
const mutate = (variables, mutateOptions) => {
|
|
257
|
+
observer.mutate(variables, mutateOptions).catch(noop);
|
|
258
|
+
};
|
|
259
|
+
const [state, setState] = createStore({
|
|
260
|
+
...observer.getCurrentResult(),
|
|
261
|
+
mutate,
|
|
262
|
+
mutateAsync: observer.getCurrentResult().mutate
|
|
263
|
+
});
|
|
264
|
+
createComputed(() => {
|
|
265
|
+
observer.setOptions(options());
|
|
266
|
+
});
|
|
267
|
+
createComputed(
|
|
268
|
+
on(
|
|
269
|
+
() => state.status,
|
|
270
|
+
() => {
|
|
271
|
+
if (state.isError && shouldThrowError(observer.options.throwOnError, [state.error])) {
|
|
272
|
+
throw state.error;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
)
|
|
276
|
+
);
|
|
277
|
+
const unsubscribe = observer.subscribe((result) => {
|
|
278
|
+
setState({
|
|
279
|
+
...result,
|
|
280
|
+
mutate,
|
|
281
|
+
mutateAsync: result.mutate
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
onCleanup(unsubscribe);
|
|
285
|
+
return state;
|
|
286
|
+
}
|
|
287
|
+
function noop() {
|
|
288
|
+
}
|
|
289
|
+
function useIsMutating(filters, queryClient) {
|
|
290
|
+
const client = createMemo(() => useQueryClient(queryClient?.()));
|
|
291
|
+
const mutationCache = createMemo(() => client().getMutationCache());
|
|
292
|
+
const [mutations, setMutations] = createSignal(
|
|
293
|
+
client().isMutating(filters?.())
|
|
294
|
+
);
|
|
295
|
+
const unsubscribe = mutationCache().subscribe((_result) => {
|
|
296
|
+
setMutations(client().isMutating(filters?.()));
|
|
297
|
+
});
|
|
298
|
+
onCleanup(unsubscribe);
|
|
299
|
+
return mutations;
|
|
300
|
+
}
|
|
301
|
+
function createQueries(queriesOptions, queryClient) {
|
|
302
|
+
const client = useQueryClient(queryClient?.());
|
|
303
|
+
const defaultedQueries = queriesOptions().queries.map((options) => {
|
|
304
|
+
const defaultedOptions = client.defaultQueryOptions(options);
|
|
305
|
+
defaultedOptions._optimisticResults = "optimistic";
|
|
306
|
+
return defaultedOptions;
|
|
307
|
+
});
|
|
308
|
+
const observer = new QueriesObserver(
|
|
309
|
+
client,
|
|
310
|
+
defaultedQueries,
|
|
311
|
+
queriesOptions().combine ? {
|
|
312
|
+
combine: queriesOptions().combine
|
|
313
|
+
} : void 0
|
|
314
|
+
);
|
|
315
|
+
const [state, setState] = createStore(
|
|
316
|
+
observer.getOptimisticResult(defaultedQueries)[1]()
|
|
317
|
+
);
|
|
318
|
+
const unsubscribe = observer.subscribe((result) => {
|
|
319
|
+
notifyManager.batchCalls(() => {
|
|
320
|
+
setState(unwrap(result));
|
|
321
|
+
})();
|
|
322
|
+
});
|
|
323
|
+
onCleanup(unsubscribe);
|
|
324
|
+
createComputed(() => {
|
|
325
|
+
const updatedQueries = queriesOptions().queries.map((options) => {
|
|
326
|
+
const defaultedOptions = client.defaultQueryOptions(options);
|
|
327
|
+
defaultedOptions._optimisticResults = "optimistic";
|
|
328
|
+
return defaultedOptions;
|
|
329
|
+
});
|
|
330
|
+
observer.setQueries(
|
|
331
|
+
updatedQueries,
|
|
332
|
+
queriesOptions().combine ? {
|
|
333
|
+
combine: queriesOptions().combine
|
|
334
|
+
} : void 0,
|
|
335
|
+
{ listeners: false }
|
|
336
|
+
);
|
|
337
|
+
});
|
|
338
|
+
return state;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export { QueryClient, QueryClientContext, QueryClientProvider, createInfiniteQuery, createMutation, createQueries, createQuery, queryOptions, useIsFetching, useIsMutating, useQueryClient };
|