@tanstack/preact-query 5.102.2 → 5.102.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/HydrationBoundary.cjs +43 -0
- package/build/legacy/HydrationBoundary.cjs.map +1 -1
- package/build/legacy/HydrationBoundary.d.cts +61 -0
- package/build/legacy/HydrationBoundary.d.cts.map +1 -1
- package/build/legacy/HydrationBoundary.d.ts +61 -0
- package/build/legacy/HydrationBoundary.d.ts.map +1 -1
- package/build/legacy/HydrationBoundary.js +43 -0
- package/build/legacy/HydrationBoundary.js.map +1 -1
- package/build/legacy/IsRestoringProvider.cjs +11 -0
- package/build/legacy/IsRestoringProvider.cjs.map +1 -1
- package/build/legacy/IsRestoringProvider.d.cts +11 -0
- package/build/legacy/IsRestoringProvider.d.cts.map +1 -1
- package/build/legacy/IsRestoringProvider.d.ts +11 -0
- package/build/legacy/IsRestoringProvider.d.ts.map +1 -1
- package/build/legacy/IsRestoringProvider.js +11 -0
- package/build/legacy/IsRestoringProvider.js.map +1 -1
- package/build/legacy/QueryClientProvider.cjs +30 -0
- package/build/legacy/QueryClientProvider.cjs.map +1 -1
- package/build/legacy/QueryClientProvider.d.cts +41 -0
- package/build/legacy/QueryClientProvider.d.cts.map +1 -1
- package/build/legacy/QueryClientProvider.d.ts +41 -0
- package/build/legacy/QueryClientProvider.d.ts.map +1 -1
- package/build/legacy/QueryClientProvider.js +30 -0
- package/build/legacy/QueryClientProvider.js.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.cjs +75 -0
- package/build/legacy/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.d.cts +106 -0
- package/build/legacy/QueryErrorResetBoundary.d.cts.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.d.ts +106 -0
- package/build/legacy/QueryErrorResetBoundary.d.ts.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.js +75 -0
- package/build/legacy/QueryErrorResetBoundary.js.map +1 -1
- package/build/legacy/infiniteQueryOptions.cjs.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.cts +187 -0
- package/build/legacy/infiniteQueryOptions.d.cts.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.ts +187 -0
- package/build/legacy/infiniteQueryOptions.d.ts.map +1 -1
- package/build/legacy/infiniteQueryOptions.js.map +1 -1
- package/build/legacy/mutationOptions.cjs.map +1 -1
- package/build/legacy/mutationOptions.d.cts +68 -0
- package/build/legacy/mutationOptions.d.cts.map +1 -1
- package/build/legacy/mutationOptions.d.ts +68 -0
- package/build/legacy/mutationOptions.d.ts.map +1 -1
- package/build/legacy/mutationOptions.js.map +1 -1
- package/build/legacy/queryOptions.cjs.map +1 -1
- package/build/legacy/queryOptions.d.cts +198 -0
- package/build/legacy/queryOptions.d.cts.map +1 -1
- package/build/legacy/queryOptions.d.ts +198 -0
- package/build/legacy/queryOptions.d.ts.map +1 -1
- package/build/legacy/queryOptions.js.map +1 -1
- package/build/legacy/types.cjs.map +1 -1
- package/build/legacy/types.d.cts +228 -2
- package/build/legacy/types.d.cts.map +1 -1
- package/build/legacy/types.d.ts +228 -2
- package/build/legacy/types.d.ts.map +1 -1
- package/build/legacy/types.js.map +1 -1
- package/build/legacy/useInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useInfiniteQuery.d.cts +116 -0
- package/build/legacy/useInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useInfiniteQuery.d.ts +116 -0
- package/build/legacy/useInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useInfiniteQuery.js.map +1 -1
- package/build/legacy/useIsFetching.cjs +20 -0
- package/build/legacy/useIsFetching.cjs.map +1 -1
- package/build/legacy/useIsFetching.d.cts +20 -0
- package/build/legacy/useIsFetching.d.cts.map +1 -1
- package/build/legacy/useIsFetching.d.ts +20 -0
- package/build/legacy/useIsFetching.d.ts.map +1 -1
- package/build/legacy/useIsFetching.js +20 -0
- package/build/legacy/useIsFetching.js.map +1 -1
- package/build/legacy/useMutation.cjs +67 -0
- package/build/legacy/useMutation.cjs.map +1 -1
- package/build/legacy/useMutation.d.cts +67 -0
- package/build/legacy/useMutation.d.cts.map +1 -1
- package/build/legacy/useMutation.d.ts +67 -0
- package/build/legacy/useMutation.d.ts.map +1 -1
- package/build/legacy/useMutation.js +67 -0
- package/build/legacy/useMutation.js.map +1 -1
- package/build/legacy/useMutationState.cjs +74 -0
- package/build/legacy/useMutationState.cjs.map +1 -1
- package/build/legacy/useMutationState.d.cts +74 -0
- package/build/legacy/useMutationState.d.cts.map +1 -1
- package/build/legacy/useMutationState.d.ts +74 -0
- package/build/legacy/useMutationState.d.ts.map +1 -1
- package/build/legacy/useMutationState.js +74 -0
- package/build/legacy/useMutationState.js.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.cjs +44 -0
- package/build/legacy/usePrefetchInfiniteQuery.cjs.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.cts +44 -0
- package/build/legacy/usePrefetchInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.ts +44 -0
- package/build/legacy/usePrefetchInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.js +44 -0
- package/build/legacy/usePrefetchInfiniteQuery.js.map +1 -1
- package/build/legacy/usePrefetchQuery.cjs +36 -1
- package/build/legacy/usePrefetchQuery.cjs.map +1 -1
- package/build/legacy/usePrefetchQuery.d.cts +35 -0
- package/build/legacy/usePrefetchQuery.d.cts.map +1 -1
- package/build/legacy/usePrefetchQuery.d.ts +35 -0
- package/build/legacy/usePrefetchQuery.d.ts.map +1 -1
- package/build/legacy/usePrefetchQuery.js +36 -1
- package/build/legacy/usePrefetchQuery.js.map +1 -1
- package/build/legacy/useQueries.cjs +50 -0
- package/build/legacy/useQueries.cjs.map +1 -1
- package/build/legacy/useQueries.d.cts +85 -2
- package/build/legacy/useQueries.d.cts.map +1 -1
- package/build/legacy/useQueries.d.ts +85 -2
- package/build/legacy/useQueries.d.ts.map +1 -1
- package/build/legacy/useQueries.js +50 -0
- package/build/legacy/useQueries.js.map +1 -1
- package/build/legacy/useQuery.cjs.map +1 -1
- package/build/legacy/useQuery.d.cts +134 -0
- package/build/legacy/useQuery.d.cts.map +1 -1
- package/build/legacy/useQuery.d.ts +134 -0
- package/build/legacy/useQuery.d.ts.map +1 -1
- package/build/legacy/useQuery.js.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.cjs +48 -0
- package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.cts +48 -0
- package/build/legacy/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.ts +48 -0
- package/build/legacy/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.js +48 -0
- package/build/legacy/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/legacy/useSuspenseQueries.cjs.map +1 -1
- package/build/legacy/useSuspenseQueries.d.cts +128 -2
- package/build/legacy/useSuspenseQueries.d.cts.map +1 -1
- package/build/legacy/useSuspenseQueries.d.ts +128 -2
- package/build/legacy/useSuspenseQueries.d.ts.map +1 -1
- package/build/legacy/useSuspenseQueries.js.map +1 -1
- package/build/legacy/useSuspenseQuery.cjs +43 -0
- package/build/legacy/useSuspenseQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseQuery.d.cts +43 -0
- package/build/legacy/useSuspenseQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseQuery.d.ts +43 -0
- package/build/legacy/useSuspenseQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseQuery.js +43 -0
- package/build/legacy/useSuspenseQuery.js.map +1 -1
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts.map +1 -1
- package/build/legacy/utils.d.ts.map +1 -1
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/HydrationBoundary.cjs +43 -0
- package/build/modern/HydrationBoundary.cjs.map +1 -1
- package/build/modern/HydrationBoundary.d.cts +61 -0
- package/build/modern/HydrationBoundary.d.cts.map +1 -1
- package/build/modern/HydrationBoundary.d.ts +61 -0
- package/build/modern/HydrationBoundary.d.ts.map +1 -1
- package/build/modern/HydrationBoundary.js +43 -0
- package/build/modern/HydrationBoundary.js.map +1 -1
- package/build/modern/IsRestoringProvider.cjs +11 -0
- package/build/modern/IsRestoringProvider.cjs.map +1 -1
- package/build/modern/IsRestoringProvider.d.cts +11 -0
- package/build/modern/IsRestoringProvider.d.cts.map +1 -1
- package/build/modern/IsRestoringProvider.d.ts +11 -0
- package/build/modern/IsRestoringProvider.d.ts.map +1 -1
- package/build/modern/IsRestoringProvider.js +11 -0
- package/build/modern/IsRestoringProvider.js.map +1 -1
- package/build/modern/QueryClientProvider.cjs +30 -0
- package/build/modern/QueryClientProvider.cjs.map +1 -1
- package/build/modern/QueryClientProvider.d.cts +41 -0
- package/build/modern/QueryClientProvider.d.cts.map +1 -1
- package/build/modern/QueryClientProvider.d.ts +41 -0
- package/build/modern/QueryClientProvider.d.ts.map +1 -1
- package/build/modern/QueryClientProvider.js +30 -0
- package/build/modern/QueryClientProvider.js.map +1 -1
- package/build/modern/QueryErrorResetBoundary.cjs +75 -0
- package/build/modern/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/modern/QueryErrorResetBoundary.d.cts +106 -0
- package/build/modern/QueryErrorResetBoundary.d.cts.map +1 -1
- package/build/modern/QueryErrorResetBoundary.d.ts +106 -0
- package/build/modern/QueryErrorResetBoundary.d.ts.map +1 -1
- package/build/modern/QueryErrorResetBoundary.js +75 -0
- package/build/modern/QueryErrorResetBoundary.js.map +1 -1
- package/build/modern/infiniteQueryOptions.cjs.map +1 -1
- package/build/modern/infiniteQueryOptions.d.cts +187 -0
- package/build/modern/infiniteQueryOptions.d.cts.map +1 -1
- package/build/modern/infiniteQueryOptions.d.ts +187 -0
- package/build/modern/infiniteQueryOptions.d.ts.map +1 -1
- package/build/modern/infiniteQueryOptions.js.map +1 -1
- package/build/modern/mutationOptions.cjs.map +1 -1
- package/build/modern/mutationOptions.d.cts +68 -0
- package/build/modern/mutationOptions.d.cts.map +1 -1
- package/build/modern/mutationOptions.d.ts +68 -0
- package/build/modern/mutationOptions.d.ts.map +1 -1
- package/build/modern/mutationOptions.js.map +1 -1
- package/build/modern/queryOptions.cjs.map +1 -1
- package/build/modern/queryOptions.d.cts +198 -0
- package/build/modern/queryOptions.d.cts.map +1 -1
- package/build/modern/queryOptions.d.ts +198 -0
- package/build/modern/queryOptions.d.ts.map +1 -1
- package/build/modern/queryOptions.js.map +1 -1
- package/build/modern/types.cjs.map +1 -1
- package/build/modern/types.d.cts +228 -2
- package/build/modern/types.d.cts.map +1 -1
- package/build/modern/types.d.ts +228 -2
- package/build/modern/types.d.ts.map +1 -1
- package/build/modern/types.js.map +1 -1
- package/build/modern/useInfiniteQuery.cjs.map +1 -1
- package/build/modern/useInfiniteQuery.d.cts +116 -0
- package/build/modern/useInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useInfiniteQuery.d.ts +116 -0
- package/build/modern/useInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useInfiniteQuery.js.map +1 -1
- package/build/modern/useIsFetching.cjs +20 -0
- package/build/modern/useIsFetching.cjs.map +1 -1
- package/build/modern/useIsFetching.d.cts +20 -0
- package/build/modern/useIsFetching.d.cts.map +1 -1
- package/build/modern/useIsFetching.d.ts +20 -0
- package/build/modern/useIsFetching.d.ts.map +1 -1
- package/build/modern/useIsFetching.js +20 -0
- package/build/modern/useIsFetching.js.map +1 -1
- package/build/modern/useMutation.cjs +67 -0
- package/build/modern/useMutation.cjs.map +1 -1
- package/build/modern/useMutation.d.cts +67 -0
- package/build/modern/useMutation.d.cts.map +1 -1
- package/build/modern/useMutation.d.ts +67 -0
- package/build/modern/useMutation.d.ts.map +1 -1
- package/build/modern/useMutation.js +67 -0
- package/build/modern/useMutation.js.map +1 -1
- package/build/modern/useMutationState.cjs +74 -0
- package/build/modern/useMutationState.cjs.map +1 -1
- package/build/modern/useMutationState.d.cts +74 -0
- package/build/modern/useMutationState.d.cts.map +1 -1
- package/build/modern/useMutationState.d.ts +74 -0
- package/build/modern/useMutationState.d.ts.map +1 -1
- package/build/modern/useMutationState.js +74 -0
- package/build/modern/useMutationState.js.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.cjs +44 -0
- package/build/modern/usePrefetchInfiniteQuery.cjs.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.cts +44 -0
- package/build/modern/usePrefetchInfiniteQuery.d.cts.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.ts +44 -0
- package/build/modern/usePrefetchInfiniteQuery.d.ts.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.js +44 -0
- package/build/modern/usePrefetchInfiniteQuery.js.map +1 -1
- package/build/modern/usePrefetchQuery.cjs +36 -1
- package/build/modern/usePrefetchQuery.cjs.map +1 -1
- package/build/modern/usePrefetchQuery.d.cts +35 -0
- package/build/modern/usePrefetchQuery.d.cts.map +1 -1
- package/build/modern/usePrefetchQuery.d.ts +35 -0
- package/build/modern/usePrefetchQuery.d.ts.map +1 -1
- package/build/modern/usePrefetchQuery.js +36 -1
- package/build/modern/usePrefetchQuery.js.map +1 -1
- package/build/modern/useQueries.cjs +50 -0
- package/build/modern/useQueries.cjs.map +1 -1
- package/build/modern/useQueries.d.cts +85 -2
- package/build/modern/useQueries.d.cts.map +1 -1
- package/build/modern/useQueries.d.ts +85 -2
- package/build/modern/useQueries.d.ts.map +1 -1
- package/build/modern/useQueries.js +50 -0
- package/build/modern/useQueries.js.map +1 -1
- package/build/modern/useQuery.cjs.map +1 -1
- package/build/modern/useQuery.d.cts +134 -0
- package/build/modern/useQuery.d.cts.map +1 -1
- package/build/modern/useQuery.d.ts +134 -0
- package/build/modern/useQuery.d.ts.map +1 -1
- package/build/modern/useQuery.js.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.cjs +48 -0
- package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.cts +48 -0
- package/build/modern/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.ts +48 -0
- package/build/modern/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.js +48 -0
- package/build/modern/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/modern/useSuspenseQueries.cjs.map +1 -1
- package/build/modern/useSuspenseQueries.d.cts +128 -2
- package/build/modern/useSuspenseQueries.d.cts.map +1 -1
- package/build/modern/useSuspenseQueries.d.ts +128 -2
- package/build/modern/useSuspenseQueries.d.ts.map +1 -1
- package/build/modern/useSuspenseQueries.js.map +1 -1
- package/build/modern/useSuspenseQuery.cjs +43 -0
- package/build/modern/useSuspenseQuery.cjs.map +1 -1
- package/build/modern/useSuspenseQuery.d.cts +43 -0
- package/build/modern/useSuspenseQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseQuery.d.ts +43 -0
- package/build/modern/useSuspenseQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseQuery.js +43 -0
- package/build/modern/useSuspenseQuery.js.map +1 -1
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts.map +1 -1
- package/build/modern/utils.d.ts.map +1 -1
- package/build/modern/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/HydrationBoundary.tsx +61 -1
- package/src/IsRestoringProvider.ts +12 -0
- package/src/QueryClientProvider.tsx +41 -0
- package/src/QueryErrorResetBoundary.tsx +109 -0
- package/src/infiniteQueryOptions.ts +187 -0
- package/src/mutationOptions.ts +68 -0
- package/src/queryOptions.ts +198 -0
- package/src/types.ts +228 -2
- package/src/useInfiniteQuery.ts +116 -0
- package/src/useIsFetching.ts +20 -0
- package/src/useMutation.ts +67 -0
- package/src/useMutationState.ts +74 -0
- package/src/usePrefetchInfiniteQuery.tsx +44 -0
- package/src/usePrefetchQuery.tsx +36 -1
- package/src/useQueries.ts +85 -2
- package/src/useQuery.ts +134 -0
- package/src/useSuspenseInfiniteQuery.ts +48 -0
- package/src/useSuspenseQueries.ts +128 -2
- package/src/useSuspenseQuery.ts +43 -0
- package/src/utils.ts +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.js","names":[],"sources":["../../src/useMutationState.ts"],"sourcesContent":["import { notifyManager, replaceEqualDeep } from '@tanstack/query-core'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n QueryClient,\n} from '@tanstack/query-core'\nimport { useCallback, useEffect, useRef } from 'preact/hooks'\n\nimport { useQueryClient } from './QueryClientProvider'\nimport { useSyncExternalStore } from './utils'\n\nexport function useIsMutating(\n filters?: MutationFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n return useMutationState(\n { filters: { ...filters, status: 'pending' } },\n client,\n ).length\n}\n\ntype MutationTypeFromResult<TResult> = [TResult] extends [\n MutationState<\n infer TData,\n infer TError,\n infer TVariables,\n infer TOnMutateResult\n >,\n]\n ? Mutation<TData, TError, TVariables, TOnMutateResult>\n : Mutation\n\ntype MutationStateOptions<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n> = {\n filters?: MutationFilters\n select?: (mutation: TMutation) => TResult\n}\n\nfunction getResult<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n mutationCache: MutationCache,\n options: MutationStateOptions<TResult, TMutation>,\n): Array<TResult> {\n return mutationCache\n .findAll(options.filters)\n .map(\n (mutation): TResult =>\n (options.select\n ? options.select(mutation as TMutation)\n : mutation.state) as TResult,\n )\n}\n\nexport function useMutationState<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n options: MutationStateOptions<TResult, TMutation> = {},\n queryClient?: QueryClient,\n): Array<TResult> {\n const mutationCache = useQueryClient(queryClient).getMutationCache()\n const optionsRef = useRef(options)\n const result = useRef<Array<TResult>>(null)\n if (result.current === null) {\n result.current = getResult(mutationCache, options)\n }\n\n useEffect(() => {\n optionsRef.current = options\n })\n\n return useSyncExternalStore(\n useCallback(\n (onStoreChange) =>\n mutationCache.subscribe(() => {\n const nextResult = replaceEqualDeep(\n result.current,\n getResult(mutationCache, optionsRef.current),\n )\n if (result.current !== nextResult) {\n result.current = nextResult\n notifyManager.schedule(onStoreChange)\n }\n }),\n [mutationCache],\n ),\n () => result.current,\n )!\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useMutationState.js","names":[],"sources":["../../src/useMutationState.ts"],"sourcesContent":["import { notifyManager, replaceEqualDeep } from '@tanstack/query-core'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n QueryClient,\n} from '@tanstack/query-core'\nimport { useCallback, useEffect, useRef } from 'preact/hooks'\n\nimport { useQueryClient } from './QueryClientProvider'\nimport { useSyncExternalStore } from './utils'\n\n/**\n * `useIsMutating` is an optional hook that returns the `number` of mutations that your application is fetching\n * (useful for app-wide loading indicators).\n *\n * @param filters - The {@link MutationFilters} to narrow down the matched mutations.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be the `number` of the mutations that your application is currently fetching.\n *\n * @example\n * ```tsx\n * import { useIsMutating } from '@tanstack/preact-query'\n *\n * // How many mutations are fetching?\n * const isMutating = useIsMutating()\n * // How many mutations matching the posts prefix are fetching?\n * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })\n * ```\n */\nexport function useIsMutating(\n filters?: MutationFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n return useMutationState(\n { filters: { ...filters, status: 'pending' } },\n client,\n ).length\n}\n\ntype MutationTypeFromResult<TResult> = [TResult] extends [\n MutationState<\n infer TData,\n infer TError,\n infer TVariables,\n infer TOnMutateResult\n >,\n]\n ? Mutation<TData, TError, TVariables, TOnMutateResult>\n : Mutation\n\ntype MutationStateOptions<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n> = {\n filters?: MutationFilters\n select?: (mutation: TMutation) => TResult\n}\n\nfunction getResult<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n mutationCache: MutationCache,\n options: MutationStateOptions<TResult, TMutation>,\n): Array<TResult> {\n return mutationCache\n .findAll(options.filters)\n .map(\n (mutation): TResult =>\n (options.select\n ? options.select(mutation as TMutation)\n : mutation.state) as TResult,\n )\n}\n\n/**\n * `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass\n * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation\n * state.\n *\n * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the\n * mutation state.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be an Array of whatever `select` returns for each matching mutation.\n *\n * @example\n * Get all variables of all running mutations:\n * ```tsx\n * import { useMutationState } from '@tanstack/preact-query'\n *\n * const variables = useMutationState({\n * filters: { status: 'pending' },\n * select: (mutation) => mutation.state.variables,\n * })\n * ```\n *\n * @example\n * Get all data for specific mutations via the `mutationKey`:\n * ```tsx\n * import { useMutation, useMutationState } from '@tanstack/preact-query'\n *\n * const mutationKey = ['posts']\n *\n * // Some mutation that we want to get the state for\n * const mutation = useMutation({\n * mutationKey,\n * mutationFn: createPosts,\n * })\n *\n * const data = useMutationState({\n * // this mutation key needs to match the mutation key of the given mutation (see above)\n * filters: { mutationKey },\n * select: (mutation) => mutation.state.data,\n * })\n * ```\n *\n * @example\n * Access the latest mutation data via the `mutationKey`. Each invocation of `mutate` adds a new entry to the\n * mutation cache for `gcTime` milliseconds — check the last item that `useMutationState` returns to get the\n * latest invocation:\n * ```tsx\n * const data = useMutationState({\n * filters: { mutationKey: ['posts'] },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * const latest = data[data.length - 1]\n * ```\n */\nexport function useMutationState<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n options: MutationStateOptions<TResult, TMutation> = {},\n queryClient?: QueryClient,\n): Array<TResult> {\n const mutationCache = useQueryClient(queryClient).getMutationCache()\n const optionsRef = useRef(options)\n const result = useRef<Array<TResult>>(null)\n if (result.current === null) {\n result.current = getResult(mutationCache, options)\n }\n\n useEffect(() => {\n optionsRef.current = options\n })\n\n return useSyncExternalStore(\n useCallback(\n (onStoreChange) =>\n mutationCache.subscribe(() => {\n const nextResult = replaceEqualDeep(\n result.current,\n getResult(mutationCache, optionsRef.current),\n )\n if (result.current !== nextResult) {\n result.current = nextResult\n notifyManager.schedule(onStoreChange)\n }\n }),\n [mutationCache],\n ),\n () => result.current,\n )!\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,cACd,SACA,aACQ;CACR,MAAM,SAAS,eAAe,WAAW;CACzC,OAAO,iBACL,EAAE,SAAS;EAAE,GAAG;EAAS,QAAQ;CAAU,EAAE,GAC7C,MACF,CAAC,CAAC;AACJ;AAsBA,SAAS,UAKP,eACA,SACgB;CAChB,OAAO,cACJ,QAAQ,QAAQ,OAAO,CAAC,CACxB,KACE,aACE,QAAQ,SACL,QAAQ,OAAO,QAAqB,IACpC,SAAS,KACjB;AACJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,SAAgB,iBAKd,UAAoD,CAAC,GACrD,aACgB;CAChB,MAAM,gBAAgB,eAAe,WAAW,CAAC,CAAC,iBAAiB;CACnE,MAAM,aAAa,OAAO,OAAO;CACjC,MAAM,SAAS,OAAuB,IAAI;CAC1C,IAAI,OAAO,YAAY,MACrB,OAAO,UAAU,UAAU,eAAe,OAAO;CAGnD,gBAAgB;EACd,WAAW,UAAU;CACvB,CAAC;CAED,OAAO,qBACL,aACG,kBACC,cAAc,gBAAgB;EAC5B,MAAM,aAAa,iBACjB,OAAO,SACP,UAAU,eAAe,WAAW,OAAO,CAC7C;EACA,IAAI,OAAO,YAAY,YAAY;GACjC,OAAO,UAAU;GACjB,cAAc,SAAS,aAAa;EACtC;CACF,CAAC,GACH,CAAC,aAAa,CAChB,SACM,OAAO,OACf;AACF"}
|
|
@@ -2,6 +2,50 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_QueryClientProvider = require("./QueryClientProvider.cjs");
|
|
3
3
|
let _tanstack_query_core = require("@tanstack/query-core");
|
|
4
4
|
//#region src/usePrefetchInfiniteQuery.tsx
|
|
5
|
+
/**
|
|
6
|
+
* `usePrefetchInfiniteQuery` does not return anything, it should be used just to fire a prefetch during render,
|
|
7
|
+
* before a suspense boundary that wraps a component that uses `useSuspenseInfiniteQuery`. You can pass
|
|
8
|
+
* everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`, though
|
|
9
|
+
* `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required unless
|
|
10
|
+
* a default query function has been defined.
|
|
11
|
+
*
|
|
12
|
+
* `getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,
|
|
13
|
+
* as well as pageParam information, and should return a single variable that will be passed to your query
|
|
14
|
+
* function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.
|
|
15
|
+
*
|
|
16
|
+
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
17
|
+
* over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
|
|
18
|
+
* already there or already in flight.
|
|
19
|
+
*
|
|
20
|
+
* @param options - The {@link UsePrefetchInfiniteQueryOptions} to use — everything you can pass to `queryClient.infiniteQuery`.
|
|
21
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
22
|
+
* be used.
|
|
23
|
+
* @returns `void` — nothing is returned.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* import { Suspense } from 'preact/compat'
|
|
28
|
+
* import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/preact-query'
|
|
29
|
+
*
|
|
30
|
+
* const projectsOptions = infiniteQueryOptions({
|
|
31
|
+
* queryKey: ['projects'],
|
|
32
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
33
|
+
* initialPageParam: 0,
|
|
34
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
35
|
+
* })
|
|
36
|
+
*
|
|
37
|
+
* function App() {
|
|
38
|
+
* // Fire the prefetch during render, before the suspense boundary below.
|
|
39
|
+
* usePrefetchInfiniteQuery(projectsOptions)
|
|
40
|
+
*
|
|
41
|
+
* return (
|
|
42
|
+
* <Suspense fallback={<h1>Loading projects...</h1>}>
|
|
43
|
+
* <Projects />
|
|
44
|
+
* </Suspense>
|
|
45
|
+
* )
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
5
49
|
function usePrefetchInfiniteQuery(options, queryClient) {
|
|
6
50
|
const client = require_QueryClientProvider.useQueryClient(queryClient);
|
|
7
51
|
if (!client.getQueryState(options.queryKey)) client.infiniteQuery(options).catch(_tanstack_query_core.noop);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePrefetchInfiniteQuery.cjs","names":["useQueryClient","noop"],"sources":["../../src/usePrefetchInfiniteQuery.tsx"],"sourcesContent":["import { noop } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\n\nimport type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'\nimport type { UsePrefetchInfiniteQueryOptions } from './types'\n\nexport function usePrefetchInfiniteQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n options: UsePrefetchInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n queryClient?: QueryClient,\n) {\n const client = useQueryClient(queryClient)\n\n if (!client.getQueryState(options.queryKey)) {\n void client.infiniteQuery(options).catch(noop)\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePrefetchInfiniteQuery.cjs","names":["useQueryClient","noop"],"sources":["../../src/usePrefetchInfiniteQuery.tsx"],"sourcesContent":["import { noop } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\n\nimport type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'\nimport type { UsePrefetchInfiniteQueryOptions } from './types'\n\n/**\n * `usePrefetchInfiniteQuery` does not return anything, it should be used just to fire a prefetch during render,\n * before a suspense boundary that wraps a component that uses `useSuspenseInfiniteQuery`. You can pass\n * everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`, though\n * `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required unless\n * a default query function has been defined.\n *\n * `getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,\n * as well as pageParam information, and should return a single variable that will be passed to your query\n * function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.\n *\n * The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left\n * over from a previous attempt — so calling this on every render is cheap and won't refetch data that's\n * already there or already in flight.\n *\n * @param options - The {@link UsePrefetchInfiniteQueryOptions} to use — everything you can pass to `queryClient.infiniteQuery`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns `void` — nothing is returned.\n *\n * @example\n * ```tsx\n * import { Suspense } from 'preact/compat'\n * import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/preact-query'\n *\n * const projectsOptions = infiniteQueryOptions({\n * queryKey: ['projects'],\n * queryFn: ({ pageParam }) => fetchProjects(pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * })\n *\n * function App() {\n * // Fire the prefetch during render, before the suspense boundary below.\n * usePrefetchInfiniteQuery(projectsOptions)\n *\n * return (\n * <Suspense fallback={<h1>Loading projects...</h1>}>\n * <Projects />\n * </Suspense>\n * )\n * }\n * ```\n */\nexport function usePrefetchInfiniteQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n options: UsePrefetchInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n queryClient?: QueryClient,\n) {\n const client = useQueryClient(queryClient)\n\n if (!client.getQueryState(options.queryKey)) {\n void client.infiniteQuery(options).catch(noop)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDA,SAAgB,yBAOd,SAOA,aACA;CACA,MAAM,SAASA,4BAAAA,eAAe,WAAW;CAEzC,IAAI,CAAC,OAAO,cAAc,QAAQ,QAAQ,GACxC,OAAY,cAAc,OAAO,CAAC,CAAC,MAAMC,qBAAAA,IAAI;AAEjD"}
|
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
import { UsePrefetchInfiniteQueryOptions } from "./types.cjs";
|
|
2
2
|
import { DefaultError, QueryClient, QueryKey } from "@tanstack/query-core";
|
|
3
3
|
//#region src/usePrefetchInfiniteQuery.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* `usePrefetchInfiniteQuery` does not return anything, it should be used just to fire a prefetch during render,
|
|
6
|
+
* before a suspense boundary that wraps a component that uses `useSuspenseInfiniteQuery`. You can pass
|
|
7
|
+
* everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`, though
|
|
8
|
+
* `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required unless
|
|
9
|
+
* a default query function has been defined.
|
|
10
|
+
*
|
|
11
|
+
* `getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,
|
|
12
|
+
* as well as pageParam information, and should return a single variable that will be passed to your query
|
|
13
|
+
* function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.
|
|
14
|
+
*
|
|
15
|
+
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
16
|
+
* over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
|
|
17
|
+
* already there or already in flight.
|
|
18
|
+
*
|
|
19
|
+
* @param options - The {@link UsePrefetchInfiniteQueryOptions} to use — everything you can pass to `queryClient.infiniteQuery`.
|
|
20
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
21
|
+
* be used.
|
|
22
|
+
* @returns `void` — nothing is returned.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* import { Suspense } from 'preact/compat'
|
|
27
|
+
* import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/preact-query'
|
|
28
|
+
*
|
|
29
|
+
* const projectsOptions = infiniteQueryOptions({
|
|
30
|
+
* queryKey: ['projects'],
|
|
31
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
32
|
+
* initialPageParam: 0,
|
|
33
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
34
|
+
* })
|
|
35
|
+
*
|
|
36
|
+
* function App() {
|
|
37
|
+
* // Fire the prefetch during render, before the suspense boundary below.
|
|
38
|
+
* usePrefetchInfiniteQuery(projectsOptions)
|
|
39
|
+
*
|
|
40
|
+
* return (
|
|
41
|
+
* <Suspense fallback={<h1>Loading projects...</h1>}>
|
|
42
|
+
* <Projects />
|
|
43
|
+
* </Suspense>
|
|
44
|
+
* )
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
4
48
|
declare function usePrefetchInfiniteQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(options: UsePrefetchInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: QueryClient): void;
|
|
5
49
|
//#endregion
|
|
6
50
|
export { usePrefetchInfiniteQuery };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePrefetchInfiniteQuery.d.cts","names":[],"sources":["../../src/usePrefetchInfiniteQuery.tsx"],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePrefetchInfiniteQuery.d.cts","names":[],"sources":["../../src/usePrefetchInfiniteQuery.tsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAkDgB,yBACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAC7B,sBAEA,SAAS,gCACP,cACA,QACA,OACA,WACA,aAEF,cAAc"}
|
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
import { UsePrefetchInfiniteQueryOptions } from "./types.js";
|
|
2
2
|
import { DefaultError, QueryClient, QueryKey } from "@tanstack/query-core";
|
|
3
3
|
//#region src/usePrefetchInfiniteQuery.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* `usePrefetchInfiniteQuery` does not return anything, it should be used just to fire a prefetch during render,
|
|
6
|
+
* before a suspense boundary that wraps a component that uses `useSuspenseInfiniteQuery`. You can pass
|
|
7
|
+
* everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`, though
|
|
8
|
+
* `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required unless
|
|
9
|
+
* a default query function has been defined.
|
|
10
|
+
*
|
|
11
|
+
* `getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,
|
|
12
|
+
* as well as pageParam information, and should return a single variable that will be passed to your query
|
|
13
|
+
* function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.
|
|
14
|
+
*
|
|
15
|
+
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
16
|
+
* over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
|
|
17
|
+
* already there or already in flight.
|
|
18
|
+
*
|
|
19
|
+
* @param options - The {@link UsePrefetchInfiniteQueryOptions} to use — everything you can pass to `queryClient.infiniteQuery`.
|
|
20
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
21
|
+
* be used.
|
|
22
|
+
* @returns `void` — nothing is returned.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* import { Suspense } from 'preact/compat'
|
|
27
|
+
* import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/preact-query'
|
|
28
|
+
*
|
|
29
|
+
* const projectsOptions = infiniteQueryOptions({
|
|
30
|
+
* queryKey: ['projects'],
|
|
31
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
32
|
+
* initialPageParam: 0,
|
|
33
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
34
|
+
* })
|
|
35
|
+
*
|
|
36
|
+
* function App() {
|
|
37
|
+
* // Fire the prefetch during render, before the suspense boundary below.
|
|
38
|
+
* usePrefetchInfiniteQuery(projectsOptions)
|
|
39
|
+
*
|
|
40
|
+
* return (
|
|
41
|
+
* <Suspense fallback={<h1>Loading projects...</h1>}>
|
|
42
|
+
* <Projects />
|
|
43
|
+
* </Suspense>
|
|
44
|
+
* )
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
4
48
|
declare function usePrefetchInfiniteQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(options: UsePrefetchInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: QueryClient): void;
|
|
5
49
|
//#endregion
|
|
6
50
|
export { usePrefetchInfiniteQuery };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePrefetchInfiniteQuery.d.ts","names":[],"sources":["../../src/usePrefetchInfiniteQuery.tsx"],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePrefetchInfiniteQuery.d.ts","names":[],"sources":["../../src/usePrefetchInfiniteQuery.tsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAkDgB,yBACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAC7B,sBAEA,SAAS,gCACP,cACA,QACA,OACA,WACA,aAEF,cAAc"}
|
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
import { useQueryClient } from "./QueryClientProvider.js";
|
|
2
2
|
import { noop } from "@tanstack/query-core";
|
|
3
3
|
//#region src/usePrefetchInfiniteQuery.tsx
|
|
4
|
+
/**
|
|
5
|
+
* `usePrefetchInfiniteQuery` does not return anything, it should be used just to fire a prefetch during render,
|
|
6
|
+
* before a suspense boundary that wraps a component that uses `useSuspenseInfiniteQuery`. You can pass
|
|
7
|
+
* everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`, though
|
|
8
|
+
* `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required unless
|
|
9
|
+
* a default query function has been defined.
|
|
10
|
+
*
|
|
11
|
+
* `getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,
|
|
12
|
+
* as well as pageParam information, and should return a single variable that will be passed to your query
|
|
13
|
+
* function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.
|
|
14
|
+
*
|
|
15
|
+
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
16
|
+
* over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
|
|
17
|
+
* already there or already in flight.
|
|
18
|
+
*
|
|
19
|
+
* @param options - The {@link UsePrefetchInfiniteQueryOptions} to use — everything you can pass to `queryClient.infiniteQuery`.
|
|
20
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
21
|
+
* be used.
|
|
22
|
+
* @returns `void` — nothing is returned.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* import { Suspense } from 'preact/compat'
|
|
27
|
+
* import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/preact-query'
|
|
28
|
+
*
|
|
29
|
+
* const projectsOptions = infiniteQueryOptions({
|
|
30
|
+
* queryKey: ['projects'],
|
|
31
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
32
|
+
* initialPageParam: 0,
|
|
33
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
34
|
+
* })
|
|
35
|
+
*
|
|
36
|
+
* function App() {
|
|
37
|
+
* // Fire the prefetch during render, before the suspense boundary below.
|
|
38
|
+
* usePrefetchInfiniteQuery(projectsOptions)
|
|
39
|
+
*
|
|
40
|
+
* return (
|
|
41
|
+
* <Suspense fallback={<h1>Loading projects...</h1>}>
|
|
42
|
+
* <Projects />
|
|
43
|
+
* </Suspense>
|
|
44
|
+
* )
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
4
48
|
function usePrefetchInfiniteQuery(options, queryClient) {
|
|
5
49
|
const client = useQueryClient(queryClient);
|
|
6
50
|
if (!client.getQueryState(options.queryKey)) client.infiniteQuery(options).catch(noop);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePrefetchInfiniteQuery.js","names":[],"sources":["../../src/usePrefetchInfiniteQuery.tsx"],"sourcesContent":["import { noop } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\n\nimport type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'\nimport type { UsePrefetchInfiniteQueryOptions } from './types'\n\nexport function usePrefetchInfiniteQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n options: UsePrefetchInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n queryClient?: QueryClient,\n) {\n const client = useQueryClient(queryClient)\n\n if (!client.getQueryState(options.queryKey)) {\n void client.infiniteQuery(options).catch(noop)\n }\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePrefetchInfiniteQuery.js","names":[],"sources":["../../src/usePrefetchInfiniteQuery.tsx"],"sourcesContent":["import { noop } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\n\nimport type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'\nimport type { UsePrefetchInfiniteQueryOptions } from './types'\n\n/**\n * `usePrefetchInfiniteQuery` does not return anything, it should be used just to fire a prefetch during render,\n * before a suspense boundary that wraps a component that uses `useSuspenseInfiniteQuery`. You can pass\n * everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`, though\n * `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required unless\n * a default query function has been defined.\n *\n * `getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,\n * as well as pageParam information, and should return a single variable that will be passed to your query\n * function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.\n *\n * The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left\n * over from a previous attempt — so calling this on every render is cheap and won't refetch data that's\n * already there or already in flight.\n *\n * @param options - The {@link UsePrefetchInfiniteQueryOptions} to use — everything you can pass to `queryClient.infiniteQuery`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns `void` — nothing is returned.\n *\n * @example\n * ```tsx\n * import { Suspense } from 'preact/compat'\n * import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/preact-query'\n *\n * const projectsOptions = infiniteQueryOptions({\n * queryKey: ['projects'],\n * queryFn: ({ pageParam }) => fetchProjects(pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * })\n *\n * function App() {\n * // Fire the prefetch during render, before the suspense boundary below.\n * usePrefetchInfiniteQuery(projectsOptions)\n *\n * return (\n * <Suspense fallback={<h1>Loading projects...</h1>}>\n * <Projects />\n * </Suspense>\n * )\n * }\n * ```\n */\nexport function usePrefetchInfiniteQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n options: UsePrefetchInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n queryClient?: QueryClient,\n) {\n const client = useQueryClient(queryClient)\n\n if (!client.getQueryState(options.queryKey)) {\n void client.infiniteQuery(options).catch(noop)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkDA,SAAgB,yBAOd,SAOA,aACA;CACA,MAAM,SAAS,eAAe,WAAW;CAEzC,IAAI,CAAC,OAAO,cAAc,QAAQ,QAAQ,GACxC,OAAY,cAAc,OAAO,CAAC,CAAC,MAAM,IAAI;AAEjD"}
|
|
@@ -2,9 +2,44 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_QueryClientProvider = require("./QueryClientProvider.cjs");
|
|
3
3
|
let _tanstack_query_core = require("@tanstack/query-core");
|
|
4
4
|
//#region src/usePrefetchQuery.tsx
|
|
5
|
+
/**
|
|
6
|
+
* `usePrefetchQuery` does not return anything, it should be used just to fire a prefetch during render, before
|
|
7
|
+
* a suspense boundary that wraps a component that uses `useSuspenseQuery`. You can pass everything to
|
|
8
|
+
* `usePrefetchQuery` that you can pass to `queryClient.query`, though `queryKey` is always required, and
|
|
9
|
+
* `queryFn` is required unless a default query function has been defined.
|
|
10
|
+
*
|
|
11
|
+
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
12
|
+
* over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
|
|
13
|
+
* already there or already in flight.
|
|
14
|
+
*
|
|
15
|
+
* @param options - The {@link UsePrefetchQueryOptions} to use — everything you can pass to `queryClient.query`.
|
|
16
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
17
|
+
* be used.
|
|
18
|
+
* @returns `void` — nothing is returned.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* import { Suspense } from 'preact/compat'
|
|
23
|
+
* import { usePrefetchQuery } from '@tanstack/preact-query'
|
|
24
|
+
*
|
|
25
|
+
* function App() {
|
|
26
|
+
* // Fire the prefetch during render, before the suspense boundary below.
|
|
27
|
+
* usePrefetchQuery({
|
|
28
|
+
* queryKey: ['posts'],
|
|
29
|
+
* queryFn: fetchPosts,
|
|
30
|
+
* })
|
|
31
|
+
*
|
|
32
|
+
* return (
|
|
33
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
34
|
+
* <Posts />
|
|
35
|
+
* </Suspense>
|
|
36
|
+
* )
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
5
40
|
function usePrefetchQuery(options, queryClient) {
|
|
6
41
|
const client = require_QueryClientProvider.useQueryClient(queryClient);
|
|
7
|
-
if (!client.getQueryState(options.queryKey)) client.
|
|
42
|
+
if (!client.getQueryState(options.queryKey)) client.query(options).catch(_tanstack_query_core.noop);
|
|
8
43
|
}
|
|
9
44
|
//#endregion
|
|
10
45
|
exports.usePrefetchQuery = usePrefetchQuery;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePrefetchQuery.cjs","names":["useQueryClient","noop"],"sources":["../../src/usePrefetchQuery.tsx"],"sourcesContent":["import { noop } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\n\nimport type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'\nimport type { UsePrefetchQueryOptions } from './types'\n\nexport function usePrefetchQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UsePrefetchQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n queryClient?: QueryClient,\n) {\n const client = useQueryClient(queryClient)\n\n if (!client.getQueryState(options.queryKey)) {\n void client.
|
|
1
|
+
{"version":3,"file":"usePrefetchQuery.cjs","names":["useQueryClient","noop"],"sources":["../../src/usePrefetchQuery.tsx"],"sourcesContent":["import { noop } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\n\nimport type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'\nimport type { UsePrefetchQueryOptions } from './types'\n\n/**\n * `usePrefetchQuery` does not return anything, it should be used just to fire a prefetch during render, before\n * a suspense boundary that wraps a component that uses `useSuspenseQuery`. You can pass everything to\n * `usePrefetchQuery` that you can pass to `queryClient.query`, though `queryKey` is always required, and\n * `queryFn` is required unless a default query function has been defined.\n *\n * The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left\n * over from a previous attempt — so calling this on every render is cheap and won't refetch data that's\n * already there or already in flight.\n *\n * @param options - The {@link UsePrefetchQueryOptions} to use — everything you can pass to `queryClient.query`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns `void` — nothing is returned.\n *\n * @example\n * ```tsx\n * import { Suspense } from 'preact/compat'\n * import { usePrefetchQuery } from '@tanstack/preact-query'\n *\n * function App() {\n * // Fire the prefetch during render, before the suspense boundary below.\n * usePrefetchQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * return (\n * <Suspense fallback={<h1>Loading posts...</h1>}>\n * <Posts />\n * </Suspense>\n * )\n * }\n * ```\n */\nexport function usePrefetchQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UsePrefetchQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n queryClient?: QueryClient,\n) {\n const client = useQueryClient(queryClient)\n\n if (!client.getQueryState(options.queryKey)) {\n void client.query(options).catch(noop)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,SAAgB,iBAOd,SAOA,aACA;CACA,MAAM,SAASA,4BAAAA,eAAe,WAAW;CAEzC,IAAI,CAAC,OAAO,cAAc,QAAQ,QAAQ,GACxC,OAAY,MAAM,OAAO,CAAC,CAAC,MAAMC,qBAAAA,IAAI;AAEzC"}
|
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
import { UsePrefetchQueryOptions } from "./types.cjs";
|
|
2
2
|
import { DefaultError, QueryClient, QueryKey } from "@tanstack/query-core";
|
|
3
3
|
//#region src/usePrefetchQuery.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* `usePrefetchQuery` does not return anything, it should be used just to fire a prefetch during render, before
|
|
6
|
+
* a suspense boundary that wraps a component that uses `useSuspenseQuery`. You can pass everything to
|
|
7
|
+
* `usePrefetchQuery` that you can pass to `queryClient.query`, though `queryKey` is always required, and
|
|
8
|
+
* `queryFn` is required unless a default query function has been defined.
|
|
9
|
+
*
|
|
10
|
+
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
11
|
+
* over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
|
|
12
|
+
* already there or already in flight.
|
|
13
|
+
*
|
|
14
|
+
* @param options - The {@link UsePrefetchQueryOptions} to use — everything you can pass to `queryClient.query`.
|
|
15
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
16
|
+
* be used.
|
|
17
|
+
* @returns `void` — nothing is returned.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* import { Suspense } from 'preact/compat'
|
|
22
|
+
* import { usePrefetchQuery } from '@tanstack/preact-query'
|
|
23
|
+
*
|
|
24
|
+
* function App() {
|
|
25
|
+
* // Fire the prefetch during render, before the suspense boundary below.
|
|
26
|
+
* usePrefetchQuery({
|
|
27
|
+
* queryKey: ['posts'],
|
|
28
|
+
* queryFn: fetchPosts,
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
* return (
|
|
32
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
33
|
+
* <Posts />
|
|
34
|
+
* </Suspense>
|
|
35
|
+
* )
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
4
39
|
declare function usePrefetchQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UsePrefetchQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>, queryClient?: QueryClient): void;
|
|
5
40
|
//#endregion
|
|
6
41
|
export { usePrefetchQuery };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePrefetchQuery.d.cts","names":[],"sources":["../../src/usePrefetchQuery.tsx"],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePrefetchQuery.d.cts","names":[],"sources":["../../src/usePrefetchQuery.tsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCgB,iBACd,wBACA,SAAS,cACT,QAAQ,cACR,aAAa,cACb,kBAAkB,WAAW,UAE7B,SAAS,wBACP,cACA,QACA,OACA,YACA,YAEF,cAAc"}
|
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
import { UsePrefetchQueryOptions } from "./types.js";
|
|
2
2
|
import { DefaultError, QueryClient, QueryKey } from "@tanstack/query-core";
|
|
3
3
|
//#region src/usePrefetchQuery.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* `usePrefetchQuery` does not return anything, it should be used just to fire a prefetch during render, before
|
|
6
|
+
* a suspense boundary that wraps a component that uses `useSuspenseQuery`. You can pass everything to
|
|
7
|
+
* `usePrefetchQuery` that you can pass to `queryClient.query`, though `queryKey` is always required, and
|
|
8
|
+
* `queryFn` is required unless a default query function has been defined.
|
|
9
|
+
*
|
|
10
|
+
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
11
|
+
* over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
|
|
12
|
+
* already there or already in flight.
|
|
13
|
+
*
|
|
14
|
+
* @param options - The {@link UsePrefetchQueryOptions} to use — everything you can pass to `queryClient.query`.
|
|
15
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
16
|
+
* be used.
|
|
17
|
+
* @returns `void` — nothing is returned.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* import { Suspense } from 'preact/compat'
|
|
22
|
+
* import { usePrefetchQuery } from '@tanstack/preact-query'
|
|
23
|
+
*
|
|
24
|
+
* function App() {
|
|
25
|
+
* // Fire the prefetch during render, before the suspense boundary below.
|
|
26
|
+
* usePrefetchQuery({
|
|
27
|
+
* queryKey: ['posts'],
|
|
28
|
+
* queryFn: fetchPosts,
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
* return (
|
|
32
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
33
|
+
* <Posts />
|
|
34
|
+
* </Suspense>
|
|
35
|
+
* )
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
4
39
|
declare function usePrefetchQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UsePrefetchQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey>, queryClient?: QueryClient): void;
|
|
5
40
|
//#endregion
|
|
6
41
|
export { usePrefetchQuery };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePrefetchQuery.d.ts","names":[],"sources":["../../src/usePrefetchQuery.tsx"],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePrefetchQuery.d.ts","names":[],"sources":["../../src/usePrefetchQuery.tsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCgB,iBACd,wBACA,SAAS,cACT,QAAQ,cACR,aAAa,cACb,kBAAkB,WAAW,UAE7B,SAAS,wBACP,cACA,QACA,OACA,YACA,YAEF,cAAc"}
|
|
@@ -1,9 +1,44 @@
|
|
|
1
1
|
import { useQueryClient } from "./QueryClientProvider.js";
|
|
2
2
|
import { noop } from "@tanstack/query-core";
|
|
3
3
|
//#region src/usePrefetchQuery.tsx
|
|
4
|
+
/**
|
|
5
|
+
* `usePrefetchQuery` does not return anything, it should be used just to fire a prefetch during render, before
|
|
6
|
+
* a suspense boundary that wraps a component that uses `useSuspenseQuery`. You can pass everything to
|
|
7
|
+
* `usePrefetchQuery` that you can pass to `queryClient.query`, though `queryKey` is always required, and
|
|
8
|
+
* `queryFn` is required unless a default query function has been defined.
|
|
9
|
+
*
|
|
10
|
+
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
11
|
+
* over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
|
|
12
|
+
* already there or already in flight.
|
|
13
|
+
*
|
|
14
|
+
* @param options - The {@link UsePrefetchQueryOptions} to use — everything you can pass to `queryClient.query`.
|
|
15
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
16
|
+
* be used.
|
|
17
|
+
* @returns `void` — nothing is returned.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* import { Suspense } from 'preact/compat'
|
|
22
|
+
* import { usePrefetchQuery } from '@tanstack/preact-query'
|
|
23
|
+
*
|
|
24
|
+
* function App() {
|
|
25
|
+
* // Fire the prefetch during render, before the suspense boundary below.
|
|
26
|
+
* usePrefetchQuery({
|
|
27
|
+
* queryKey: ['posts'],
|
|
28
|
+
* queryFn: fetchPosts,
|
|
29
|
+
* })
|
|
30
|
+
*
|
|
31
|
+
* return (
|
|
32
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
33
|
+
* <Posts />
|
|
34
|
+
* </Suspense>
|
|
35
|
+
* )
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
4
39
|
function usePrefetchQuery(options, queryClient) {
|
|
5
40
|
const client = useQueryClient(queryClient);
|
|
6
|
-
if (!client.getQueryState(options.queryKey)) client.
|
|
41
|
+
if (!client.getQueryState(options.queryKey)) client.query(options).catch(noop);
|
|
7
42
|
}
|
|
8
43
|
//#endregion
|
|
9
44
|
export { usePrefetchQuery };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePrefetchQuery.js","names":[],"sources":["../../src/usePrefetchQuery.tsx"],"sourcesContent":["import { noop } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\n\nimport type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'\nimport type { UsePrefetchQueryOptions } from './types'\n\nexport function usePrefetchQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UsePrefetchQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n queryClient?: QueryClient,\n) {\n const client = useQueryClient(queryClient)\n\n if (!client.getQueryState(options.queryKey)) {\n void client.
|
|
1
|
+
{"version":3,"file":"usePrefetchQuery.js","names":[],"sources":["../../src/usePrefetchQuery.tsx"],"sourcesContent":["import { noop } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\n\nimport type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'\nimport type { UsePrefetchQueryOptions } from './types'\n\n/**\n * `usePrefetchQuery` does not return anything, it should be used just to fire a prefetch during render, before\n * a suspense boundary that wraps a component that uses `useSuspenseQuery`. You can pass everything to\n * `usePrefetchQuery` that you can pass to `queryClient.query`, though `queryKey` is always required, and\n * `queryFn` is required unless a default query function has been defined.\n *\n * The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left\n * over from a previous attempt — so calling this on every render is cheap and won't refetch data that's\n * already there or already in flight.\n *\n * @param options - The {@link UsePrefetchQueryOptions} to use — everything you can pass to `queryClient.query`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns `void` — nothing is returned.\n *\n * @example\n * ```tsx\n * import { Suspense } from 'preact/compat'\n * import { usePrefetchQuery } from '@tanstack/preact-query'\n *\n * function App() {\n * // Fire the prefetch during render, before the suspense boundary below.\n * usePrefetchQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * return (\n * <Suspense fallback={<h1>Loading posts...</h1>}>\n * <Posts />\n * </Suspense>\n * )\n * }\n * ```\n */\nexport function usePrefetchQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UsePrefetchQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n queryClient?: QueryClient,\n) {\n const client = useQueryClient(queryClient)\n\n if (!client.getQueryState(options.queryKey)) {\n void client.query(options).catch(noop)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,SAAgB,iBAOd,SAOA,aACA;CACA,MAAM,SAAS,eAAe,WAAW;CAEzC,IAAI,CAAC,OAAO,cAAc,QAAQ,QAAQ,GACxC,OAAY,MAAM,OAAO,CAAC,CAAC,MAAM,IAAI;AAEzC"}
|
|
@@ -8,6 +8,56 @@ const require_utils = require("./utils.cjs");
|
|
|
8
8
|
let _tanstack_query_core = require("@tanstack/query-core");
|
|
9
9
|
let preact_hooks = require("preact/hooks");
|
|
10
10
|
//#region src/useQueries.ts
|
|
11
|
+
/**
|
|
12
|
+
* The `useQueries` hook can be used to fetch a variable number of queries.
|
|
13
|
+
*
|
|
14
|
+
* The `queries` key accepts an array with query option objects identical to `useQuery` (excluding the
|
|
15
|
+
* `queryClient` option - because the `QueryClient` can be passed in on the top level).
|
|
16
|
+
*
|
|
17
|
+
* Having the same query key more than once in the array of query objects may cause some data to be shared
|
|
18
|
+
* between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
|
|
19
|
+
* structure.
|
|
20
|
+
*
|
|
21
|
+
* The `combine` option can be used to combine the results of the queries into a single value. The result will
|
|
22
|
+
* be structurally shared to be as referentially stable as possible.
|
|
23
|
+
*
|
|
24
|
+
* @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
25
|
+
* will be used.
|
|
26
|
+
* @returns The combined result. Without `combine`, this is an array with all the query results, in the same
|
|
27
|
+
* order as the input. When `combine` is provided, this is the value returned by `combine` instead.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```tsx
|
|
31
|
+
* import { useQueries } from '@tanstack/preact-query'
|
|
32
|
+
*
|
|
33
|
+
* const ids = [1, 2, 3]
|
|
34
|
+
* const results = useQueries({
|
|
35
|
+
* queries: ids.map((id) => ({
|
|
36
|
+
* queryKey: ['post', id],
|
|
37
|
+
* queryFn: () => fetchPost(id),
|
|
38
|
+
* staleTime: Infinity,
|
|
39
|
+
* })),
|
|
40
|
+
* })
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* Combining results into a single value:
|
|
45
|
+
* ```tsx
|
|
46
|
+
* const ids = [1, 2, 3]
|
|
47
|
+
* const combinedQueries = useQueries({
|
|
48
|
+
* queries: ids.map((id) => ({
|
|
49
|
+
* queryKey: ['post', id],
|
|
50
|
+
* queryFn: () => fetchPost(id),
|
|
51
|
+
* })),
|
|
52
|
+
* combine: (results) => {
|
|
53
|
+
* return {
|
|
54
|
+
* data: results.map((result) => result.data),
|
|
55
|
+
* pending: results.some((result) => result.isPending),
|
|
56
|
+
* }
|
|
57
|
+
* },
|
|
58
|
+
* })
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
11
61
|
function useQueries({ queries, ...options }, queryClient) {
|
|
12
62
|
const client = require_QueryClientProvider.useQueryClient(queryClient);
|
|
13
63
|
const isRestoring = require_IsRestoringProvider.useIsRestoring();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQueries.cjs","names":["useQueryClient","useIsRestoring","useQueryErrorResetBoundary","useMemo","useState","QueriesObserver","useCallback","notifyManager","noop","shouldSuspend","QueryObserver","fetchOptimistic","getHasError"],"sources":["../../src/useQueries.ts"],"sourcesContent":["import {\n QueriesObserver,\n QueryObserver,\n noop,\n notifyManager,\n} from '@tanstack/query-core'\nimport type {\n DefaultError,\n OmitKeyof,\n QueriesObserverOptions,\n QueriesPlaceholderDataFunction,\n QueryClient,\n QueryFunction,\n QueryKey,\n QueryObserverOptions,\n ThrowOnError,\n} from '@tanstack/query-core'\nimport { useCallback, useEffect, useMemo, useState } from 'preact/hooks'\n\nimport { useIsRestoring } from './IsRestoringProvider'\nimport { useQueryClient } from './QueryClientProvider'\nimport { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'\nimport {\n ensurePreventErrorBoundaryRetry,\n getHasError,\n useClearResetErrorBoundary,\n} from './errorBoundaryUtils'\nimport {\n ensureSuspenseTimers,\n fetchOptimistic,\n shouldSuspend,\n} from './suspense'\nimport type {\n DefinedUseQueryResult,\n UseQueryOptions,\n UseQueryResult,\n} from './types'\nimport { useSyncExternalStore } from './utils'\n\n// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.\n// `placeholderData` function always gets undefined passed\ntype UseQueryOptionsForUseQueries<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> = OmitKeyof<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'placeholderData' | 'subscribed'\n> & {\n placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>\n}\n\n// Avoid TS depth-limit error in case of large array literal\ntype MAXIMUM_DEPTH = 20\n\n// Widen the type of the symbol to enable type inference even if skipToken is not immutable.\ntype SkipTokenForUseQueries = symbol\n\ntype GetUseQueryOptionsForUseQueries<T> =\n // Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }\n T extends {\n queryFnData: infer TQueryFnData\n error?: infer TError\n data: infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<unknown, TError, TData>\n : // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]\n T extends [infer TQueryFnData, infer TError, infer TData]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryOptionsForUseQueries<TQueryFnData>\n : // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, infer TQueryKey>\n | SkipTokenForUseQueries\n select?: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? UseQueryOptionsForUseQueries<\n TQueryFnData,\n unknown extends TError ? DefaultError : TError,\n unknown extends TData ? TQueryFnData : TData,\n TQueryKey\n >\n : // Fallback\n UseQueryOptionsForUseQueries\n\n// A defined initialData setting should return a DefinedUseQueryResult rather than UseQueryResult\ntype GetDefinedOrUndefinedQueryResult<T, TData, TError = unknown> = T extends {\n initialData?: infer TInitialData\n}\n ? unknown extends TInitialData\n ? UseQueryResult<TData, TError>\n : TInitialData extends TData\n ? DefinedUseQueryResult<TData, TError>\n : TInitialData extends () => infer TInitialDataResult\n ? unknown extends TInitialDataResult\n ? UseQueryResult<TData, TError>\n : TInitialDataResult extends TData\n ? DefinedUseQueryResult<TData, TError>\n : UseQueryResult<TData, TError>\n : UseQueryResult<TData, TError>\n : UseQueryResult<TData, TError>\n\ntype GetUseQueryResult<T> =\n // Part 1: responsible for mapping explicit type parameter to function result, if object\n T extends { queryFnData: any; error?: infer TError; data: infer TData }\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : // Part 2: responsible for mapping explicit type parameter to function result, if tuple\n T extends [any, infer TError, infer TData]\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : T extends [infer TQueryFnData, infer TError]\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData>\n : // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, any>\n | SkipTokenForUseQueries\n select?: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? GetDefinedOrUndefinedQueryResult<\n T,\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >\n : // Fallback\n UseQueryResult\n\n/**\n * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param\n */\nexport type QueriesOptions<\n T extends Array<any>,\n TResults extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<UseQueryOptionsForUseQueries>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResults, GetUseQueryOptionsForUseQueries<Head>]\n : T extends [infer Head, ...infer Tails]\n ? QueriesOptions<\n [...Tails],\n [...TResults, GetUseQueryOptionsForUseQueries<Head>],\n [...TDepth, 1]\n >\n : ReadonlyArray<unknown> extends T\n ? T\n : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogeneous type!\n // use this to infer the param types in the case of Array.map() argument\n T extends Array<\n UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n infer TQueryKey\n >\n >\n ? Array<\n UseQueryOptionsForUseQueries<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >\n >\n : // Fallback\n Array<UseQueryOptionsForUseQueries>\n\n/**\n * QueriesResults reducer recursively maps type param to results\n */\nexport type QueriesResults<\n T extends Array<any>,\n TResults extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<UseQueryResult>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResults, GetUseQueryResult<Head>]\n : T extends [infer Head, ...infer Tails]\n ? QueriesResults<\n [...Tails],\n [...TResults, GetUseQueryResult<Head>],\n [...TDepth, 1]\n >\n : { [K in keyof T]: GetUseQueryResult<T[K]> }\n\nexport function useQueries<\n T extends Array<any>,\n TCombinedResult = QueriesResults<T>,\n>(\n {\n queries,\n ...options\n }: {\n queries:\n | readonly [...QueriesOptions<T>]\n | readonly [...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]> }]\n combine?: (result: QueriesResults<T>) => TCombinedResult\n subscribed?: boolean\n },\n queryClient?: QueryClient,\n): TCombinedResult {\n const client = useQueryClient(queryClient)\n const isRestoring = useIsRestoring()\n const errorResetBoundary = useQueryErrorResetBoundary()\n\n const defaultedQueries = useMemo(\n () =>\n queries.map((opts) => {\n const defaultedOptions = client.defaultQueryOptions(\n opts as QueryObserverOptions,\n )\n\n // Make sure the results are already in fetching state before subscribing or updating options\n defaultedOptions._optimisticResults = isRestoring\n ? 'isRestoring'\n : 'optimistic'\n\n return defaultedOptions\n }),\n [queries, client, isRestoring],\n )\n\n defaultedQueries.forEach((queryOptions) => {\n ensureSuspenseTimers(queryOptions)\n const query = client.getQueryCache().get(queryOptions.queryHash)\n ensurePreventErrorBoundaryRetry(queryOptions, errorResetBoundary, query)\n })\n\n useClearResetErrorBoundary(errorResetBoundary)\n\n const [observer] = useState(\n () =>\n new QueriesObserver<TCombinedResult>(\n client,\n defaultedQueries,\n options as QueriesObserverOptions<TCombinedResult>,\n ),\n )\n\n // note: this must be called before useSyncExternalStore\n const [optimisticResult, getCombinedResult, trackResult] =\n observer.getOptimisticResult(\n defaultedQueries,\n (options as QueriesObserverOptions<TCombinedResult>).combine,\n )\n\n const shouldSubscribe = !isRestoring && options.subscribed !== false\n useSyncExternalStore(\n useCallback(\n (onStoreChange) =>\n shouldSubscribe\n ? observer.subscribe(notifyManager.batchCalls(onStoreChange))\n : noop,\n [observer, shouldSubscribe],\n ),\n () => observer.getCurrentResult(),\n )\n\n useEffect(() => {\n observer.setQueries(\n defaultedQueries,\n options as QueriesObserverOptions<TCombinedResult>,\n )\n }, [defaultedQueries, options, observer])\n\n const shouldAtLeastOneSuspend = optimisticResult.some((result, index) =>\n shouldSuspend(defaultedQueries[index], result),\n )\n\n const suspensePromises = shouldAtLeastOneSuspend\n ? optimisticResult.flatMap((result, index) => {\n const opts = defaultedQueries[index]\n\n if (opts && shouldSuspend(opts, result)) {\n const queryObserver = new QueryObserver(client, opts)\n return fetchOptimistic(opts, queryObserver, errorResetBoundary)\n }\n return []\n })\n : []\n\n if (suspensePromises.length > 0) {\n throw Promise.all(suspensePromises)\n }\n const firstSingleResultWhichShouldThrow = optimisticResult.find(\n (result, index) => {\n const query = defaultedQueries[index]\n return (\n query &&\n getHasError({\n result,\n errorResetBoundary,\n throwOnError: query.throwOnError,\n query: client.getQueryCache().get(query.queryHash),\n suspense: query.suspense,\n })\n )\n },\n )\n\n if (firstSingleResultWhichShouldThrow?.error) {\n throw firstSingleResultWhichShouldThrow.error\n }\n\n return getCombinedResult(trackResult())\n}\n"],"mappings":";;;;;;;;;;AA8MA,SAAgB,WAId,EACE,SACA,GAAG,WAQL,aACiB;CACjB,MAAM,SAASA,4BAAAA,eAAe,WAAW;CACzC,MAAM,cAAcC,4BAAAA,eAAe;CACnC,MAAM,qBAAqBC,gCAAAA,2BAA2B;CAEtD,MAAM,oBAAA,GAAmBC,aAAAA,QAAAA,OAErB,QAAQ,KAAK,SAAS;EACpB,MAAM,mBAAmB,OAAO,oBAC9B,IACF;EAGA,iBAAiB,qBAAqB,cAClC,gBACA;EAEJ,OAAO;CACT,CAAC,GACH;EAAC;EAAS;EAAQ;CAAW,CAC/B;CAEA,iBAAiB,SAAS,iBAAiB;EACzC,iBAAA,qBAAqB,YAAY;EACjC,MAAM,QAAQ,OAAO,cAAc,CAAC,CAAC,IAAI,aAAa,SAAS;EAC/D,2BAAA,gCAAgC,cAAc,oBAAoB,KAAK;CACzE,CAAC;CAED,2BAAA,2BAA2B,kBAAkB;CAE7C,MAAM,CAAC,aAAA,GAAYC,aAAAA,SAAAA,OAEf,IAAIC,qBAAAA,gBACF,QACA,kBACA,OACF,CACJ;CAGA,MAAM,CAAC,kBAAkB,mBAAmB,eAC1C,SAAS,oBACP,kBACC,QAAoD,OACvD;CAEF,MAAM,kBAAkB,CAAC,eAAe,QAAQ,eAAe;CAC/D,cAAA,sBAAA,GACEC,aAAAA,YAAAA,EACG,kBACC,kBACI,SAAS,UAAUC,qBAAAA,cAAc,WAAW,aAAa,CAAC,IAC1DC,qBAAAA,MACN,CAAC,UAAU,eAAe,CAC5B,SACM,SAAS,iBAAiB,CAClC;CAEA,CAAA,GAAA,aAAA,UAAA,OAAgB;EACd,SAAS,WACP,kBACA,OACF;CACF,GAAG;EAAC;EAAkB;EAAS;CAAQ,CAAC;CAMxC,MAAM,mBAJ0B,iBAAiB,MAAM,QAAQ,UAC7DC,iBAAAA,cAAc,iBAAiB,QAAQ,MAAM,CAGA,IAC3C,iBAAiB,SAAS,QAAQ,UAAU;EAC1C,MAAM,OAAO,iBAAiB;EAE9B,IAAI,QAAQA,iBAAAA,cAAc,MAAM,MAAM,GAAG;GACvC,MAAM,gBAAgB,IAAIC,qBAAAA,cAAc,QAAQ,IAAI;GACpD,OAAOC,iBAAAA,gBAAgB,MAAM,eAAe,kBAAkB;EAChE;EACA,OAAO,CAAC;CACV,CAAC,IACD,CAAC;CAEL,IAAI,iBAAiB,SAAS,GAC5B,MAAM,QAAQ,IAAI,gBAAgB;CAEpC,MAAM,oCAAoC,iBAAiB,MACxD,QAAQ,UAAU;EACjB,MAAM,QAAQ,iBAAiB;EAC/B,OACE,SACAC,2BAAAA,YAAY;GACV;GACA;GACA,cAAc,MAAM;GACpB,OAAO,OAAO,cAAc,CAAC,CAAC,IAAI,MAAM,SAAS;GACjD,UAAU,MAAM;EAClB,CAAC;CAEL,CACF;CAEA,IAAI,mCAAmC,OACrC,MAAM,kCAAkC;CAG1C,OAAO,kBAAkB,YAAY,CAAC;AACxC"}
|
|
1
|
+
{"version":3,"file":"useQueries.cjs","names":["useQueryClient","useIsRestoring","useQueryErrorResetBoundary","useMemo","useState","QueriesObserver","useCallback","notifyManager","noop","shouldSuspend","QueryObserver","fetchOptimistic","getHasError"],"sources":["../../src/useQueries.ts"],"sourcesContent":["import {\n QueriesObserver,\n QueryObserver,\n noop,\n notifyManager,\n} from '@tanstack/query-core'\nimport type {\n DefaultError,\n OmitKeyof,\n QueriesObserverOptions,\n QueriesPlaceholderDataFunction,\n QueryClient,\n QueryFunction,\n QueryKey,\n QueryObserverOptions,\n ThrowOnError,\n} from '@tanstack/query-core'\nimport { useCallback, useEffect, useMemo, useState } from 'preact/hooks'\n\nimport { useIsRestoring } from './IsRestoringProvider'\nimport { useQueryClient } from './QueryClientProvider'\nimport { useQueryErrorResetBoundary } from './QueryErrorResetBoundary'\nimport {\n ensurePreventErrorBoundaryRetry,\n getHasError,\n useClearResetErrorBoundary,\n} from './errorBoundaryUtils'\nimport {\n ensureSuspenseTimers,\n fetchOptimistic,\n shouldSuspend,\n} from './suspense'\nimport type {\n DefinedUseQueryResult,\n UseQueryOptions,\n UseQueryResult,\n} from './types'\nimport { useSyncExternalStore } from './utils'\n\n// This defines the `UseQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.\n// `placeholderData` function always gets undefined passed\ntype UseQueryOptionsForUseQueries<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> = OmitKeyof<\n UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'placeholderData' | 'subscribed'\n> & {\n placeholderData?: TQueryFnData | QueriesPlaceholderDataFunction<TQueryFnData>\n}\n\n// Avoid TS depth-limit error in case of large array literal\ntype MAXIMUM_DEPTH = 20\n\n// Widen the type of the symbol to enable type inference even if skipToken is not immutable.\ntype SkipTokenForUseQueries = symbol\n\ntype GetUseQueryOptionsForUseQueries<T> =\n // Part 1: responsible for applying explicit type parameter to function arguments, if object { queryFnData: TQueryFnData, error: TError, data: TData }\n T extends {\n queryFnData: infer TQueryFnData\n error?: infer TError\n data: infer TData\n }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? UseQueryOptionsForUseQueries<unknown, TError, TData>\n : // Part 2: responsible for applying explicit type parameter to function arguments, if tuple [TQueryFnData, TError, TData]\n T extends [infer TQueryFnData, infer TError, infer TData]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError, TData>\n : T extends [infer TQueryFnData, infer TError]\n ? UseQueryOptionsForUseQueries<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? UseQueryOptionsForUseQueries<TQueryFnData>\n : // Part 3: responsible for inferring and enforcing type if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, infer TQueryKey>\n | SkipTokenForUseQueries\n select?: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? UseQueryOptionsForUseQueries<\n TQueryFnData,\n unknown extends TError ? DefaultError : TError,\n unknown extends TData ? TQueryFnData : TData,\n TQueryKey\n >\n : // Fallback\n UseQueryOptionsForUseQueries\n\n// A defined initialData setting should return a DefinedUseQueryResult rather than UseQueryResult\ntype GetDefinedOrUndefinedQueryResult<T, TData, TError = unknown> = T extends {\n initialData?: infer TInitialData\n}\n ? unknown extends TInitialData\n ? UseQueryResult<TData, TError>\n : TInitialData extends TData\n ? DefinedUseQueryResult<TData, TError>\n : TInitialData extends () => infer TInitialDataResult\n ? unknown extends TInitialDataResult\n ? UseQueryResult<TData, TError>\n : TInitialDataResult extends TData\n ? DefinedUseQueryResult<TData, TError>\n : UseQueryResult<TData, TError>\n : UseQueryResult<TData, TError>\n : UseQueryResult<TData, TError>\n\ntype GetUseQueryResult<T> =\n // Part 1: responsible for mapping explicit type parameter to function result, if object\n T extends { queryFnData: any; error?: infer TError; data: infer TData }\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : // Part 2: responsible for mapping explicit type parameter to function result, if tuple\n T extends [any, infer TError, infer TData]\n ? GetDefinedOrUndefinedQueryResult<T, TData, TError>\n : T extends [infer TQueryFnData, infer TError]\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? GetDefinedOrUndefinedQueryResult<T, TQueryFnData>\n : // Part 3: responsible for mapping inferred type to results, if no explicit parameter was provided\n T extends {\n queryFn?:\n | QueryFunction<infer TQueryFnData, any>\n | SkipTokenForUseQueries\n select?: (data: any) => infer TData\n throwOnError?: ThrowOnError<any, infer TError, any, any>\n }\n ? GetDefinedOrUndefinedQueryResult<\n T,\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >\n : // Fallback\n UseQueryResult\n\n/**\n * The `queries` array accepted by `useQueries`. Recursively unwraps each tuple element so every entry's\n * `queryFn`/`select`/`throwOnError` are inferred individually, up to 20 elements. An opaque array (e.g.\n * `unknown[]`) is returned as-is; a non-tuple array of a known element type, or a tuple past 20 elements, falls\n * back to a single homogeneous options type.\n *\n * @template T - The type of the `queries` array as written at the call site.\n * @template TResults - The internal accumulator that this type builds during recursion. It is not meant\n * to be set explicitly.\n * @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not\n * meant to be set explicitly.\n */\nexport type QueriesOptions<\n T extends Array<any>,\n TResults extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<UseQueryOptionsForUseQueries>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResults, GetUseQueryOptionsForUseQueries<Head>]\n : T extends [infer Head, ...infer Tails]\n ? QueriesOptions<\n [...Tails],\n [...TResults, GetUseQueryOptionsForUseQueries<Head>],\n [...TDepth, 1]\n >\n : ReadonlyArray<unknown> extends T\n ? T\n : // If T is *some* array but we couldn't assign unknown[] to it, then it must hold some known/homogeneous type!\n // use this to infer the param types in the case of Array.map() argument\n T extends Array<\n UseQueryOptionsForUseQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n infer TQueryKey\n >\n >\n ? Array<\n UseQueryOptionsForUseQueries<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >\n >\n : // Fallback\n Array<UseQueryOptionsForUseQueries>\n\n/**\n * The result type returned by `useQueries`, when no `combine` is provided. Mirrors {@link QueriesOptions}: each\n * tuple element's result type is inferred individually, up to 20 elements. A non-tuple array is mapped\n * per-element instead, still inferring each entry individually; only past 20 elements does this fall back to a\n * single homogeneous {@link UseQueryResult} type.\n *\n * @template T - The type of the `queries` array, as inferred by {@link QueriesOptions}.\n * @template TResults - The internal accumulator that this type builds during recursion. It is not meant\n * to be set explicitly.\n * @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not\n * meant to be set explicitly.\n */\nexport type QueriesResults<\n T extends Array<any>,\n TResults extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<UseQueryResult>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResults, GetUseQueryResult<Head>]\n : T extends [infer Head, ...infer Tails]\n ? QueriesResults<\n [...Tails],\n [...TResults, GetUseQueryResult<Head>],\n [...TDepth, 1]\n >\n : { [K in keyof T]: GetUseQueryResult<T[K]> }\n\n/**\n * The `useQueries` hook can be used to fetch a variable number of queries.\n *\n * The `queries` key accepts an array with query option objects identical to `useQuery` (excluding the\n * `queryClient` option - because the `QueryClient` can be passed in on the top level).\n *\n * Having the same query key more than once in the array of query objects may cause some data to be shared\n * between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired\n * structure.\n *\n * The `combine` option can be used to combine the results of the queries into a single value. The result will\n * be structurally shared to be as referentially stable as possible.\n *\n * @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context\n * will be used.\n * @returns The combined result. Without `combine`, this is an array with all the query results, in the same\n * order as the input. When `combine` is provided, this is the value returned by `combine` instead.\n *\n * @example\n * ```tsx\n * import { useQueries } from '@tanstack/preact-query'\n *\n * const ids = [1, 2, 3]\n * const results = useQueries({\n * queries: ids.map((id) => ({\n * queryKey: ['post', id],\n * queryFn: () => fetchPost(id),\n * staleTime: Infinity,\n * })),\n * })\n * ```\n *\n * @example\n * Combining results into a single value:\n * ```tsx\n * const ids = [1, 2, 3]\n * const combinedQueries = useQueries({\n * queries: ids.map((id) => ({\n * queryKey: ['post', id],\n * queryFn: () => fetchPost(id),\n * })),\n * combine: (results) => {\n * return {\n * data: results.map((result) => result.data),\n * pending: results.some((result) => result.isPending),\n * }\n * },\n * })\n * ```\n */\nexport function useQueries<\n T extends Array<any>,\n TCombinedResult = QueriesResults<T>,\n>(\n {\n queries,\n ...options\n }: {\n /**\n * An array with query option objects, mostly identical to `useQuery` — except that `queryClient` and\n * `subscribed` aren't accepted per-query (`subscribed` is a top-level option here instead), and\n * `placeholderData` accepts a {@link QueriesPlaceholderDataFunction}, which is called with `previousData`\n * and `previousQuery` always `undefined`, rather than `useQuery`'s placeholder function.\n */\n queries:\n | readonly [...QueriesOptions<T>]\n | readonly [...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]> }]\n /**\n * Use this to combine the results of the queries into a single value. The result will be structurally\n * shared to be as referentially stable as possible.\n */\n combine?: (result: QueriesResults<T>) => TCombinedResult\n /**\n * Set this to `false` to unsubscribe this observer from updates to the query cache.\n *\n * @defaultValue true\n */\n subscribed?: boolean\n },\n queryClient?: QueryClient,\n): TCombinedResult {\n const client = useQueryClient(queryClient)\n const isRestoring = useIsRestoring()\n const errorResetBoundary = useQueryErrorResetBoundary()\n\n const defaultedQueries = useMemo(\n () =>\n queries.map((opts) => {\n const defaultedOptions = client.defaultQueryOptions(\n opts as QueryObserverOptions,\n )\n\n // Make sure the results are already in fetching state before subscribing or updating options\n defaultedOptions._optimisticResults = isRestoring\n ? 'isRestoring'\n : 'optimistic'\n\n return defaultedOptions\n }),\n [queries, client, isRestoring],\n )\n\n defaultedQueries.forEach((queryOptions) => {\n ensureSuspenseTimers(queryOptions)\n const query = client.getQueryCache().get(queryOptions.queryHash)\n ensurePreventErrorBoundaryRetry(queryOptions, errorResetBoundary, query)\n })\n\n useClearResetErrorBoundary(errorResetBoundary)\n\n const [observer] = useState(\n () =>\n new QueriesObserver<TCombinedResult>(\n client,\n defaultedQueries,\n options as QueriesObserverOptions<TCombinedResult>,\n ),\n )\n\n // note: this must be called before useSyncExternalStore\n const [optimisticResult, getCombinedResult, trackResult] =\n observer.getOptimisticResult(\n defaultedQueries,\n (options as QueriesObserverOptions<TCombinedResult>).combine,\n )\n\n const shouldSubscribe = !isRestoring && options.subscribed !== false\n useSyncExternalStore(\n useCallback(\n (onStoreChange) =>\n shouldSubscribe\n ? observer.subscribe(notifyManager.batchCalls(onStoreChange))\n : noop,\n [observer, shouldSubscribe],\n ),\n () => observer.getCurrentResult(),\n )\n\n useEffect(() => {\n observer.setQueries(\n defaultedQueries,\n options as QueriesObserverOptions<TCombinedResult>,\n )\n }, [defaultedQueries, options, observer])\n\n const shouldAtLeastOneSuspend = optimisticResult.some((result, index) =>\n shouldSuspend(defaultedQueries[index], result),\n )\n\n const suspensePromises = shouldAtLeastOneSuspend\n ? optimisticResult.flatMap((result, index) => {\n const opts = defaultedQueries[index]\n\n if (opts && shouldSuspend(opts, result)) {\n const queryObserver = new QueryObserver(client, opts)\n return fetchOptimistic(opts, queryObserver, errorResetBoundary)\n }\n return []\n })\n : []\n\n if (suspensePromises.length > 0) {\n throw Promise.all(suspensePromises)\n }\n const firstSingleResultWhichShouldThrow = optimisticResult.find(\n (result, index) => {\n const query = defaultedQueries[index]\n return (\n query &&\n getHasError({\n result,\n errorResetBoundary,\n throwOnError: query.throwOnError,\n query: client.getQueryCache().get(query.queryHash),\n suspense: query.suspense,\n })\n )\n },\n )\n\n if (firstSingleResultWhichShouldThrow?.error) {\n throw firstSingleResultWhichShouldThrow.error\n }\n\n return getCombinedResult(trackResult())\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkRA,SAAgB,WAId,EACE,SACA,GAAG,WAuBL,aACiB;CACjB,MAAM,SAASA,4BAAAA,eAAe,WAAW;CACzC,MAAM,cAAcC,4BAAAA,eAAe;CACnC,MAAM,qBAAqBC,gCAAAA,2BAA2B;CAEtD,MAAM,oBAAA,GAAmBC,aAAAA,QAAAA,OAErB,QAAQ,KAAK,SAAS;EACpB,MAAM,mBAAmB,OAAO,oBAC9B,IACF;EAGA,iBAAiB,qBAAqB,cAClC,gBACA;EAEJ,OAAO;CACT,CAAC,GACH;EAAC;EAAS;EAAQ;CAAW,CAC/B;CAEA,iBAAiB,SAAS,iBAAiB;EACzC,iBAAA,qBAAqB,YAAY;EACjC,MAAM,QAAQ,OAAO,cAAc,CAAC,CAAC,IAAI,aAAa,SAAS;EAC/D,2BAAA,gCAAgC,cAAc,oBAAoB,KAAK;CACzE,CAAC;CAED,2BAAA,2BAA2B,kBAAkB;CAE7C,MAAM,CAAC,aAAA,GAAYC,aAAAA,SAAAA,OAEf,IAAIC,qBAAAA,gBACF,QACA,kBACA,OACF,CACJ;CAGA,MAAM,CAAC,kBAAkB,mBAAmB,eAC1C,SAAS,oBACP,kBACC,QAAoD,OACvD;CAEF,MAAM,kBAAkB,CAAC,eAAe,QAAQ,eAAe;CAC/D,cAAA,sBAAA,GACEC,aAAAA,YAAAA,EACG,kBACC,kBACI,SAAS,UAAUC,qBAAAA,cAAc,WAAW,aAAa,CAAC,IAC1DC,qBAAAA,MACN,CAAC,UAAU,eAAe,CAC5B,SACM,SAAS,iBAAiB,CAClC;CAEA,CAAA,GAAA,aAAA,UAAA,OAAgB;EACd,SAAS,WACP,kBACA,OACF;CACF,GAAG;EAAC;EAAkB;EAAS;CAAQ,CAAC;CAMxC,MAAM,mBAJ0B,iBAAiB,MAAM,QAAQ,UAC7DC,iBAAAA,cAAc,iBAAiB,QAAQ,MAAM,CAGA,IAC3C,iBAAiB,SAAS,QAAQ,UAAU;EAC1C,MAAM,OAAO,iBAAiB;EAE9B,IAAI,QAAQA,iBAAAA,cAAc,MAAM,MAAM,GAAG;GACvC,MAAM,gBAAgB,IAAIC,qBAAAA,cAAc,QAAQ,IAAI;GACpD,OAAOC,iBAAAA,gBAAgB,MAAM,eAAe,kBAAkB;EAChE;EACA,OAAO,CAAC;CACV,CAAC,IACD,CAAC;CAEL,IAAI,iBAAiB,SAAS,GAC5B,MAAM,QAAQ,IAAI,gBAAgB;CAEpC,MAAM,oCAAoC,iBAAiB,MACxD,QAAQ,UAAU;EACjB,MAAM,QAAQ,iBAAiB;EAC/B,OACE,SACAC,2BAAAA,YAAY;GACV;GACA;GACA,cAAc,MAAM;GACpB,OAAO,OAAO,cAAc,CAAC,CAAC,IAAI,MAAM,SAAS;GACjD,UAAU,MAAM;EAClB,CAAC;CAEL,CACF;CAEA,IAAI,mCAAmC,OACrC,MAAM,kCAAkC;CAG1C,OAAO,kBAAkB,YAAY,CAAC;AACxC"}
|