Open History to see the last commit
1import { replaceEqualDeep } from '@tanstack/query-core'
2import { useQueryClient } from './useQueryClient.js'
3import type {
4 Mutation,
5 MutationCache,
6 MutationState,
7 QueryClient,
8} from '@tanstack/query-core'
9import type { MutationStateOptions, MutationTypeFromResult } from './types.js'
10β
11function getResult<
12 TResult = MutationState,
13 TMutation extends Mutation<any, any, any, any> =
14 MutationTypeFromResult<TResult>,
15>(
16 mutationCache: MutationCache,
17 options: MutationStateOptions<TResult, TMutation>,
18): Array<TResult> {
19 return mutationCache
20 .findAll(options.filters)
21 .map(
22 (mutation): TResult =>
23 (options.select
24 ? options.select(mutation as TMutation)
25 : mutation.state) as TResult,
26 )
27}
28β
29export function useMutationState<
30 TResult = MutationState,
31 TMutation extends Mutation<any, any, any, any> =
32 MutationTypeFromResult<TResult>,
33>(
34 options: MutationStateOptions<TResult, TMutation> = {},
35 queryClient?: QueryClient,
36): Array<TResult> {
37 const mutationCache = useQueryClient(queryClient).getMutationCache()
38 const result = $state(getResult(mutationCache, options))
39β
40 $effect(() => {
41 const unsubscribe = mutationCache.subscribe(() => {
42 const nextResult = replaceEqualDeep(
43 result,
44 getResult(mutationCache, options),
45 )
46 if (result !== nextResult) {
47 Object.assign(result, nextResult)
48 }
49 })
50β
51 return unsubscribe
52 })
53β
54 /* $effect(() => {
55 mutationCache.subscribe(() => {
56 const nextResult = replaceEqualDeep(
57 result.current,
58 getResult(mutationCache, optionsRef),
59 )
60 if (result.current !== nextResult) {
61 result = nextResult
62 //notifyManager.schedule(onStoreChange)
63 }
64 })
65 }) */
66 return result
67}