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.

30 lines 1.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchOptimistic = exports.shouldSuspend = exports.willFetch = exports.ensureStaleTime = void 0; const ensureStaleTime = (defaultedOptions) => { 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; } } }; exports.ensureStaleTime = ensureStaleTime; const willFetch = (result, isRestoring) => result.isLoading && result.isFetching && !isRestoring; exports.willFetch = willFetch; const shouldSuspend = (defaultedOptions, result, isRestoring) => defaultedOptions?.suspense && (0, exports.willFetch)(result, isRestoring); exports.shouldSuspend = shouldSuspend; const fetchOptimistic = (defaultedOptions, observer, errorResetBoundary) => observer .fetchOptimistic(defaultedOptions) .then(({ data }) => { defaultedOptions.onSuccess?.(data); defaultedOptions.onSettled?.(data, null); }) .catch((error) => { errorResetBoundary.clearReset(); defaultedOptions.onError?.(error); defaultedOptions.onSettled?.(undefined, error); }); exports.fetchOptimistic = fetchOptimistic; //# sourceMappingURL=suspense.js.map