UNPKG

@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.

60 lines (55 loc) 1.9 kB
import type { DefaultedQueryObserverOptions } from '@tanstack/query-core' import type { QueryObserver } from '@tanstack/query-core' import type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary.tsx' import type { QueryObserverResult } from '@tanstack/query-core' import type { QueryKey } from '@tanstack/query-core' export const ensureStaleTime = ( defaultedOptions: DefaultedQueryObserverOptions<any, any, any, any, any>, ): void => { if (defaultedOptions.suspense) { // Always set stale time when using suspense to prevent // fetching again when directly mounting after suspending if (typeof defaultedOptions.staleTime !== 'number') { defaultedOptions.staleTime = 1000 } } } export const willFetch = ( result: QueryObserverResult<any, any>, isRestoring: boolean, ): boolean => result.isLoading && result.isFetching && !isRestoring export const shouldSuspend = ( defaultedOptions: | DefaultedQueryObserverOptions<any, any, any, any, any> | undefined, result: QueryObserverResult<any, any>, isRestoring: boolean, ): boolean | undefined => defaultedOptions?.suspense && willFetch(result, isRestoring) export const fetchOptimistic = < TQueryFnData, TError, TData, TQueryData, TQueryKey extends QueryKey, >( defaultedOptions: DefaultedQueryObserverOptions< TQueryFnData, TError, TData, TQueryData, TQueryKey >, observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>, errorResetBoundary: QueryErrorResetBoundaryValue, ): Promise<void> => observer .fetchOptimistic(defaultedOptions) .then(({ data }) => { defaultedOptions.onSuccess?.(data as TData) defaultedOptions.onSettled?.(data, null) }) .catch((error) => { errorResetBoundary.clearReset() defaultedOptions.onError?.(error) defaultedOptions.onSettled?.(undefined, error) })