@tanstack/svelte-query 5.0.0-alpha.31 → 5.0.0-alpha.34
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.
|
@@ -45,8 +45,8 @@ export declare type QueriesOptions<T extends any[], Result extends any[] = [], D
|
|
|
45
45
|
* QueriesResults reducer recursively maps type param to results
|
|
46
46
|
*/
|
|
47
47
|
export declare type QueriesResults<T extends any[], Result extends any[] = [], Depth extends ReadonlyArray<number> = []> = Depth['length'] extends MAXIMUM_DEPTH ? QueryObserverResult[] : T extends [] ? [] : T extends [infer Head] ? [...Result, GetResults<Head>] : T extends [infer Head, ...infer Tail] ? QueriesResults<[...Tail], [...Result, GetResults<Head>], [...Depth, 1]> : T extends CreateQueryOptionsForCreateQueries<infer TQueryFnData, infer TError, infer TData, any>[] ? QueryObserverResult<unknown extends TData ? TQueryFnData : TData, unknown extends TError ? DefaultError : TError>[] : QueryObserverResult[];
|
|
48
|
-
export declare
|
|
49
|
-
export declare function createQueries<T extends any[]>({ queries, }: {
|
|
48
|
+
export declare function createQueries<T extends any[], TCombinedResult = QueriesResults<T>>({ queries, ...options }: {
|
|
50
49
|
queries: WritableOrVal<[...QueriesOptions<T>]>;
|
|
51
|
-
|
|
50
|
+
combine?: (result: QueriesResults<T>) => TCombinedResult;
|
|
51
|
+
}, queryClient?: QueryClient): Readable<TCombinedResult>;
|
|
52
52
|
export {};
|
|
@@ -2,25 +2,26 @@ import { notifyManager, QueriesObserver } from '@tanstack/query-core';
|
|
|
2
2
|
import { derived, get, readable, writable } from 'svelte/store';
|
|
3
3
|
import { useQueryClient } from './useQueryClient';
|
|
4
4
|
import { isWritable } from './utils';
|
|
5
|
-
export function createQueries({ queries, }, queryClient) {
|
|
5
|
+
export function createQueries({ queries, ...options }, queryClient) {
|
|
6
6
|
const client = useQueryClient(queryClient);
|
|
7
7
|
// const isRestoring = useIsRestoring()
|
|
8
8
|
const queriesStore = isWritable(queries) ? queries : writable(queries);
|
|
9
9
|
const defaultedQueriesStore = derived(queriesStore, ($queries) => {
|
|
10
|
-
return $queries.map((
|
|
11
|
-
const defaultedOptions = client.defaultQueryOptions(
|
|
10
|
+
return $queries.map((opts) => {
|
|
11
|
+
const defaultedOptions = client.defaultQueryOptions(opts);
|
|
12
12
|
// Make sure the results are already in fetching state before subscribing or updating options
|
|
13
13
|
defaultedOptions._optimisticResults = 'optimistic';
|
|
14
14
|
return defaultedOptions;
|
|
15
15
|
});
|
|
16
16
|
});
|
|
17
|
-
const observer = new QueriesObserver(client, get(defaultedQueriesStore));
|
|
17
|
+
const observer = new QueriesObserver(client, get(defaultedQueriesStore), options);
|
|
18
18
|
defaultedQueriesStore.subscribe(($defaultedQueries) => {
|
|
19
19
|
// Do not notify on updates because of changes in the options because
|
|
20
20
|
// these changes should already be reflected in the optimistic result.
|
|
21
|
-
observer.setQueries($defaultedQueries, { listeners: false });
|
|
21
|
+
observer.setQueries($defaultedQueries, options, { listeners: false });
|
|
22
22
|
});
|
|
23
|
-
const
|
|
23
|
+
const [, getCombinedResult] = observer.getOptimisticResult(get(defaultedQueriesStore));
|
|
24
|
+
const { subscribe } = readable(getCombinedResult(), (set) => {
|
|
24
25
|
return observer.subscribe(notifyManager.batchCalls(set));
|
|
25
26
|
});
|
|
26
27
|
return { subscribe };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/svelte-query",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.34",
|
|
4
4
|
"description": "Primitives for managing, caching and syncing asynchronous and remote data in Svelte",
|
|
5
5
|
"author": "Lachlan Collins",
|
|
6
6
|
"license": "MIT",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"vite": "^4.0.0"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@tanstack/query-core": "5.0.0-alpha.
|
|
45
|
+
"@tanstack/query-core": "5.0.0-alpha.34"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"svelte": "^3.54.0"
|
package/src/createQueries.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
QueriesPlaceholderDataFunction,
|
|
6
6
|
QueryObserverResult,
|
|
7
7
|
DefaultError,
|
|
8
|
+
QueriesObserverOptions,
|
|
8
9
|
} from '@tanstack/query-core'
|
|
9
10
|
|
|
10
11
|
import { notifyManager, QueriesObserver } from '@tanstack/query-core'
|
|
@@ -150,44 +151,56 @@ export type QueriesResults<
|
|
|
150
151
|
: // Fallback
|
|
151
152
|
QueryObserverResult[]
|
|
152
153
|
|
|
153
|
-
export
|
|
154
|
-
|
|
155
|
-
|
|
154
|
+
export function createQueries<
|
|
155
|
+
T extends any[],
|
|
156
|
+
TCombinedResult = QueriesResults<T>,
|
|
157
|
+
>(
|
|
156
158
|
{
|
|
157
159
|
queries,
|
|
160
|
+
...options
|
|
158
161
|
}: {
|
|
159
162
|
queries: WritableOrVal<[...QueriesOptions<T>]>
|
|
163
|
+
combine?: (result: QueriesResults<T>) => TCombinedResult
|
|
160
164
|
},
|
|
161
165
|
queryClient?: QueryClient,
|
|
162
|
-
):
|
|
166
|
+
): Readable<TCombinedResult> {
|
|
163
167
|
const client = useQueryClient(queryClient)
|
|
164
168
|
// const isRestoring = useIsRestoring()
|
|
165
169
|
|
|
166
170
|
const queriesStore = isWritable(queries) ? queries : writable(queries)
|
|
167
171
|
|
|
168
172
|
const defaultedQueriesStore = derived(queriesStore, ($queries) => {
|
|
169
|
-
return $queries.map((
|
|
170
|
-
const defaultedOptions = client.defaultQueryOptions(
|
|
173
|
+
return $queries.map((opts) => {
|
|
174
|
+
const defaultedOptions = client.defaultQueryOptions(opts)
|
|
171
175
|
// Make sure the results are already in fetching state before subscribing or updating options
|
|
172
176
|
defaultedOptions._optimisticResults = 'optimistic'
|
|
173
177
|
|
|
174
178
|
return defaultedOptions
|
|
175
179
|
})
|
|
176
180
|
})
|
|
177
|
-
const observer = new QueriesObserver(
|
|
181
|
+
const observer = new QueriesObserver<TCombinedResult>(
|
|
182
|
+
client,
|
|
183
|
+
get(defaultedQueriesStore),
|
|
184
|
+
options as QueriesObserverOptions<TCombinedResult>,
|
|
185
|
+
)
|
|
178
186
|
|
|
179
187
|
defaultedQueriesStore.subscribe(($defaultedQueries) => {
|
|
180
188
|
// Do not notify on updates because of changes in the options because
|
|
181
189
|
// these changes should already be reflected in the optimistic result.
|
|
182
|
-
observer.setQueries(
|
|
190
|
+
observer.setQueries(
|
|
191
|
+
$defaultedQueries,
|
|
192
|
+
options as QueriesObserverOptions<TCombinedResult>,
|
|
193
|
+
{ listeners: false },
|
|
194
|
+
)
|
|
183
195
|
})
|
|
184
196
|
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
(set) => {
|
|
188
|
-
return observer.subscribe(notifyManager.batchCalls(set))
|
|
189
|
-
},
|
|
197
|
+
const [, getCombinedResult] = observer.getOptimisticResult(
|
|
198
|
+
get(defaultedQueriesStore),
|
|
190
199
|
)
|
|
191
200
|
|
|
201
|
+
const { subscribe } = readable(getCombinedResult() as any, (set) => {
|
|
202
|
+
return observer.subscribe(notifyManager.batchCalls(set))
|
|
203
|
+
})
|
|
204
|
+
|
|
192
205
|
return { subscribe }
|
|
193
206
|
}
|