|
1 | | -import { |
2 | | - Mutation, |
3 | | - MutationFilters, |
4 | | - MutationFunction, |
5 | | - MutationKey, |
6 | | - MutationObserver, |
7 | | - MutationState, |
8 | | - QueryClient, |
9 | | - QueryKeyHashFunction, |
10 | | - UseMutationOptions, |
11 | | - useIsMutating, |
12 | | - useMutation, |
13 | | - useMutationState, |
14 | | - useQueryClient, |
15 | | -} from '@tanstack/react-query'; |
16 | 1 | import { QueryTagOption, QueryUpdateTagObject } from '../tags/types'; |
| 2 | +import { MutationBuilderFrozen } from './MutationBuilderFrozen'; |
17 | 3 | import { MiddlewareFn, createMiddlewareFunction } from './createMiddlewareFunction'; |
18 | 4 | import { createUpdateMiddleware } from './createUpdateMiddleware'; |
19 | | -import { BuilderConfig } from './types'; |
20 | | -import { areKeysEqual, mergeMutationOptions, mergeVars } from './utils'; |
21 | | - |
22 | | -function getRandomKey() { |
23 | | - return Math.random().toString(36).substring(7); |
24 | | -} |
25 | | - |
26 | | -export class MutationBuilderFrozen<TVars, TData, TError, TKey extends unknown[]> { |
27 | | - protected declare _config: MutationBuilderConfig<TVars, TData, TError, TKey>; |
28 | | - protected declare _options: typeof this._config.options; |
29 | | - protected declare _vars: TVars; |
30 | | - |
31 | | - constructor( |
32 | | - public config: typeof this._config, |
33 | | - public mutationKeyPrefix = getRandomKey(), |
34 | | - ) {} |
35 | | - |
36 | | - protected mergeConfigs: (config: typeof this._config, other: Partial<typeof this._config>) => typeof this._config = ( |
37 | | - config, |
38 | | - other, |
39 | | - ) => { |
40 | | - return { |
41 | | - ...config, |
42 | | - ...other, |
43 | | - vars: mergeVars([config.vars, other.vars], other.mergeVars || config.mergeVars), |
44 | | - options: mergeMutationOptions([config.options, other.options]), |
45 | | - }; |
46 | | - }; |
47 | | - |
48 | | - protected mergeVars: (list: (Partial<TVars> | undefined)[]) => TVars = (list) => { |
49 | | - return mergeVars(list, this.config.mergeVars); |
50 | | - }; |
51 | | - |
52 | | - getMutationFn: (queryClient: QueryClient, meta?: any) => MutationFunction<TData, TVars> = (queryClient, meta) => { |
53 | | - return async (vars) => { |
54 | | - const queryKey = [this.mergeVars([this.config.vars, vars])] as TKey; |
55 | | - return this.config.queryFn({ |
56 | | - queryKey, |
57 | | - meta, |
58 | | - client: this.config.queryClient || queryClient, |
59 | | - signal: new AbortController().signal, |
60 | | - originalQueryKey: queryKey, |
61 | | - }); |
62 | | - }; |
63 | | - }; |
64 | | - |
65 | | - getMutationKey: () => MutationKey = () => { |
66 | | - return [this.mutationKeyPrefix]; |
67 | | - }; |
68 | | - |
69 | | - getMutationOptions: ( |
70 | | - queryClient: QueryClient, |
71 | | - opts?: typeof this._options, |
72 | | - ) => UseMutationOptions<TData, TError, TVars> = (queryClient, opts) => { |
73 | | - return mergeMutationOptions([ |
74 | | - { |
75 | | - mutationKey: this.getMutationKey(), |
76 | | - mutationFn: this.getMutationFn(queryClient, opts?.meta), |
77 | | - }, |
78 | | - this.config.options, |
79 | | - opts, |
80 | | - ]); |
81 | | - }; |
82 | | - |
83 | | - getMutationFilters: ( |
84 | | - vars?: TVars, |
85 | | - filters?: MutationFilters<TData, TError, TVars>, |
86 | | - ) => MutationFilters<any, any, any> = (vars, filters) => { |
87 | | - return { |
88 | | - mutationKey: this.getMutationKey(), |
89 | | - ...filters, |
90 | | - predicate: (m) => { |
91 | | - if (filters?.predicate && !filters.predicate(m)) return false; |
92 | | - if (vars == null) return true; |
93 | | - if (!m.state.variables) return false; |
94 | | - return areKeysEqual( |
95 | | - [m.state.variables], |
96 | | - [vars], |
97 | | - this.config.queryKeyHashFn as QueryKeyHashFunction<readonly unknown[]>, |
98 | | - ); |
99 | | - }, |
100 | | - }; |
101 | | - }; |
102 | | - |
103 | | - useMutation: (opts?: typeof this._options) => ReturnType<typeof useMutation<TData, TError, TVars>> = (opts) => { |
104 | | - const queryClient = useQueryClient(this.config.queryClient); |
105 | | - return useMutation(this.getMutationOptions(queryClient, opts), this.config.queryClient); |
106 | | - }; |
107 | | - |
108 | | - useIsMutating: (vars: TVars, filters?: MutationFilters<TData, TError, TVars>) => number = (vars, filters) => { |
109 | | - return useIsMutating(this.getMutationFilters(vars, filters), this.config.queryClient); |
110 | | - }; |
111 | | - |
112 | | - useMutationState: <TSelect = Mutation<TData, TError, TVars>>( |
113 | | - vars?: TVars, |
114 | | - filters?: MutationFilters<TData, TError, TVars>, |
115 | | - select?: (mt: Mutation<TData, TError, TVars>) => TSelect, |
116 | | - ) => TSelect[] = (vars, filters, select) => { |
117 | | - return useMutationState( |
118 | | - { filters: this.getMutationFilters(vars, filters), select: select as any }, |
119 | | - this.config.queryClient, |
120 | | - ); |
121 | | - }; |
122 | | - |
123 | | - readonly getMutation = (vars?: TVars, filters?: MutationFilters<TData, TError, TVars>, queryClient?: QueryClient) => { |
124 | | - const client = queryClient || this.config.queryClient!; |
125 | | - return client.getMutationCache().find(this.getMutationFilters(vars, filters)); |
126 | | - }; |
127 | | - |
128 | | - readonly isMutating = (vars?: TVars, filters?: MutationFilters<TData, TError, TVars>, queryClient?: QueryClient) => { |
129 | | - const client = queryClient || this.config.queryClient!; |
130 | | - return client.isMutating(this.getMutationFilters(vars, filters)); |
131 | | - }; |
132 | | - |
133 | | - readonly mutate = async (vars: TVars, opts?: typeof this._options, queryClient?: QueryClient) => { |
134 | | - const client = queryClient || this.config.queryClient!; |
135 | | - const options = this.getMutationOptions(client, opts); |
136 | | - const observer = new MutationObserver<TData, TError, TVars>(client, options); |
137 | | - return observer.mutate(vars, options).finally(() => observer.reset()); |
138 | | - }; |
139 | | -} |
140 | | - |
141 | | -export type MutationStateHelper<TVars, TData, TError, TKey> = { |
142 | | - list: MutationState<TData, TError, TVars>[]; |
143 | | - getMutation(vars: TVars): MutationState<TData, TError, TVars> | undefined; |
144 | | -}; |
| 5 | +import { mergeVars } from './utils'; |
145 | 6 |
|
146 | 7 | export class MutationBuilder<TVars, TData, TError, TKey extends unknown[]> extends MutationBuilderFrozen< |
147 | 8 | TVars, |
@@ -194,12 +55,3 @@ export class MutationBuilder<TVars, TData, TError, TKey extends unknown[]> exten |
194 | 55 | return this; |
195 | 56 | } |
196 | 57 | } |
197 | | - |
198 | | -export type MutationBuilderConfig<TVars, TData, TError, TKey extends unknown[]> = BuilderConfig< |
199 | | - TVars, |
200 | | - TData, |
201 | | - TError, |
202 | | - TKey |
203 | | -> & { |
204 | | - options?: UseMutationOptions<TData, TError, TVars>; |
205 | | -}; |
0 commit comments