@tanstack/svelte-query 6.1.48 → 6.2.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/dist/context.d.ts +27 -2
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +27 -2
- package/dist/createInfiniteQuery.d.ts +97 -1
- package/dist/createInfiniteQuery.d.ts.map +1 -1
- package/dist/createMutation.svelte.d.ts +156 -2
- package/dist/createMutation.svelte.d.ts.map +1 -1
- package/dist/createMutation.svelte.js +156 -2
- package/dist/createQueries.svelte.d.ts +71 -0
- package/dist/createQueries.svelte.d.ts.map +1 -1
- package/dist/createQueries.svelte.js +71 -0
- package/dist/createQuery.d.ts +209 -0
- package/dist/createQuery.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/infiniteQueryOptions.d.ts +95 -2
- package/dist/infiniteQueryOptions.d.ts.map +1 -1
- package/dist/mutationOptions.d.ts +52 -0
- package/dist/mutationOptions.d.ts.map +1 -1
- package/dist/queryOptions.d.ts +73 -0
- package/dist/queryOptions.d.ts.map +1 -1
- package/dist/types.d.ts +3 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/useHydrate.d.ts +28 -0
- package/dist/useHydrate.d.ts.map +1 -1
- package/dist/useHydrate.js +28 -0
- package/dist/useIsFetching.svelte.d.ts +35 -0
- package/dist/useIsFetching.svelte.d.ts.map +1 -1
- package/dist/useIsFetching.svelte.js +35 -0
- package/dist/useIsMutating.svelte.d.ts +23 -0
- package/dist/useIsMutating.svelte.d.ts.map +1 -1
- package/dist/useIsMutating.svelte.js +23 -0
- package/dist/useMutationState.svelte.d.ts +70 -0
- package/dist/useMutationState.svelte.d.ts.map +1 -1
- package/dist/useMutationState.svelte.js +70 -0
- package/package.json +3 -3
- package/src/context.ts +27 -2
- package/src/createInfiniteQuery.ts +143 -2
- package/src/createMutation.svelte.ts +156 -2
- package/src/createQueries.svelte.ts +71 -0
- package/src/createQuery.ts +209 -0
- package/src/index.ts +4 -0
- package/src/infiniteQueryOptions.ts +162 -4
- package/src/mutationOptions.ts +53 -0
- package/src/queryOptions.ts +73 -0
- package/src/types.ts +7 -0
- package/src/useHydrate.ts +28 -0
- package/src/useIsFetching.svelte.ts +35 -0
- package/src/useIsMutating.svelte.ts +23 -0
- package/src/useMutationState.svelte.ts +70 -0
package/dist/useHydrate.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useHydrate.d.ts","sourceRoot":"","sources":["../src/useHydrate.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAEvE,wBAAgB,UAAU,CACxB,KAAK,CAAC,EAAE,OAAO,EACf,OAAO,CAAC,EAAE,cAAc,EACxB,WAAW,CAAC,EAAE,WAAW,QAO1B"}
|
|
1
|
+
{"version":3,"file":"useHydrate.d.ts","sourceRoot":"","sources":["../src/useHydrate.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAEvE;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,UAAU,CACxB,KAAK,CAAC,EAAE,OAAO,EACf,OAAO,CAAC,EAAE,cAAc,EACxB,WAAW,CAAC,EAAE,WAAW,QAO1B"}
|
package/dist/useHydrate.js
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
import { hydrate } from '@tanstack/query-core';
|
|
2
2
|
import { useQueryClient } from './useQueryClient.js';
|
|
3
|
+
/**
|
|
4
|
+
* Adds a previously dehydrated `state` into the `queryClient` (from the nearest context, or the one
|
|
5
|
+
* passed explicitly). If the client already contains data, the new queries will be intelligently merged based
|
|
6
|
+
* on update timestamp. `HydrationBoundary` wraps this — use it directly only if you need to hydrate from your
|
|
7
|
+
* own component instead.
|
|
8
|
+
*
|
|
9
|
+
* @param state - The dehydrated state to hydrate into the cache, as produced by `dehydrate`.
|
|
10
|
+
* @param options - {@link HydrateOptions} to control the hydration.
|
|
11
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
12
|
+
* be used.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* Server-side prefetch handed off to the client via `dehydrate` — `dehydratedState` would typically come
|
|
16
|
+
* from a server load function that prefetched with `queryClient.query` and called `dehydrate(queryClient)`:
|
|
17
|
+
* ```svelte
|
|
18
|
+
* <script lang="ts">
|
|
19
|
+
* import { HydrationBoundary } from '@tanstack/svelte-query'
|
|
20
|
+
* import type { DehydratedState } from '@tanstack/svelte-query'
|
|
21
|
+
* import Posts from './Posts.svelte'
|
|
22
|
+
*
|
|
23
|
+
* let { dehydratedState }: { dehydratedState: DehydratedState } = $props()
|
|
24
|
+
* </script>
|
|
25
|
+
*
|
|
26
|
+
* <HydrationBoundary state={dehydratedState}>
|
|
27
|
+
* <Posts />
|
|
28
|
+
* </HydrationBoundary>
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
3
31
|
export function useHydrate(state, options, queryClient) {
|
|
4
32
|
const client = useQueryClient(queryClient);
|
|
5
33
|
if (state) {
|
|
@@ -1,4 +1,39 @@
|
|
|
1
1
|
import { ReactiveValue } from './containers.svelte.js';
|
|
2
2
|
import type { QueryClient, QueryFilters } from '@tanstack/query-core';
|
|
3
|
+
/**
|
|
4
|
+
* @param filters - {@link QueryFilters} to narrow down which queries to count. Omit to count every fetching
|
|
5
|
+
* query.
|
|
6
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
7
|
+
* be used.
|
|
8
|
+
* @returns A reactive value — read `.current` to get how many matching queries are currently fetching.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```svelte
|
|
12
|
+
* <script lang="ts">
|
|
13
|
+
* import { useIsFetching } from '@tanstack/svelte-query'
|
|
14
|
+
*
|
|
15
|
+
* // How many queries matching the posts prefix are fetching?
|
|
16
|
+
* const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
|
|
17
|
+
* </script>
|
|
18
|
+
*
|
|
19
|
+
* {#if isFetchingPosts.current}
|
|
20
|
+
* <span>Refreshing posts...</span>
|
|
21
|
+
* {/if}
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* A global loading indicator for any query fetching in the background, not just the ones on screen:
|
|
26
|
+
* ```svelte
|
|
27
|
+
* <script lang="ts">
|
|
28
|
+
* import { useIsFetching } from '@tanstack/svelte-query'
|
|
29
|
+
*
|
|
30
|
+
* const isFetching = useIsFetching()
|
|
31
|
+
* </script>
|
|
32
|
+
*
|
|
33
|
+
* {#if isFetching.current}
|
|
34
|
+
* <div>Queries are fetching in the background...</div>
|
|
35
|
+
* {/if}
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
3
38
|
export declare function useIsFetching(filters?: QueryFilters, queryClient?: QueryClient): ReactiveValue<number>;
|
|
4
39
|
//# sourceMappingURL=useIsFetching.svelte.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useIsFetching.svelte.d.ts","sourceRoot":"","sources":["../src/useIsFetching.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAEtD,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAErE,wBAAgB,aAAa,CAC3B,OAAO,CAAC,EAAE,YAAY,EACtB,WAAW,CAAC,EAAE,WAAW,GACxB,aAAa,CAAC,MAAM,CAAC,CAQvB"}
|
|
1
|
+
{"version":3,"file":"useIsFetching.svelte.d.ts","sourceRoot":"","sources":["../src/useIsFetching.svelte.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAEtD,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAErE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,aAAa,CAC3B,OAAO,CAAC,EAAE,YAAY,EACtB,WAAW,CAAC,EAAE,WAAW,GACxB,aAAa,CAAC,MAAM,CAAC,CAQvB"}
|
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
import { ReactiveValue } from './containers.svelte.js';
|
|
2
2
|
import { useQueryClient } from './useQueryClient.js';
|
|
3
|
+
/**
|
|
4
|
+
* @param filters - {@link QueryFilters} to narrow down which queries to count. Omit to count every fetching
|
|
5
|
+
* query.
|
|
6
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
7
|
+
* be used.
|
|
8
|
+
* @returns A reactive value — read `.current` to get how many matching queries are currently fetching.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```svelte
|
|
12
|
+
* <script lang="ts">
|
|
13
|
+
* import { useIsFetching } from '@tanstack/svelte-query'
|
|
14
|
+
*
|
|
15
|
+
* // How many queries matching the posts prefix are fetching?
|
|
16
|
+
* const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
|
|
17
|
+
* </script>
|
|
18
|
+
*
|
|
19
|
+
* {#if isFetchingPosts.current}
|
|
20
|
+
* <span>Refreshing posts...</span>
|
|
21
|
+
* {/if}
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* A global loading indicator for any query fetching in the background, not just the ones on screen:
|
|
26
|
+
* ```svelte
|
|
27
|
+
* <script lang="ts">
|
|
28
|
+
* import { useIsFetching } from '@tanstack/svelte-query'
|
|
29
|
+
*
|
|
30
|
+
* const isFetching = useIsFetching()
|
|
31
|
+
* </script>
|
|
32
|
+
*
|
|
33
|
+
* {#if isFetching.current}
|
|
34
|
+
* <div>Queries are fetching in the background...</div>
|
|
35
|
+
* {/if}
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
3
38
|
export function useIsFetching(filters, queryClient) {
|
|
4
39
|
const client = useQueryClient(queryClient);
|
|
5
40
|
const queryCache = client.getQueryCache();
|
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
import { ReactiveValue } from './containers.svelte.js';
|
|
2
2
|
import type { MutationFilters, QueryClient } from '@tanstack/query-core';
|
|
3
|
+
/**
|
|
4
|
+
* `useIsMutating` is an optional hook that returns the `number` of mutations that your application is
|
|
5
|
+
* running (useful for app-wide loading indicators).
|
|
6
|
+
*
|
|
7
|
+
* @param filters - {@link MutationFilters} to narrow down which mutations to count.
|
|
8
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
9
|
+
* be used.
|
|
10
|
+
* @returns A reactive value — read `.current` to get how many matching mutations are currently running.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```svelte
|
|
14
|
+
* <script lang="ts">
|
|
15
|
+
* import { useIsMutating } from '@tanstack/svelte-query'
|
|
16
|
+
*
|
|
17
|
+
* // How many mutations matching the posts prefix are in progress?
|
|
18
|
+
* const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })
|
|
19
|
+
* </script>
|
|
20
|
+
*
|
|
21
|
+
* {#if isMutatingPosts.current}
|
|
22
|
+
* <span>Saving posts...</span>
|
|
23
|
+
* {/if}
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
3
26
|
export declare function useIsMutating(filters?: MutationFilters, queryClient?: QueryClient): ReactiveValue<number>;
|
|
4
27
|
//# sourceMappingURL=useIsMutating.svelte.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useIsMutating.svelte.d.ts","sourceRoot":"","sources":["../src/useIsMutating.svelte.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AACtD,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAExE,wBAAgB,aAAa,CAC3B,OAAO,CAAC,EAAE,eAAe,EACzB,WAAW,CAAC,EAAE,WAAW,GACxB,aAAa,CAAC,MAAM,CAAC,CAQvB"}
|
|
1
|
+
{"version":3,"file":"useIsMutating.svelte.d.ts","sourceRoot":"","sources":["../src/useIsMutating.svelte.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AACtD,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAExE;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,aAAa,CAC3B,OAAO,CAAC,EAAE,eAAe,EACzB,WAAW,CAAC,EAAE,WAAW,GACxB,aAAa,CAAC,MAAM,CAAC,CAQvB"}
|
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
import { useQueryClient } from './useQueryClient.js';
|
|
2
2
|
import { ReactiveValue } from './containers.svelte.js';
|
|
3
|
+
/**
|
|
4
|
+
* `useIsMutating` is an optional hook that returns the `number` of mutations that your application is
|
|
5
|
+
* running (useful for app-wide loading indicators).
|
|
6
|
+
*
|
|
7
|
+
* @param filters - {@link MutationFilters} to narrow down which mutations to count.
|
|
8
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
9
|
+
* be used.
|
|
10
|
+
* @returns A reactive value — read `.current` to get how many matching mutations are currently running.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```svelte
|
|
14
|
+
* <script lang="ts">
|
|
15
|
+
* import { useIsMutating } from '@tanstack/svelte-query'
|
|
16
|
+
*
|
|
17
|
+
* // How many mutations matching the posts prefix are in progress?
|
|
18
|
+
* const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })
|
|
19
|
+
* </script>
|
|
20
|
+
*
|
|
21
|
+
* {#if isMutatingPosts.current}
|
|
22
|
+
* <span>Saving posts...</span>
|
|
23
|
+
* {/if}
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
3
26
|
export function useIsMutating(filters, queryClient) {
|
|
4
27
|
const client = useQueryClient(queryClient);
|
|
5
28
|
const cache = client.getMutationCache();
|
|
@@ -1,4 +1,74 @@
|
|
|
1
1
|
import type { Mutation, MutationState, QueryClient } from '@tanstack/query-core';
|
|
2
2
|
import type { MutationStateOptions, MutationTypeFromResult } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* `useMutationState` gives you access to all mutations (matching the given `filters`), including ones that
|
|
5
|
+
* were created by a different component or hook instance, or even ones no longer mounted.
|
|
6
|
+
*
|
|
7
|
+
* @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
|
|
8
|
+
* mutation state.
|
|
9
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
10
|
+
* be used.
|
|
11
|
+
* @returns An Array of whatever `select` returns for each matching mutation.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* Get all variables of all running mutations:
|
|
15
|
+
* ```svelte
|
|
16
|
+
* <script lang="ts">
|
|
17
|
+
* import { useMutationState } from '@tanstack/svelte-query'
|
|
18
|
+
*
|
|
19
|
+
* const pendingVariables = useMutationState({
|
|
20
|
+
* filters: { status: 'pending' },
|
|
21
|
+
* select: (mutation) => mutation.state.variables,
|
|
22
|
+
* })
|
|
23
|
+
* </script>
|
|
24
|
+
*
|
|
25
|
+
* {pendingVariables.length} posts saving...
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* Get all data for specific mutations via the `mutationKey`:
|
|
30
|
+
* ```svelte
|
|
31
|
+
* <script lang="ts">
|
|
32
|
+
* import { createMutation, useMutationState } from '@tanstack/svelte-query'
|
|
33
|
+
*
|
|
34
|
+
* const mutationKey = ['posts']
|
|
35
|
+
*
|
|
36
|
+
* // Some mutation that we want to get the state for
|
|
37
|
+
* const mutation = createMutation(() => ({
|
|
38
|
+
* mutationKey,
|
|
39
|
+
* mutationFn: createPosts,
|
|
40
|
+
* }))
|
|
41
|
+
*
|
|
42
|
+
* const savedPosts = useMutationState({
|
|
43
|
+
* // this mutation key needs to match the mutation key of the given mutation (see above)
|
|
44
|
+
* filters: { mutationKey, status: 'success' },
|
|
45
|
+
* select: (mutation) => mutation.state.data,
|
|
46
|
+
* })
|
|
47
|
+
* </script>
|
|
48
|
+
*
|
|
49
|
+
* <button onclick={() => mutation.mutate(['New Post'])}>
|
|
50
|
+
* Create post ({savedPosts.length} saved so far)
|
|
51
|
+
* </button>
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* Access the latest mutation data via the `mutationKey`. Each invocation of `mutate` adds a new entry to the
|
|
56
|
+
* mutation cache for `gcTime` milliseconds — check the last item that `useMutationState` returns to get the
|
|
57
|
+
* latest successful mutation (the `status: 'success'` filter above excludes pending/errored ones):
|
|
58
|
+
* ```svelte
|
|
59
|
+
* <script lang="ts">
|
|
60
|
+
* import { useMutationState } from '@tanstack/svelte-query'
|
|
61
|
+
*
|
|
62
|
+
* const savedPosts = useMutationState({
|
|
63
|
+
* filters: { mutationKey: ['posts'], status: 'success' },
|
|
64
|
+
* select: (mutation) => mutation.state.data,
|
|
65
|
+
* })
|
|
66
|
+
*
|
|
67
|
+
* const latestSavedPost = $derived(savedPosts[savedPosts.length - 1])
|
|
68
|
+
* </script>
|
|
69
|
+
*
|
|
70
|
+
* {latestSavedPost ? 'Saved' : 'Nothing saved yet'}
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
3
73
|
export declare function useMutationState<TResult = MutationState, TMutation extends Mutation<any, any, any, any> = MutationTypeFromResult<TResult>>(options?: MutationStateOptions<TResult, TMutation>, queryClient?: QueryClient): Array<TResult>;
|
|
4
74
|
//# sourceMappingURL=useMutationState.svelte.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.svelte.d.ts","sourceRoot":"","sources":["../src/useMutationState.svelte.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,QAAQ,EAER,aAAa,EACb,WAAW,EACZ,MAAM,sBAAsB,CAAA;AAC7B,OAAO,KAAK,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AAoB9E,wBAAgB,gBAAgB,CAC9B,OAAO,GAAG,aAAa,EACvB,SAAS,SAAS,QAAQ,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAC5C,sBAAsB,CAAC,OAAO,CAAC,EAEjC,OAAO,GAAE,oBAAoB,CAAC,OAAO,EAAE,SAAS,CAAM,EACtD,WAAW,CAAC,EAAE,WAAW,GACxB,KAAK,CAAC,OAAO,CAAC,CA+BhB"}
|
|
1
|
+
{"version":3,"file":"useMutationState.svelte.d.ts","sourceRoot":"","sources":["../src/useMutationState.svelte.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,QAAQ,EAER,aAAa,EACb,WAAW,EACZ,MAAM,sBAAsB,CAAA;AAC7B,OAAO,KAAK,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AAoB9E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqEG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,GAAG,aAAa,EACvB,SAAS,SAAS,QAAQ,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAC5C,sBAAsB,CAAC,OAAO,CAAC,EAEjC,OAAO,GAAE,oBAAoB,CAAC,OAAO,EAAE,SAAS,CAAM,EACtD,WAAW,CAAC,EAAE,WAAW,GACxB,KAAK,CAAC,OAAO,CAAC,CA+BhB"}
|
|
@@ -7,6 +7,76 @@ function getResult(mutationCache, options) {
|
|
|
7
7
|
? options.select(mutation)
|
|
8
8
|
: mutation.state));
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* `useMutationState` gives you access to all mutations (matching the given `filters`), including ones that
|
|
12
|
+
* were created by a different component or hook instance, or even ones no longer mounted.
|
|
13
|
+
*
|
|
14
|
+
* @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
|
|
15
|
+
* mutation state.
|
|
16
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
17
|
+
* be used.
|
|
18
|
+
* @returns An Array of whatever `select` returns for each matching mutation.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* Get all variables of all running mutations:
|
|
22
|
+
* ```svelte
|
|
23
|
+
* <script lang="ts">
|
|
24
|
+
* import { useMutationState } from '@tanstack/svelte-query'
|
|
25
|
+
*
|
|
26
|
+
* const pendingVariables = useMutationState({
|
|
27
|
+
* filters: { status: 'pending' },
|
|
28
|
+
* select: (mutation) => mutation.state.variables,
|
|
29
|
+
* })
|
|
30
|
+
* </script>
|
|
31
|
+
*
|
|
32
|
+
* {pendingVariables.length} posts saving...
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* Get all data for specific mutations via the `mutationKey`:
|
|
37
|
+
* ```svelte
|
|
38
|
+
* <script lang="ts">
|
|
39
|
+
* import { createMutation, useMutationState } from '@tanstack/svelte-query'
|
|
40
|
+
*
|
|
41
|
+
* const mutationKey = ['posts']
|
|
42
|
+
*
|
|
43
|
+
* // Some mutation that we want to get the state for
|
|
44
|
+
* const mutation = createMutation(() => ({
|
|
45
|
+
* mutationKey,
|
|
46
|
+
* mutationFn: createPosts,
|
|
47
|
+
* }))
|
|
48
|
+
*
|
|
49
|
+
* const savedPosts = useMutationState({
|
|
50
|
+
* // this mutation key needs to match the mutation key of the given mutation (see above)
|
|
51
|
+
* filters: { mutationKey, status: 'success' },
|
|
52
|
+
* select: (mutation) => mutation.state.data,
|
|
53
|
+
* })
|
|
54
|
+
* </script>
|
|
55
|
+
*
|
|
56
|
+
* <button onclick={() => mutation.mutate(['New Post'])}>
|
|
57
|
+
* Create post ({savedPosts.length} saved so far)
|
|
58
|
+
* </button>
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* Access the latest mutation data via the `mutationKey`. Each invocation of `mutate` adds a new entry to the
|
|
63
|
+
* mutation cache for `gcTime` milliseconds — check the last item that `useMutationState` returns to get the
|
|
64
|
+
* latest successful mutation (the `status: 'success'` filter above excludes pending/errored ones):
|
|
65
|
+
* ```svelte
|
|
66
|
+
* <script lang="ts">
|
|
67
|
+
* import { useMutationState } from '@tanstack/svelte-query'
|
|
68
|
+
*
|
|
69
|
+
* const savedPosts = useMutationState({
|
|
70
|
+
* filters: { mutationKey: ['posts'], status: 'success' },
|
|
71
|
+
* select: (mutation) => mutation.state.data,
|
|
72
|
+
* })
|
|
73
|
+
*
|
|
74
|
+
* const latestSavedPost = $derived(savedPosts[savedPosts.length - 1])
|
|
75
|
+
* </script>
|
|
76
|
+
*
|
|
77
|
+
* {latestSavedPost ? 'Saved' : 'Nothing saved yet'}
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
10
80
|
export function useMutationState(options = {}, queryClient) {
|
|
11
81
|
const mutationCache = useQueryClient(queryClient).getMutationCache();
|
|
12
82
|
const result = $state(getResult(mutationCache, options));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/svelte-query",
|
|
3
|
-
"version": "6.1
|
|
3
|
+
"version": "6.2.1",
|
|
4
4
|
"description": "Primitives for managing, caching and syncing asynchronous and remote data in Svelte",
|
|
5
5
|
"author": "Lachlan Collins",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"!src/__tests__"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@tanstack/query-core": "5.
|
|
42
|
+
"@tanstack/query-core": "5.103.1"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@sveltejs/package": "^2.5.8",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
59
|
"clean": "premove ./dist ./coverage ./.svelte-kit ./dist-ts",
|
|
60
|
-
"compile": "tsc --
|
|
60
|
+
"compile": "tsc --project tsconfig.json",
|
|
61
61
|
"test:types": "svelte-check --tsconfig ./tsconfig.json",
|
|
62
62
|
"test:eslint": "eslint --concurrency=auto ./src ./tests",
|
|
63
63
|
"test:lib": "vitest",
|
package/src/context.ts
CHANGED
|
@@ -4,7 +4,13 @@ import type { Box } from './containers.svelte'
|
|
|
4
4
|
|
|
5
5
|
const _contextKey = Symbol('QueryClient')
|
|
6
6
|
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Retrieves the `QueryClient` set on Svelte's context by `QueryClientProvider` (or by
|
|
9
|
+
* {@link setQueryClientContext} directly). This is what {@link useQueryClient} calls internally.
|
|
10
|
+
*
|
|
11
|
+
* @throws If no `QueryClient` was found in context.
|
|
12
|
+
* @returns The `QueryClient` set on context, whether by `QueryClientProvider` or {@link setQueryClientContext}.
|
|
13
|
+
*/
|
|
8
14
|
export const getQueryClientContext = (): QueryClient => {
|
|
9
15
|
const client = getContext<QueryClient | undefined>(_contextKey)
|
|
10
16
|
if (!client) {
|
|
@@ -16,7 +22,26 @@ export const getQueryClientContext = (): QueryClient => {
|
|
|
16
22
|
return client
|
|
17
23
|
}
|
|
18
24
|
|
|
19
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Sets a `QueryClient` on Svelte's context, so it can be read with {@link getQueryClientContext} (or
|
|
27
|
+
* {@link useQueryClient}) from any descendant component. `QueryClientProvider` wraps this — use it directly
|
|
28
|
+
* only if you need to set the client from your own component instead.
|
|
29
|
+
*
|
|
30
|
+
* @param client - The `QueryClient` to make available to descendant components.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```svelte
|
|
34
|
+
* <script lang="ts">
|
|
35
|
+
* import { QueryClient, QueryClientProvider } from '@tanstack/svelte-query'
|
|
36
|
+
*
|
|
37
|
+
* const queryClient = new QueryClient()
|
|
38
|
+
* </script>
|
|
39
|
+
*
|
|
40
|
+
* <QueryClientProvider client={queryClient}>
|
|
41
|
+
* ...
|
|
42
|
+
* </QueryClientProvider>
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
20
45
|
export const setQueryClientContext = (client: QueryClient): void => {
|
|
21
46
|
setContext(_contextKey, client)
|
|
22
47
|
}
|
|
@@ -11,8 +11,144 @@ import type {
|
|
|
11
11
|
Accessor,
|
|
12
12
|
CreateInfiniteQueryOptions,
|
|
13
13
|
CreateInfiniteQueryResult,
|
|
14
|
+
DefinedCreateInfiniteQueryResult,
|
|
14
15
|
} from './types.js'
|
|
16
|
+
import type {
|
|
17
|
+
DefinedInitialDataInfiniteOptions,
|
|
18
|
+
UndefinedInitialDataInfiniteOptions,
|
|
19
|
+
} from './infiniteQueryOptions.js'
|
|
20
|
+
|
|
21
|
+
export function createInfiniteQuery<
|
|
22
|
+
TQueryFnData = unknown,
|
|
23
|
+
TError = DefaultError,
|
|
24
|
+
TData = InfiniteData<TQueryFnData>,
|
|
25
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
26
|
+
TPageParam = unknown,
|
|
27
|
+
>(
|
|
28
|
+
options: Accessor<
|
|
29
|
+
DefinedInitialDataInfiniteOptions<
|
|
30
|
+
TQueryFnData,
|
|
31
|
+
TError,
|
|
32
|
+
TData,
|
|
33
|
+
TQueryKey,
|
|
34
|
+
TPageParam
|
|
35
|
+
>
|
|
36
|
+
>,
|
|
37
|
+
queryClient?: Accessor<QueryClient>,
|
|
38
|
+
): DefinedCreateInfiniteQueryResult<TData, TError>
|
|
15
39
|
|
|
40
|
+
export function createInfiniteQuery<
|
|
41
|
+
TQueryFnData = unknown,
|
|
42
|
+
TError = DefaultError,
|
|
43
|
+
TData = InfiniteData<TQueryFnData>,
|
|
44
|
+
TQueryKey extends QueryKey = QueryKey,
|
|
45
|
+
TPageParam = unknown,
|
|
46
|
+
>(
|
|
47
|
+
options: Accessor<
|
|
48
|
+
UndefinedInitialDataInfiniteOptions<
|
|
49
|
+
TQueryFnData,
|
|
50
|
+
TError,
|
|
51
|
+
TData,
|
|
52
|
+
TQueryKey,
|
|
53
|
+
TPageParam
|
|
54
|
+
>
|
|
55
|
+
>,
|
|
56
|
+
queryClient?: Accessor<QueryClient>,
|
|
57
|
+
): CreateInfiniteQueryResult<TData, TError>
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @see {@link infiniteQueryOptions} to share these options between `createInfiniteQuery` and imperative APIs
|
|
61
|
+
* like `queryClient.infiniteQuery`.
|
|
62
|
+
* @param options - The {@link CreateInfiniteQueryOptions} to use — everything you can pass to
|
|
63
|
+
* `createInfiniteQuery`, wrapped in an {@link Accessor} so options can be reactive.
|
|
64
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
65
|
+
* be used.
|
|
66
|
+
* @returns The current query result, plus `fetchNextPage`/`fetchPreviousPage`/`hasNextPage`/`hasPreviousPage`
|
|
67
|
+
* to page through the query.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* Fetching the next page from a "Load More" button click:
|
|
71
|
+
* ```svelte
|
|
72
|
+
* <script lang="ts">
|
|
73
|
+
* import { createInfiniteQuery } from '@tanstack/svelte-query'
|
|
74
|
+
*
|
|
75
|
+
* const query = createInfiniteQuery(() => ({
|
|
76
|
+
* queryKey: ['projects'],
|
|
77
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
78
|
+
* initialPageParam: 0,
|
|
79
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
80
|
+
* }))
|
|
81
|
+
* </script>
|
|
82
|
+
*
|
|
83
|
+
* {#if query.isPending}
|
|
84
|
+
* Loading...
|
|
85
|
+
* {:else if query.isError}
|
|
86
|
+
* <span>Error: {query.error.message}</span>
|
|
87
|
+
* {:else}
|
|
88
|
+
* <ul>
|
|
89
|
+
* {#each query.data.pages as page}
|
|
90
|
+
* {#each page.projects as project (project.id)}
|
|
91
|
+
* <li>{project.name}</li>
|
|
92
|
+
* {/each}
|
|
93
|
+
* {/each}
|
|
94
|
+
* </ul>
|
|
95
|
+
* <button
|
|
96
|
+
* onclick={() => query.fetchNextPage()}
|
|
97
|
+
* disabled={!query.hasNextPage || query.isFetching}
|
|
98
|
+
* >
|
|
99
|
+
* {query.isFetchingNextPage
|
|
100
|
+
* ? 'Loading more...'
|
|
101
|
+
* : query.hasNextPage
|
|
102
|
+
* ? 'Load More'
|
|
103
|
+
* : 'Nothing more to load'}
|
|
104
|
+
* </button>
|
|
105
|
+
* {/if}
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
|
|
110
|
+
* sentinel element after the list:
|
|
111
|
+
* ```svelte
|
|
112
|
+
* <script lang="ts">
|
|
113
|
+
* import { createInfiniteQuery } from '@tanstack/svelte-query'
|
|
114
|
+
*
|
|
115
|
+
* const query = createInfiniteQuery(() => ({
|
|
116
|
+
* queryKey: ['projects'],
|
|
117
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
118
|
+
* initialPageParam: 0,
|
|
119
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
120
|
+
* }))
|
|
121
|
+
*
|
|
122
|
+
* let sentinel: HTMLDivElement | undefined = $state()
|
|
123
|
+
*
|
|
124
|
+
* $effect(() => {
|
|
125
|
+
* if (sentinel == null || !query.hasNextPage || query.isFetching) return
|
|
126
|
+
*
|
|
127
|
+
* const observer = new IntersectionObserver(([entry]) => {
|
|
128
|
+
* if (entry?.isIntersecting) query.fetchNextPage()
|
|
129
|
+
* })
|
|
130
|
+
* observer.observe(sentinel)
|
|
131
|
+
*
|
|
132
|
+
* return () => observer.disconnect()
|
|
133
|
+
* })
|
|
134
|
+
* </script>
|
|
135
|
+
*
|
|
136
|
+
* {#if query.isPending}
|
|
137
|
+
* Loading...
|
|
138
|
+
* {:else if query.isError}
|
|
139
|
+
* <span>Error: {query.error.message}</span>
|
|
140
|
+
* {:else}
|
|
141
|
+
* <ul>
|
|
142
|
+
* {#each query.data.pages as page}
|
|
143
|
+
* {#each page.projects as project (project.id)}
|
|
144
|
+
* <li>{project.name}</li>
|
|
145
|
+
* {/each}
|
|
146
|
+
* {/each}
|
|
147
|
+
* </ul>
|
|
148
|
+
* <div bind:this={sentinel}></div>
|
|
149
|
+
* {/if}
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
16
152
|
export function createInfiniteQuery<
|
|
17
153
|
TQueryFnData,
|
|
18
154
|
TError = DefaultError,
|
|
@@ -30,10 +166,15 @@ export function createInfiniteQuery<
|
|
|
30
166
|
>
|
|
31
167
|
>,
|
|
32
168
|
queryClient?: Accessor<QueryClient>,
|
|
33
|
-
): CreateInfiniteQueryResult<TData, TError>
|
|
169
|
+
): CreateInfiniteQueryResult<TData, TError>
|
|
170
|
+
|
|
171
|
+
export function createInfiniteQuery(
|
|
172
|
+
options: Accessor<CreateInfiniteQueryOptions>,
|
|
173
|
+
queryClient?: Accessor<QueryClient>,
|
|
174
|
+
) {
|
|
34
175
|
return createBaseQuery(
|
|
35
176
|
options,
|
|
36
177
|
InfiniteQueryObserver as typeof QueryObserver,
|
|
37
178
|
queryClient,
|
|
38
|
-
)
|
|
179
|
+
)
|
|
39
180
|
}
|