@tanstack/svelte-query 5.37.0 → 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/context.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="svelte" />
1
2
  import type { QueryClient } from '@tanstack/query-core';
2
3
  import type { Readable } from 'svelte/store';
3
4
  /** Retrieves a Client from Svelte's context */
@@ -1,3 +1,4 @@
1
+ /// <reference types="svelte" />
1
2
  import type { Readable } from 'svelte/store';
2
3
  import type { StoreOrVal } from './types';
3
4
  import type { DefaultError, OmitKeyof, QueriesPlaceholderDataFunction, QueryClient, QueryFunction, QueryKey, QueryObserverOptions, QueryObserverResult, SkipToken, ThrowOnError } from '@tanstack/query-core';
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,4 +1,5 @@
1
- import type { DefaultError, DefinedQueryObserverResult, InfiniteQueryObserverOptions, InfiniteQueryObserverResult, MutateFunction, MutationObserverOptions, MutationObserverResult, OmitKeyof, QueryKey, QueryObserverOptions, QueryObserverResult } from '@tanstack/query-core';
1
+ /// <reference types="svelte" />
2
+ import type { DefaultError, DefinedQueryObserverResult, InfiniteQueryObserverOptions, InfiniteQueryObserverResult, MutateFunction, Mutation, MutationFilters, MutationObserverOptions, MutationObserverResult, MutationState, OmitKeyof, QueryKey, QueryObserverOptions, QueryObserverResult } from '@tanstack/query-core';
2
3
  import type { Readable } from 'svelte/store';
3
4
  /** Allows a type to be either the base object or a store of that object */
4
5
  export type StoreOrVal<T> = T | Readable<T>;
@@ -32,4 +33,9 @@ export type CreateMutationResult<TData = unknown, TError = DefaultError, TVariab
32
33
  type Override<TTargetA, TTargetB> = {
33
34
  [AKey in keyof TTargetA]: AKey extends keyof TTargetB ? TTargetB[AKey] : TTargetA[AKey];
34
35
  };
36
+ /** Options for useMutationState */
37
+ export type MutationStateOptions<TResult = MutationState> = {
38
+ filters?: MutationFilters;
39
+ select?: (mutation: Mutation<unknown, DefaultError, unknown, unknown>) => TResult;
40
+ };
35
41
  export {};
@@ -1,3 +1,4 @@
1
+ /// <reference types="svelte" />
1
2
  import { type QueryClient, type QueryFilters } from '@tanstack/query-core';
2
3
  import type { Readable } from 'svelte/store';
3
4
  export declare function useIsFetching(filters?: QueryFilters, queryClient?: QueryClient): Readable<number>;
@@ -1,3 +1,4 @@
1
+ /// <reference types="svelte" />
1
2
  import { type MutationFilters, type QueryClient } from '@tanstack/query-core';
2
3
  import type { Readable } from 'svelte/store';
3
4
  export declare function useIsMutating(filters?: MutationFilters, queryClient?: QueryClient): Readable<number>;
@@ -1,2 +1,3 @@
1
+ /// <reference types="svelte" />
1
2
  import type { Readable } from 'svelte/store';
2
3
  export declare function useIsRestoring(): Readable<boolean>;
@@ -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/dist/utils.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="svelte" />
1
2
  import type { Readable } from 'svelte/store';
2
3
  import type { StoreOrVal } from './types';
3
4
  export declare function isSvelteStore<T extends object>(obj: StoreOrVal<T>): obj is Readable<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/svelte-query",
3
- "version": "5.37.0",
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,28 +33,19 @@
33
33
  "!src/__tests__"
34
34
  ],
35
35
  "dependencies": {
36
- "@tanstack/query-core": "5.36.1"
36
+ "@tanstack/query-core": "5.38.0"
37
37
  },
38
38
  "devDependencies": {
39
- "@sveltejs/package": "^2.2.6",
40
- "@sveltejs/vite-plugin-svelte": "^3.0.2",
41
- "@testing-library/svelte": "^4.1.0",
39
+ "@sveltejs/package": "^2.3.1",
40
+ "@sveltejs/vite-plugin-svelte": "^3.1.0",
41
+ "@testing-library/svelte": "^5.1.0",
42
42
  "eslint-plugin-svelte": "^2.39.0",
43
- "svelte": "^4.2.10",
44
- "svelte-check": "^3.6.4",
45
- "svelte-eslint-parser": "^0.33.1"
43
+ "svelte": "^4.2.17",
44
+ "svelte-check": "^3.7.1",
45
+ "svelte-eslint-parser": "^0.36.0"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "svelte": "^3.54.0 || ^4.0.0 || ^5.0.0-next.0"
49
49
  },
50
- "scripts": {
51
- "clean": "rimraf ./dist && rimraf ./coverage",
52
- "test:types": "svelte-check --tsconfig ./tsconfig.json",
53
- "test:eslint": "eslint --ext .svelte,.ts ./src",
54
- "test:lib": "vitest",
55
- "test:lib:dev": "pnpm run test:lib --watch",
56
- "test:build": "publint --strict",
57
- "test:attw": "attw --pack",
58
- "build": "svelte-package --input ./src --output ./dist"
59
- }
50
+ "scripts": {}
60
51
  }
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
+ }