Download react-query/packages/solid-query/src/useMutation.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.87 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/packages/solid-query/src/useMutation.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/packages/solid-query/src/useMutation.ts
-
curl -L -o useMutation.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/react-query/packages/solid-query/src/useMutation.ts
1.87 kB
| import { MutationObserver, noop, shouldThrowError } from '@tanstack/query-core' | |
| import { createComputed, createMemo, on, onCleanup } from 'solid-js' | |
| import { createStore } from 'solid-js/store' | |
| import { useQueryClient } from './QueryClientProvider' | |
| import type { DefaultError } from '@tanstack/query-core' | |
| import type { QueryClient } from './QueryClient' | |
| import type { | |
| UseMutateFunction, | |
| UseMutationOptions, | |
| UseMutationResult, | |
| } from './types' | |
| import type { Accessor } from 'solid-js' | |
| // HOOK | |
| export function useMutation< | |
| TData = unknown, | |
| TError = DefaultError, | |
| TVariables = void, | |
| TContext = unknown, | |
| >( | |
| options: UseMutationOptions<TData, TError, TVariables, TContext>, | |
| queryClient?: Accessor<QueryClient>, | |
| ): UseMutationResult<TData, TError, TVariables, TContext> { | |
| const client = createMemo(() => useQueryClient(queryClient?.())) | |
| const observer = new MutationObserver<TData, TError, TVariables, TContext>( | |
| client(), | |
| options(), | |
| ) | |
| const mutate: UseMutateFunction<TData, TError, TVariables, TContext> = ( | |
| variables, | |
| mutateOptions, | |
| ) => { | |
| observer.mutate(variables, mutateOptions).catch(noop) | |
| } | |
| const [state, setState] = createStore< | |
| UseMutationResult<TData, TError, TVariables, TContext> | |
| >({ | |
| ...observer.getCurrentResult(), | |
| mutate, | |
| mutateAsync: observer.getCurrentResult().mutate, | |
| }) | |
| createComputed(() => { | |
| observer.setOptions(options()) | |
| }) | |
| createComputed( | |
| on( | |
| () => state.status, | |
| () => { | |
| if ( | |
| state.isError && | |
| shouldThrowError(observer.options.throwOnError, [state.error]) | |
| ) { | |
| throw state.error | |
| } | |
| }, | |
| ), | |
| ) | |
| const unsubscribe = observer.subscribe((result) => { | |
| setState({ | |
| ...result, | |
| mutate, | |
| mutateAsync: result.mutate, | |
| }) | |
| }) | |
| onCleanup(unsubscribe) | |
| return state | |
| } | |