( options: Accessor<CreateMutationOptions<TData, TError, TVariables, TContext>>, queryClient?: Accessor<QueryClient>, )
| 162 | * ``` |
| 163 | */ |
| 164 | export function createMutation< |
| 165 | TData = unknown, |
| 166 | TError = DefaultError, |
| 167 | TVariables = void, |
| 168 | TContext = unknown, |
| 169 | >( |
| 170 | options: Accessor<CreateMutationOptions<TData, TError, TVariables, TContext>>, |
| 171 | queryClient?: Accessor<QueryClient>, |
| 172 | ): CreateMutationResult<TData, TError, TVariables, TContext> { |
| 173 | const client = $derived(useQueryClient(queryClient?.())) |
| 174 | |
| 175 | // svelte-ignore state_referenced_locally - intentional, initial value |
| 176 | let observer = $state( |
| 177 | // svelte-ignore state_referenced_locally - intentional, initial value |
| 178 | new MutationObserver<TData, TError, TVariables, TContext>( |
| 179 | client, |
| 180 | options(), |
| 181 | ), |
| 182 | ) |
| 183 | |
| 184 | watchChanges( |
| 185 | () => client, |
| 186 | 'pre', |
| 187 | () => { |
| 188 | observer = new MutationObserver(client, options()) |
| 189 | }, |
| 190 | ) |
| 191 | |
| 192 | $effect.pre(() => { |
| 193 | observer.setOptions(options()) |
| 194 | }) |
| 195 | |
| 196 | const mutate: CreateMutateFunction<TData, TError, TVariables, TContext> = ( |
| 197 | ...args |
| 198 | ) => { |
| 199 | observer.mutate(args[0] as TVariables, args[1]).catch(noop) |
| 200 | } |
| 201 | |
| 202 | let result = $state(observer.getCurrentResult()) |
| 203 | watchChanges( |
| 204 | () => observer, |
| 205 | 'pre', |
| 206 | () => { |
| 207 | result = observer.getCurrentResult() |
| 208 | }, |
| 209 | ) |
| 210 | |
| 211 | $effect.pre(() => { |
| 212 | const unsubscribe = observer.subscribe((val) => { |
| 213 | notifyManager.batchCalls(() => { |
| 214 | Object.assign(result, val) |
| 215 | })() |
| 216 | }) |
| 217 | return unsubscribe |
| 218 | }) |
| 219 | |
| 220 | const resultProxy = $derived( |
| 221 | new Proxy(result, { |
no test coverage detected