@preact-signals/query
Version:
A reactive utility for React/Preact that simplifies the handling of data fetching and state management. Powered by Preact Signals, it provides hooks and functions to create reactive resources and manage their state seamlessly.
42 lines • 1.9 kB
JavaScript
import { useComputedOnce } from "@preact-signals/utils/hooks";
import { MutationObserver } from "@tanstack/query-core";
import { useMemo } from "react";
import { useQueryClient$ } from "./react-query/QueryClientProvider";
import { useObserverStore } from "./useObserver";
import { EMPTY_ARRAY, useRefBasedOptions, wrapFunctionsInUntracked, } from "./utils";
import { untracked, useSignalEffect } from "@preact-signals/unified-signals";
import { shouldThrowError } from "./react-query/utils";
function noop() { }
export const useMutation$ = (options) => {
const $options = useRefBasedOptions(options);
const $client = useQueryClient$({
context: useComputedOnce(() => $options.value.context).value,
});
const observer = useComputedOnce(
// we will update current mutation observer with new options, so using `peek`
() => new MutationObserver($client.value, wrapFunctionsInUntracked($options.peek())));
useSignalEffect(() => {
observer.value.setOptions(wrapFunctionsInUntracked($options.value));
});
const mutate = useMemo(() => (variables, mutateOptions) => void observer.peek().mutate(variables, mutateOptions).catch(noop), EMPTY_ARRAY);
const observerResultToStore = (result) => ({
...result,
mutate,
mutateAsync: result.mutate,
});
const store = useObserverStore(() => ({
getCurrent: () => observerResultToStore(observer.value.getCurrentResult()),
subscribe: (emit) => observer.value.subscribe((newValue) => {
emit(observerResultToStore(newValue));
}),
}));
const shouldThrow = useComputedOnce(() => store.error &&
shouldThrowError(observer.value.options.useErrorBoundary, [store.error]));
if (shouldThrow.value) {
untracked(() => {
throw store.error;
});
}
return store;
};
//# sourceMappingURL=useMutation$.js.map