@tanstack/svelte-query 5.37.1 → 5.38.0
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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +6 -1
- package/dist/useMutationState.d.ts +5 -0
- package/dist/useMutationState.js +23 -0
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/types.ts +11 -0
- package/src/useMutationState.ts +49 -0
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export { createQueries } from './createQueries';
|
|
|
9
9
|
export { createInfiniteQuery } from './createInfiniteQuery';
|
|
10
10
|
export { infiniteQueryOptions } from './infiniteQueryOptions';
|
|
11
11
|
export { createMutation } from './createMutation';
|
|
12
|
+
export { useMutationState } from './useMutationState';
|
|
12
13
|
export { useQueryClient } from './useQueryClient';
|
|
13
14
|
export { useIsFetching } from './useIsFetching';
|
|
14
15
|
export { useIsMutating } from './useIsMutating';
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,7 @@ export { createQueries } from './createQueries';
|
|
|
10
10
|
export { createInfiniteQuery } from './createInfiniteQuery';
|
|
11
11
|
export { infiniteQueryOptions } from './infiniteQueryOptions';
|
|
12
12
|
export { createMutation } from './createMutation';
|
|
13
|
+
export { useMutationState } from './useMutationState';
|
|
13
14
|
export { useQueryClient } from './useQueryClient';
|
|
14
15
|
export { useIsFetching } from './useIsFetching';
|
|
15
16
|
export { useIsMutating } from './useIsMutating';
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="svelte" />
|
|
2
|
-
import type { DefaultError, DefinedQueryObserverResult, InfiniteQueryObserverOptions, InfiniteQueryObserverResult, MutateFunction, MutationObserverOptions, MutationObserverResult, OmitKeyof, QueryKey, QueryObserverOptions, QueryObserverResult } from '@tanstack/query-core';
|
|
2
|
+
import type { DefaultError, DefinedQueryObserverResult, InfiniteQueryObserverOptions, InfiniteQueryObserverResult, MutateFunction, Mutation, MutationFilters, MutationObserverOptions, MutationObserverResult, MutationState, OmitKeyof, QueryKey, QueryObserverOptions, QueryObserverResult } from '@tanstack/query-core';
|
|
3
3
|
import type { Readable } from 'svelte/store';
|
|
4
4
|
/** Allows a type to be either the base object or a store of that object */
|
|
5
5
|
export type StoreOrVal<T> = T | Readable<T>;
|
|
@@ -33,4 +33,9 @@ export type CreateMutationResult<TData = unknown, TError = DefaultError, TVariab
|
|
|
33
33
|
type Override<TTargetA, TTargetB> = {
|
|
34
34
|
[AKey in keyof TTargetA]: AKey extends keyof TTargetB ? TTargetB[AKey] : TTargetA[AKey];
|
|
35
35
|
};
|
|
36
|
+
/** Options for useMutationState */
|
|
37
|
+
export type MutationStateOptions<TResult = MutationState> = {
|
|
38
|
+
filters?: MutationFilters;
|
|
39
|
+
select?: (mutation: Mutation<unknown, DefaultError, unknown, unknown>) => TResult;
|
|
40
|
+
};
|
|
36
41
|
export {};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/// <reference types="svelte" />
|
|
2
|
+
import type { MutationState, QueryClient } from '@tanstack/query-core';
|
|
3
|
+
import type { Readable } from 'svelte/store';
|
|
4
|
+
import type { MutationStateOptions } from './types';
|
|
5
|
+
export declare function useMutationState<TResult = MutationState>(options?: MutationStateOptions<TResult>, queryClient?: QueryClient): Readable<Array<TResult>>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { readable } from 'svelte/store';
|
|
2
|
+
import { notifyManager, replaceEqualDeep } from '@tanstack/query-core';
|
|
3
|
+
import { useQueryClient } from './useQueryClient';
|
|
4
|
+
function getResult(mutationCache, options) {
|
|
5
|
+
return mutationCache
|
|
6
|
+
.findAll(options.filters)
|
|
7
|
+
.map((mutation) => (options.select ? options.select(mutation) : mutation.state));
|
|
8
|
+
}
|
|
9
|
+
export function useMutationState(options = {}, queryClient) {
|
|
10
|
+
const client = useQueryClient(queryClient);
|
|
11
|
+
const mutationCache = client.getMutationCache();
|
|
12
|
+
let result = getResult(mutationCache, options);
|
|
13
|
+
const { subscribe } = readable(result, (set) => {
|
|
14
|
+
return mutationCache.subscribe(notifyManager.batchCalls(() => {
|
|
15
|
+
const nextResult = replaceEqualDeep(result, getResult(mutationCache, options));
|
|
16
|
+
if (result !== nextResult) {
|
|
17
|
+
result = nextResult;
|
|
18
|
+
set(result);
|
|
19
|
+
}
|
|
20
|
+
}));
|
|
21
|
+
});
|
|
22
|
+
return { subscribe };
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/svelte-query",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.38.0",
|
|
4
4
|
"description": "Primitives for managing, caching and syncing asynchronous and remote data in Svelte",
|
|
5
5
|
"author": "Lachlan Collins",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"!src/__tests__"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@tanstack/query-core": "5.
|
|
36
|
+
"@tanstack/query-core": "5.38.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@sveltejs/package": "^2.3.1",
|
package/src/index.ts
CHANGED
|
@@ -18,6 +18,7 @@ export { createQueries } from './createQueries'
|
|
|
18
18
|
export { createInfiniteQuery } from './createInfiniteQuery'
|
|
19
19
|
export { infiniteQueryOptions } from './infiniteQueryOptions'
|
|
20
20
|
export { createMutation } from './createMutation'
|
|
21
|
+
export { useMutationState } from './useMutationState'
|
|
21
22
|
export { useQueryClient } from './useQueryClient'
|
|
22
23
|
export { useIsFetching } from './useIsFetching'
|
|
23
24
|
export { useIsMutating } from './useIsMutating'
|
package/src/types.ts
CHANGED
|
@@ -4,8 +4,11 @@ import type {
|
|
|
4
4
|
InfiniteQueryObserverOptions,
|
|
5
5
|
InfiniteQueryObserverResult,
|
|
6
6
|
MutateFunction,
|
|
7
|
+
Mutation,
|
|
8
|
+
MutationFilters,
|
|
7
9
|
MutationObserverOptions,
|
|
8
10
|
MutationObserverResult,
|
|
11
|
+
MutationState,
|
|
9
12
|
OmitKeyof,
|
|
10
13
|
QueryKey,
|
|
11
14
|
QueryObserverOptions,
|
|
@@ -134,3 +137,11 @@ type Override<TTargetA, TTargetB> = {
|
|
|
134
137
|
? TTargetB[AKey]
|
|
135
138
|
: TTargetA[AKey]
|
|
136
139
|
}
|
|
140
|
+
|
|
141
|
+
/** Options for useMutationState */
|
|
142
|
+
export type MutationStateOptions<TResult = MutationState> = {
|
|
143
|
+
filters?: MutationFilters
|
|
144
|
+
select?: (
|
|
145
|
+
mutation: Mutation<unknown, DefaultError, unknown, unknown>,
|
|
146
|
+
) => TResult
|
|
147
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { readable } from 'svelte/store'
|
|
2
|
+
import { notifyManager, replaceEqualDeep } from '@tanstack/query-core'
|
|
3
|
+
import { useQueryClient } from './useQueryClient'
|
|
4
|
+
import type {
|
|
5
|
+
MutationCache,
|
|
6
|
+
MutationState,
|
|
7
|
+
QueryClient,
|
|
8
|
+
} from '@tanstack/query-core'
|
|
9
|
+
import type { Readable } from 'svelte/store'
|
|
10
|
+
import type { MutationStateOptions } from './types'
|
|
11
|
+
|
|
12
|
+
function getResult<TResult = MutationState>(
|
|
13
|
+
mutationCache: MutationCache,
|
|
14
|
+
options: MutationStateOptions<TResult>,
|
|
15
|
+
): Array<TResult> {
|
|
16
|
+
return mutationCache
|
|
17
|
+
.findAll(options.filters)
|
|
18
|
+
.map(
|
|
19
|
+
(mutation): TResult =>
|
|
20
|
+
(options.select ? options.select(mutation) : mutation.state) as TResult,
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function useMutationState<TResult = MutationState>(
|
|
25
|
+
options: MutationStateOptions<TResult> = {},
|
|
26
|
+
queryClient?: QueryClient,
|
|
27
|
+
): Readable<Array<TResult>> {
|
|
28
|
+
const client = useQueryClient(queryClient)
|
|
29
|
+
const mutationCache = client.getMutationCache()
|
|
30
|
+
|
|
31
|
+
let result = getResult(mutationCache, options)
|
|
32
|
+
|
|
33
|
+
const { subscribe } = readable(result, (set) => {
|
|
34
|
+
return mutationCache.subscribe(
|
|
35
|
+
notifyManager.batchCalls(() => {
|
|
36
|
+
const nextResult = replaceEqualDeep(
|
|
37
|
+
result,
|
|
38
|
+
getResult(mutationCache, options),
|
|
39
|
+
)
|
|
40
|
+
if (result !== nextResult) {
|
|
41
|
+
result = nextResult
|
|
42
|
+
set(result)
|
|
43
|
+
}
|
|
44
|
+
}),
|
|
45
|
+
)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
return { subscribe }
|
|
49
|
+
}
|