@tanstack/query-core
Version:
The framework agnostic core that powers TanStack Query
1 lines • 9.73 kB
Source Map (JSON)
{"version":3,"file":"infiniteQueryObserver.cjs","names":["QueryObserver","hasNextPage","hasPreviousPage"],"sources":["../../src/infiniteQueryObserver.ts"],"sourcesContent":["import { QueryObserver } from './queryObserver'\nimport { hasNextPage, hasPreviousPage } from './infiniteQueryBehavior'\nimport type { Subscribable } from './subscribable'\nimport type {\n DefaultError,\n DefaultedInfiniteQueryObserverOptions,\n FetchNextPageOptions,\n FetchPreviousPageOptions,\n InfiniteData,\n InfiniteQueryObserverBaseResult,\n InfiniteQueryObserverOptions,\n InfiniteQueryObserverResult,\n QueryKey,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { Query } from './query'\n\ntype InfiniteQueryObserverListener<TData, TError> = (\n result: InfiniteQueryObserverResult<TData, TError>,\n) => void\n\n/**\n * An `InfiniteQueryObserver` extends `QueryObserver` to observe and switch\n * between infinite queries. It augments the base `QueryObserverResult` with\n * infinite-query-specific fields and methods, such as `hasNextPage` and\n * `fetchNextPage`, and is the primitive that framework adapters (e.g.\n * `useInfiniteQuery`) build their hooks on top of.\n * @example\n * ```ts\n * const observer = new InfiniteQueryObserver(queryClient, {\n * queryKey: ['projects'],\n * queryFn: ({ pageParam }) => fetchProjects(pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextCursor,\n * })\n *\n * const unsubscribe = observer.subscribe((result) => console.log(result))\n * ```\n */\nexport class InfiniteQueryObserver<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n> extends QueryObserver<\n TQueryFnData,\n TError,\n TData,\n InfiniteData<TQueryFnData, TPageParam>,\n TQueryKey\n> {\n // Type override\n override subscribe!: Subscribable<\n InfiniteQueryObserverListener<TData, TError>\n >['subscribe']\n\n // Type override\n override getCurrentResult!: ReplaceReturnType<\n QueryObserver<\n TQueryFnData,\n TError,\n TData,\n InfiniteData<TQueryFnData, TPageParam>,\n TQueryKey\n >['getCurrentResult'],\n InfiniteQueryObserverResult<TData, TError>\n >\n\n // Type override\n protected override fetch!: ReplaceReturnType<\n QueryObserver<\n TQueryFnData,\n TError,\n TData,\n InfiniteData<TQueryFnData, TPageParam>,\n TQueryKey\n >['fetch'],\n Promise<InfiniteQueryObserverResult<TData, TError>>\n >\n\n constructor(\n client: QueryClient,\n options: InfiniteQueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n ) {\n super(client, options)\n }\n\n protected override bindMethods(): void {\n super.bindMethods()\n this.fetchNextPage = this.fetchNextPage.bind(this)\n this.fetchPreviousPage = this.fetchPreviousPage.bind(this)\n }\n\n /**\n * Updates the observer's options. Behaves the same as\n * `QueryObserver.setOptions`, additionally marking the options as\n * belonging to an infinite query before delegating to the base\n * implementation.\n * @param options - The new infinite query observer options.\n */\n override setOptions(\n options: InfiniteQueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n ): void {\n options._type = 'infinite'\n super.setOptions(options)\n }\n\n /**\n * The infinite-query counterpart of {@link QueryObserver#getOptimisticResult}, marking the\n * options as an infinite query before delegating to it. Called by framework adapters (e.g.\n * `useInfiniteQuery`) ahead of subscribing, to compute the current `InfiniteQueryObserverResult`\n * synchronously.\n * @param options - The defaulted infinite query observer options to compute the result for.\n * @returns The result for the given options.\n */\n override getOptimisticResult(\n options: DefaultedInfiniteQueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n ): InfiniteQueryObserverResult<TData, TError> {\n options._type = 'infinite'\n return super.getOptimisticResult(options) as InfiniteQueryObserverResult<\n TData,\n TError\n >\n }\n\n /**\n * Fetches the next page of the infinite query and returns a promise that\n * resolves with the resulting `InfiniteQueryObserverResult`. The page\n * param used for the fetch is determined by `getNextPageParam`, which\n * receives the current pages/page params and whose result also determines\n * `hasNextPage`.\n * @param options - Set `cancelRefetch` to `false` to ignore the call while a fetch is running,\n * and `throwOnError` to `true` to reject when the fetch fails.\n * @returns A promise that resolves with the result after the next page is fetched. With\n * `cancelRefetch: false`, a running fetch is reused instead, so the next page may not be fetched.\n * @see {@link InfiniteQueryObserver#fetchPreviousPage}\n * @example\n * ```ts\n * const { hasNextPage } = observer.getCurrentResult()\n *\n * if (hasNextPage) {\n * await observer.fetchNextPage()\n * }\n * ```\n */\n fetchNextPage(\n options?: FetchNextPageOptions,\n ): Promise<InfiniteQueryObserverResult<TData, TError>> {\n return this.fetch({\n ...options,\n meta: {\n fetchMore: { direction: 'forward' },\n },\n })\n }\n\n /**\n * Fetches the previous page of the infinite query and returns a promise\n * that resolves with the resulting `InfiniteQueryObserverResult`. The page\n * param used for the fetch is determined by `getPreviousPageParam`, which\n * receives the current pages/page params and whose result also determines\n * `hasPreviousPage`.\n * @param options - Set `cancelRefetch` to `false` to ignore the call while a fetch is running,\n * and `throwOnError` to `true` to reject when the fetch fails.\n * @returns A promise that resolves with the result after the previous page is fetched. With\n * `cancelRefetch: false`, a running fetch is reused instead, so the previous page may not be fetched.\n * @see {@link InfiniteQueryObserver#fetchNextPage}\n * @example\n * ```ts\n * const { hasPreviousPage } = observer.getCurrentResult()\n *\n * if (hasPreviousPage) {\n * await observer.fetchPreviousPage()\n * }\n * ```\n */\n fetchPreviousPage(\n options?: FetchPreviousPageOptions,\n ): Promise<InfiniteQueryObserverResult<TData, TError>> {\n return this.fetch({\n ...options,\n meta: {\n fetchMore: { direction: 'backward' },\n },\n })\n }\n\n protected override createResult(\n query: Query<\n TQueryFnData,\n TError,\n InfiniteData<TQueryFnData, TPageParam>,\n TQueryKey\n >,\n options: InfiniteQueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n ): InfiniteQueryObserverResult<TData, TError> {\n const { state } = query\n const parentResult = super.createResult(query, options)\n\n const { isFetching, isRefetching, isError, isRefetchError } = parentResult\n const fetchDirection = state.fetchMeta?.fetchMore?.direction\n\n const isFetchNextPageError = isError && fetchDirection === 'forward'\n const isFetchingNextPage = isFetching && fetchDirection === 'forward'\n\n const isFetchPreviousPageError = isError && fetchDirection === 'backward'\n const isFetchingPreviousPage = isFetching && fetchDirection === 'backward'\n\n const result: InfiniteQueryObserverBaseResult<TData, TError> = {\n ...parentResult,\n fetchNextPage: this.fetchNextPage,\n fetchPreviousPage: this.fetchPreviousPage,\n hasNextPage: hasNextPage(options, state.data),\n hasPreviousPage: hasPreviousPage(options, state.data),\n isFetchNextPageError,\n isFetchingNextPage,\n isFetchPreviousPageError,\n isFetchingPreviousPage,\n isRefetchError:\n isRefetchError && !isFetchNextPageError && !isFetchPreviousPageError,\n isRefetching:\n isRefetching && !isFetchingNextPage && !isFetchingPreviousPage,\n }\n\n return result as InfiniteQueryObserverResult<TData, TError>\n }\n}\n\ntype ReplaceReturnType<\n TFunction extends (...args: Array<any>) => unknown,\n TReturn,\n> = (...args: Parameters<TFunction>) => TReturn\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAuCA,IAAa,wBAAb,cAMUA,sBAAAA,cAMR;CA8BA,YACE,QACA,SAOA;EACA,MAAM,QAAQ,OAAO;CACvB;CAEA,cAAuC;EACrC,MAAM,YAAY;EAClB,KAAK,gBAAgB,KAAK,cAAc,KAAK,IAAI;EACjD,KAAK,oBAAoB,KAAK,kBAAkB,KAAK,IAAI;CAC3D;;;;;;;;CASA,WACE,SAOM;EACN,QAAQ,QAAQ;EAChB,MAAM,WAAW,OAAO;CAC1B;;;;;;;;;CAUA,oBACE,SAO4C;EAC5C,QAAQ,QAAQ;EAChB,OAAO,MAAM,oBAAoB,OAAO;CAI1C;;;;;;;;;;;;;;;;;;;;;CAsBA,cACE,SACqD;EACrD,OAAO,KAAK,MAAM;GAChB,GAAG;GACH,MAAM,EACJ,WAAW,EAAE,WAAW,UAAU,EACpC;EACF,CAAC;CACH;;;;;;;;;;;;;;;;;;;;;CAsBA,kBACE,SACqD;EAC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