@tanstack/svelte-query 5.0.0-beta.20 → 5.0.0-beta.26
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/dist/context.d.ts +5 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +13 -0
- package/dist/createBaseQuery.d.ts.map +1 -1
- package/dist/createBaseQuery.js +12 -4
- package/dist/createQueries.d.ts.map +1 -1
- package/dist/createQueries.js +22 -6
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/useIsRestoring.d.ts +3 -0
- package/dist/useIsRestoring.d.ts.map +1 -0
- package/dist/useIsRestoring.js +4 -0
- package/package.json +2 -2
- package/src/context.ts +16 -0
- package/src/createBaseQuery.ts +27 -9
- package/src/createQueries.ts +46 -14
- package/src/index.ts +1 -0
- package/src/useIsRestoring.ts +6 -0
package/dist/context.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { QueryClient } from '@tanstack/query-core';
|
|
2
|
+
import type { Readable } from 'svelte/store';
|
|
2
3
|
/** Retrieves a Client from Svelte's context */
|
|
3
4
|
export declare const getQueryClientContext: () => QueryClient;
|
|
4
5
|
/** Sets a QueryClient on Svelte's context */
|
|
5
6
|
export declare const setQueryClientContext: (client: QueryClient) => void;
|
|
7
|
+
/** Retrieves a `isRestoring` from Svelte's context */
|
|
8
|
+
export declare const getIsRestoringContext: () => Readable<boolean>;
|
|
9
|
+
/** Sets a `isRestoring` on Svelte's context */
|
|
10
|
+
export declare const setIsRestoringContext: (isRestoring: Readable<boolean>) => void;
|
|
6
11
|
//# sourceMappingURL=context.d.ts.map
|
package/dist/context.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/context.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/context.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAI5C,+CAA+C;AAC/C,eAAO,MAAM,qBAAqB,QAAO,WASxC,CAAA;AAED,6CAA6C;AAC7C,eAAO,MAAM,qBAAqB,WAAY,WAAW,KAAG,IAE3D,CAAA;AAID,sDAAsD;AACtD,eAAO,MAAM,qBAAqB,QAAO,SAAS,OAAO,CAIxD,CAAA;AAED,+CAA+C;AAC/C,eAAO,MAAM,qBAAqB,gBAAiB,SAAS,OAAO,CAAC,KAAG,IAEtE,CAAA"}
|
package/dist/context.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getContext, setContext } from 'svelte';
|
|
2
|
+
import { readable } from 'svelte/store';
|
|
2
3
|
const _contextKey = '$$_queryClient';
|
|
3
4
|
/** Retrieves a Client from Svelte's context */
|
|
4
5
|
export const getQueryClientContext = () => {
|
|
@@ -12,3 +13,15 @@ export const getQueryClientContext = () => {
|
|
|
12
13
|
export const setQueryClientContext = (client) => {
|
|
13
14
|
setContext(_contextKey, client);
|
|
14
15
|
};
|
|
16
|
+
const _isRestoringContextKey = '$$_isRestoring';
|
|
17
|
+
/** Retrieves a `isRestoring` from Svelte's context */
|
|
18
|
+
export const getIsRestoringContext = () => {
|
|
19
|
+
const isRestoring = getContext(_isRestoringContextKey);
|
|
20
|
+
if (!isRestoring)
|
|
21
|
+
return readable(false);
|
|
22
|
+
return isRestoring;
|
|
23
|
+
};
|
|
24
|
+
/** Sets a `isRestoring` on Svelte's context */
|
|
25
|
+
export const setIsRestoringContext = (isRestoring) => {
|
|
26
|
+
setContext(_isRestoringContextKey, isRestoring);
|
|
27
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createBaseQuery.d.ts","sourceRoot":"","sources":["../../src/createBaseQuery.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createBaseQuery.d.ts","sourceRoot":"","sources":["../../src/createBaseQuery.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,WAAW,EACX,QAAQ,EACR,aAAa,EAEd,MAAM,sBAAsB,CAAA;AAC7B,OAAO,KAAK,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAA;AAE5E,wBAAgB,eAAe,CAC7B,YAAY,EACZ,MAAM,EACN,KAAK,EACL,UAAU,EACV,SAAS,SAAS,QAAQ,EAE1B,OAAO,EAAE,sBAAsB,CAC7B,YAAY,EACZ,MAAM,EACN,KAAK,EACL,UAAU,EACV,SAAS,CACV,EACD,QAAQ,EAAE,OAAO,aAAa,EAC9B,WAAW,CAAC,EAAE,WAAW,GACxB,qBAAqB,CAAC,KAAK,EAAE,MAAM,CAAC,CAyDtC"}
|
package/dist/createBaseQuery.js
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import { derived, get, readable } from 'svelte/store';
|
|
2
2
|
import { notifyManager } from '@tanstack/query-core';
|
|
3
|
+
import { useIsRestoring } from './useIsRestoring';
|
|
3
4
|
import { useQueryClient } from './useQueryClient';
|
|
4
5
|
import { isSvelteStore } from './utils';
|
|
5
6
|
export function createBaseQuery(options, Observer, queryClient) {
|
|
6
7
|
/** Load query client */
|
|
7
8
|
const client = useQueryClient(queryClient);
|
|
9
|
+
const isRestoring = useIsRestoring();
|
|
8
10
|
/** Converts options to a svelte store if not already a store object */
|
|
9
11
|
const optionsStore = isSvelteStore(options) ? options : readable(options);
|
|
10
12
|
/** Creates a store that has the default options applied */
|
|
11
|
-
const defaultedOptionsStore = derived(optionsStore, ($optionsStore) => {
|
|
13
|
+
const defaultedOptionsStore = derived([optionsStore, isRestoring], ([$optionsStore, $isRestoring]) => {
|
|
12
14
|
const defaultedOptions = client.defaultQueryOptions($optionsStore);
|
|
13
|
-
defaultedOptions._optimisticResults =
|
|
15
|
+
defaultedOptions._optimisticResults = $isRestoring
|
|
16
|
+
? 'isRestoring'
|
|
17
|
+
: 'optimistic';
|
|
14
18
|
return defaultedOptions;
|
|
15
19
|
});
|
|
16
20
|
/** Creates the observer */
|
|
@@ -20,8 +24,12 @@ export function createBaseQuery(options, Observer, queryClient) {
|
|
|
20
24
|
// these changes should already be reflected in the optimistic result.
|
|
21
25
|
observer.setOptions($defaultedOptions, { listeners: false });
|
|
22
26
|
});
|
|
23
|
-
const result =
|
|
24
|
-
|
|
27
|
+
const result = derived(isRestoring, ($isRestoring, set) => {
|
|
28
|
+
const unsubscribe = $isRestoring
|
|
29
|
+
? () => undefined
|
|
30
|
+
: observer.subscribe(notifyManager.batchCalls(set));
|
|
31
|
+
observer.updateResult();
|
|
32
|
+
return unsubscribe;
|
|
25
33
|
});
|
|
26
34
|
/** Subscribe to changes in result and defaultedOptionsStore */
|
|
27
35
|
const { subscribe } = derived([result, defaultedOptionsStore], ([$result, $defaultedOptionsStore]) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createQueries.d.ts","sourceRoot":"","sources":["../../src/createQueries.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createQueries.d.ts","sourceRoot":"","sources":["../../src/createQueries.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAC5C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AACzC,OAAO,KAAK,EACV,YAAY,EAEZ,8BAA8B,EAC9B,WAAW,EACX,aAAa,EACb,QAAQ,EACR,oBAAoB,EACpB,mBAAmB,EACpB,MAAM,sBAAsB,CAAA;AAI7B,KAAK,oCAAoC,CACvC,YAAY,GAAG,OAAO,EACtB,MAAM,GAAG,YAAY,EACrB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,IACnC,IAAI,CACN,oBAAoB,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,SAAS,CAAC,EAC1E,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rC,KAAK,CAAC,mBAAmB,CAAC,GAC1B,CAAC,SAAS,EAAE,GACZ,EAAE,GACF,CAAC,SAAS,CAAC,MAAM,IAAI,CAAC,GACtB,CAAC,GAAG,MAAM,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC,GAC7B,CAAC,SAAS,CAAC,MAAM,IAAI,EAAE,GAAG,MAAM,IAAI,CAAC,GACrC,cAAc,CAAC,CAAC,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,MAAM,EAAE,UAAU,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,CAAC,CAAC,GACvE,CAAC,SAAS,KAAK,CACb,oCAAoC,CAClC,MAAM,YAAY,EAClB,MAAM,MAAM,EACZ,MAAM,KAAK,EACX,GAAG,CACJ,CACF,GAED,KAAK,CACH,mBAAmB,CACjB,OAAO,SAAS,KAAK,GAAG,YAAY,GAAG,KAAK,EAC5C,OAAO,SAAS,MAAM,GAAG,YAAY,GAAG,MAAM,CAC/C,CACF,GAED,KAAK,CAAC,mBAAmB,CAAC,CAAA;AAE9B,wBAAgB,aAAa,CAC3B,CAAC,SAAS,KAAK,CAAC,GAAG,CAAC,EACpB,eAAe,GAAG,cAAc,CAAC,CAAC,CAAC,EAEnC,EACE,OAAO,EACP,GAAG,OAAO,EACX,EAAE;IACD,OAAO,EAAE,UAAU,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAC3C,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,KAAK,eAAe,CAAA;CACzD,EACD,WAAW,CAAC,EAAE,WAAW,GACxB,QAAQ,CAAC,eAAe,CAAC,CAuE3B"}
|
package/dist/createQueries.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { QueriesObserver, notifyManager } from '@tanstack/query-core';
|
|
2
2
|
import { derived, get, readable } from 'svelte/store';
|
|
3
|
+
import { useIsRestoring } from './useIsRestoring';
|
|
3
4
|
import { useQueryClient } from './useQueryClient';
|
|
4
5
|
import { isSvelteStore } from './utils';
|
|
5
6
|
export function createQueries({ queries, ...options }, queryClient) {
|
|
6
7
|
const client = useQueryClient(queryClient);
|
|
7
|
-
|
|
8
|
+
const isRestoring = useIsRestoring();
|
|
8
9
|
const queriesStore = isSvelteStore(queries) ? queries : readable(queries);
|
|
9
|
-
const defaultedQueriesStore = derived(queriesStore, ($queries) => {
|
|
10
|
+
const defaultedQueriesStore = derived([queriesStore, isRestoring], ([$queries, $isRestoring]) => {
|
|
10
11
|
return $queries.map((opts) => {
|
|
11
12
|
const defaultedOptions = client.defaultQueryOptions(opts);
|
|
12
13
|
// Make sure the results are already in fetching state before subscribing or updating options
|
|
13
|
-
defaultedOptions._optimisticResults =
|
|
14
|
+
defaultedOptions._optimisticResults = $isRestoring
|
|
15
|
+
? 'isRestoring'
|
|
16
|
+
: 'optimistic';
|
|
14
17
|
return defaultedOptions;
|
|
15
18
|
});
|
|
16
19
|
});
|
|
@@ -20,9 +23,22 @@ export function createQueries({ queries, ...options }, queryClient) {
|
|
|
20
23
|
// these changes should already be reflected in the optimistic result.
|
|
21
24
|
observer.setQueries($defaultedQueries, options, { listeners: false });
|
|
22
25
|
});
|
|
23
|
-
const [, getCombinedResult] = observer.getOptimisticResult(get(defaultedQueriesStore));
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
+
const [, getCombinedResult, trackResult] = observer.getOptimisticResult(get(defaultedQueriesStore));
|
|
27
|
+
const result = derived(isRestoring, ($isRestoring, set) => {
|
|
28
|
+
const unsubscribe = $isRestoring
|
|
29
|
+
? () => undefined
|
|
30
|
+
: observer.subscribe(notifyManager.batchCalls(set));
|
|
31
|
+
return () => unsubscribe();
|
|
32
|
+
}, getCombinedResult(trackResult()));
|
|
33
|
+
const { subscribe } = derived([result, defaultedQueriesStore], ([$result, $defaultedQueries]) => {
|
|
34
|
+
$result = observer.getOptimisticResult($defaultedQueries)[0];
|
|
35
|
+
const observers = observer.getObservers();
|
|
36
|
+
return $defaultedQueries.map((query, index) => query.notifyOnChangeProps
|
|
37
|
+
? // @ts-expect-error TCombinedResult should be an array
|
|
38
|
+
$result[index]
|
|
39
|
+
: // @ts-expect-error TCombinedResult should be an array
|
|
40
|
+
observers[index].trackResult($result[index]));
|
|
26
41
|
});
|
|
42
|
+
// @ts-expect-error TCombinedResult should be an array
|
|
27
43
|
return { subscribe };
|
|
28
44
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export { createMutation } from './createMutation';
|
|
|
8
8
|
export { useQueryClient } from './useQueryClient';
|
|
9
9
|
export { useIsFetching } from './useIsFetching';
|
|
10
10
|
export { useIsMutating } from './useIsMutating';
|
|
11
|
+
export { useIsRestoring } from './useIsRestoring';
|
|
11
12
|
export { useHydrate } from './useHydrate';
|
|
12
13
|
export { default as HydrationBoundary } from './HydrationBoundary.svelte';
|
|
13
14
|
export { default as QueryClientProvider } from './QueryClientProvider.svelte';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,sBAAsB,CAAA;AAGpC,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,OAAO,IAAI,iBAAiB,EAAE,MAAM,4BAA4B,CAAA;AACzE,OAAO,EAAE,OAAO,IAAI,mBAAmB,EAAE,MAAM,8BAA8B,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,sBAAsB,CAAA;AAGpC,cAAc,SAAS,CAAA;AACvB,cAAc,WAAW,CAAA;AACzB,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAA;AAC3D,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,OAAO,IAAI,iBAAiB,EAAE,MAAM,4BAA4B,CAAA;AACzE,OAAO,EAAE,OAAO,IAAI,mBAAmB,EAAE,MAAM,8BAA8B,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -11,6 +11,7 @@ export { createMutation } from './createMutation';
|
|
|
11
11
|
export { useQueryClient } from './useQueryClient';
|
|
12
12
|
export { useIsFetching } from './useIsFetching';
|
|
13
13
|
export { useIsMutating } from './useIsMutating';
|
|
14
|
+
export { useIsRestoring } from './useIsRestoring';
|
|
14
15
|
export { useHydrate } from './useHydrate';
|
|
15
16
|
export { default as HydrationBoundary } from './HydrationBoundary.svelte';
|
|
16
17
|
export { default as QueryClientProvider } from './QueryClientProvider.svelte';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useIsRestoring.d.ts","sourceRoot":"","sources":["../../src/useIsRestoring.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAE5C,wBAAgB,cAAc,IAAI,QAAQ,CAAC,OAAO,CAAC,CAElD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/svelte-query",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.26",
|
|
4
4
|
"description": "Primitives for managing, caching and syncing asynchronous and remote data in Svelte",
|
|
5
5
|
"author": "Lachlan Collins",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"!src/__tests__"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@tanstack/query-core": "5.0.0-beta.
|
|
37
|
+
"@tanstack/query-core": "5.0.0-beta.23"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@sveltejs/package": "^2.2.0",
|
package/src/context.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { getContext, setContext } from 'svelte'
|
|
2
|
+
import { readable } from 'svelte/store'
|
|
2
3
|
import type { QueryClient } from '@tanstack/query-core'
|
|
4
|
+
import type { Readable } from 'svelte/store'
|
|
3
5
|
|
|
4
6
|
const _contextKey = '$$_queryClient'
|
|
5
7
|
|
|
@@ -19,3 +21,17 @@ export const getQueryClientContext = (): QueryClient => {
|
|
|
19
21
|
export const setQueryClientContext = (client: QueryClient): void => {
|
|
20
22
|
setContext(_contextKey, client)
|
|
21
23
|
}
|
|
24
|
+
|
|
25
|
+
const _isRestoringContextKey = '$$_isRestoring'
|
|
26
|
+
|
|
27
|
+
/** Retrieves a `isRestoring` from Svelte's context */
|
|
28
|
+
export const getIsRestoringContext = (): Readable<boolean> => {
|
|
29
|
+
const isRestoring = getContext(_isRestoringContextKey)
|
|
30
|
+
if (!isRestoring) return readable(false)
|
|
31
|
+
return isRestoring as Readable<boolean>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Sets a `isRestoring` on Svelte's context */
|
|
35
|
+
export const setIsRestoringContext = (isRestoring: Readable<boolean>): void => {
|
|
36
|
+
setContext(_isRestoringContextKey, isRestoring)
|
|
37
|
+
}
|
package/src/createBaseQuery.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { derived, get, readable } from 'svelte/store'
|
|
2
2
|
import { notifyManager } from '@tanstack/query-core'
|
|
3
|
+
import { useIsRestoring } from './useIsRestoring'
|
|
3
4
|
import { useQueryClient } from './useQueryClient'
|
|
4
5
|
import { isSvelteStore } from './utils'
|
|
5
|
-
import type {
|
|
6
|
+
import type {
|
|
7
|
+
QueryClient,
|
|
8
|
+
QueryKey,
|
|
9
|
+
QueryObserver,
|
|
10
|
+
QueryObserverResult,
|
|
11
|
+
} from '@tanstack/query-core'
|
|
6
12
|
import type { CreateBaseQueryOptions, CreateBaseQueryResult } from './types'
|
|
7
13
|
|
|
8
14
|
export function createBaseQuery<
|
|
@@ -24,16 +30,21 @@ export function createBaseQuery<
|
|
|
24
30
|
): CreateBaseQueryResult<TData, TError> {
|
|
25
31
|
/** Load query client */
|
|
26
32
|
const client = useQueryClient(queryClient)
|
|
27
|
-
|
|
33
|
+
const isRestoring = useIsRestoring()
|
|
28
34
|
/** Converts options to a svelte store if not already a store object */
|
|
29
35
|
const optionsStore = isSvelteStore(options) ? options : readable(options)
|
|
30
36
|
|
|
31
37
|
/** Creates a store that has the default options applied */
|
|
32
|
-
const defaultedOptionsStore = derived(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
const defaultedOptionsStore = derived(
|
|
39
|
+
[optionsStore, isRestoring],
|
|
40
|
+
([$optionsStore, $isRestoring]) => {
|
|
41
|
+
const defaultedOptions = client.defaultQueryOptions($optionsStore)
|
|
42
|
+
defaultedOptions._optimisticResults = $isRestoring
|
|
43
|
+
? 'isRestoring'
|
|
44
|
+
: 'optimistic'
|
|
45
|
+
return defaultedOptions
|
|
46
|
+
},
|
|
47
|
+
)
|
|
37
48
|
|
|
38
49
|
/** Creates the observer */
|
|
39
50
|
const observer = new Observer<
|
|
@@ -50,8 +61,15 @@ export function createBaseQuery<
|
|
|
50
61
|
observer.setOptions($defaultedOptions, { listeners: false })
|
|
51
62
|
})
|
|
52
63
|
|
|
53
|
-
const result =
|
|
54
|
-
|
|
64
|
+
const result = derived<
|
|
65
|
+
typeof isRestoring,
|
|
66
|
+
QueryObserverResult<TData, TError>
|
|
67
|
+
>(isRestoring, ($isRestoring, set) => {
|
|
68
|
+
const unsubscribe = $isRestoring
|
|
69
|
+
? () => undefined
|
|
70
|
+
: observer.subscribe(notifyManager.batchCalls(set))
|
|
71
|
+
observer.updateResult()
|
|
72
|
+
return unsubscribe
|
|
55
73
|
})
|
|
56
74
|
|
|
57
75
|
/** Subscribe to changes in result and defaultedOptionsStore */
|
package/src/createQueries.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { QueriesObserver, notifyManager } from '@tanstack/query-core'
|
|
2
2
|
import { derived, get, readable } from 'svelte/store'
|
|
3
|
+
import { useIsRestoring } from './useIsRestoring'
|
|
3
4
|
import { useQueryClient } from './useQueryClient'
|
|
4
5
|
import { isSvelteStore } from './utils'
|
|
5
6
|
import type { Readable } from 'svelte/store'
|
|
@@ -183,19 +184,23 @@ export function createQueries<
|
|
|
183
184
|
queryClient?: QueryClient,
|
|
184
185
|
): Readable<TCombinedResult> {
|
|
185
186
|
const client = useQueryClient(queryClient)
|
|
186
|
-
|
|
187
|
+
const isRestoring = useIsRestoring()
|
|
187
188
|
|
|
188
189
|
const queriesStore = isSvelteStore(queries) ? queries : readable(queries)
|
|
189
190
|
|
|
190
|
-
const defaultedQueriesStore = derived(
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
191
|
+
const defaultedQueriesStore = derived(
|
|
192
|
+
[queriesStore, isRestoring],
|
|
193
|
+
([$queries, $isRestoring]) => {
|
|
194
|
+
return $queries.map((opts) => {
|
|
195
|
+
const defaultedOptions = client.defaultQueryOptions(opts)
|
|
196
|
+
// Make sure the results are already in fetching state before subscribing or updating options
|
|
197
|
+
defaultedOptions._optimisticResults = $isRestoring
|
|
198
|
+
? 'isRestoring'
|
|
199
|
+
: 'optimistic'
|
|
200
|
+
return defaultedOptions
|
|
201
|
+
})
|
|
202
|
+
},
|
|
203
|
+
)
|
|
199
204
|
const observer = new QueriesObserver<TCombinedResult>(
|
|
200
205
|
client,
|
|
201
206
|
get(defaultedQueriesStore),
|
|
@@ -212,13 +217,40 @@ export function createQueries<
|
|
|
212
217
|
)
|
|
213
218
|
})
|
|
214
219
|
|
|
215
|
-
const [, getCombinedResult] = observer.getOptimisticResult(
|
|
220
|
+
const [, getCombinedResult, trackResult] = observer.getOptimisticResult(
|
|
216
221
|
get(defaultedQueriesStore),
|
|
217
222
|
)
|
|
218
223
|
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
224
|
+
const result = derived<
|
|
225
|
+
typeof isRestoring,
|
|
226
|
+
| Parameters<Parameters<typeof observer.subscribe>[0]>[0]
|
|
227
|
+
| ReturnType<typeof getCombinedResult>
|
|
228
|
+
>(
|
|
229
|
+
isRestoring,
|
|
230
|
+
($isRestoring, set) => {
|
|
231
|
+
const unsubscribe = $isRestoring
|
|
232
|
+
? () => undefined
|
|
233
|
+
: observer.subscribe(notifyManager.batchCalls(set))
|
|
222
234
|
|
|
235
|
+
return () => unsubscribe()
|
|
236
|
+
},
|
|
237
|
+
getCombinedResult(trackResult()),
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
const { subscribe } = derived(
|
|
241
|
+
[result, defaultedQueriesStore],
|
|
242
|
+
([$result, $defaultedQueries]) => {
|
|
243
|
+
$result = observer.getOptimisticResult($defaultedQueries)[0]
|
|
244
|
+
const observers = observer.getObservers()
|
|
245
|
+
return $defaultedQueries.map((query, index) =>
|
|
246
|
+
query.notifyOnChangeProps
|
|
247
|
+
? // @ts-expect-error TCombinedResult should be an array
|
|
248
|
+
$result[index]
|
|
249
|
+
: // @ts-expect-error TCombinedResult should be an array
|
|
250
|
+
observers[index]!.trackResult($result[index]),
|
|
251
|
+
)
|
|
252
|
+
},
|
|
253
|
+
)
|
|
254
|
+
// @ts-expect-error TCombinedResult should be an array
|
|
223
255
|
return { subscribe }
|
|
224
256
|
}
|
package/src/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ export { createMutation } from './createMutation'
|
|
|
13
13
|
export { useQueryClient } from './useQueryClient'
|
|
14
14
|
export { useIsFetching } from './useIsFetching'
|
|
15
15
|
export { useIsMutating } from './useIsMutating'
|
|
16
|
+
export { useIsRestoring } from './useIsRestoring'
|
|
16
17
|
export { useHydrate } from './useHydrate'
|
|
17
18
|
export { default as HydrationBoundary } from './HydrationBoundary.svelte'
|
|
18
19
|
export { default as QueryClientProvider } from './QueryClientProvider.svelte'
|