@tanstack/angular-query 5.0.0-rc.5 → 5.108.0
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/README.md +1 -1
- package/hydration-state-key.mjs +1 -2
- package/index.d.ts +1 -1
- package/index.mjs +1 -2
- package/index.mjs.map +1 -1
- package/infinite-query-options.d.ts +15 -13
- package/infinite-query-options.mjs +1 -2
- package/infinite-query-options.mjs.map +1 -1
- package/inject-base-query.mjs +3 -4
- package/inject-base-query.mjs.map +1 -1
- package/inject-infinite-query.d.ts +170 -33
- package/inject-infinite-query.mjs +2 -3
- package/inject-infinite-query.mjs.map +1 -1
- package/inject-is-fetching.d.ts +37 -6
- package/inject-is-fetching.mjs +36 -6
- package/inject-is-fetching.mjs.map +1 -1
- package/inject-is-mutating.d.ts +21 -6
- package/inject-is-mutating.mjs +20 -6
- package/inject-is-mutating.mjs.map +1 -1
- package/inject-is-restoring.d.ts +5 -3
- package/inject-is-restoring.mjs +6 -5
- package/inject-is-restoring.mjs.map +1 -1
- package/inject-mutation-state.d.ts +51 -3
- package/inject-mutation-state.mjs +52 -5
- package/inject-mutation-state.mjs.map +1 -1
- package/inject-mutation.d.ts +118 -13
- package/inject-mutation.mjs +121 -17
- package/inject-mutation.mjs.map +1 -1
- package/inject-queries.d.ts +113 -15
- package/inject-queries.mjs +116 -19
- package/inject-queries.mjs.map +1 -1
- package/inject-queries.types.d.ts +25 -5
- package/inject-query.d.ts +11 -8
- package/inject-query.mjs +2 -3
- package/inject-query.mjs.map +1 -1
- package/mutation-options.mjs +1 -2
- package/package.json +2 -2
- package/providers.d.ts +31 -6
- package/providers.mjs +32 -8
- package/providers.mjs.map +1 -1
- package/query-options.d.ts +23 -13
- package/query-options.mjs +1 -2
- package/query-options.mjs.map +1 -1
- package/query-resource.d.ts +4 -1
- package/query-resource.mjs +4 -2
- package/query-resource.mjs.map +1 -1
- package/schematics/migrate-from-experimental/index.js +3 -3
- package/schematics/migrate-from-experimental/workspace.js +1 -1
- package/schematics/ng-add/index.js +5 -4
- package/types.d.ts +38 -7
- package/utils/inject-external-store.mjs +1 -2
- package/utils/inject-pending-tasks-lifecycle.mjs +1 -2
- package/utils/inject-query-zone.mjs +1 -2
- package/utils/result-fields.mjs +1 -2
- package/utils/signal-proxy.mjs +1 -2
package/README.md
CHANGED
|
@@ -93,7 +93,7 @@ export class TodosComponent {
|
|
|
93
93
|
|
|
94
94
|
4. If you need to update options on your query dynamically, make sure to pass them as signals. The query will refetch automatically if data for an updated query key is stale or not present.
|
|
95
95
|
|
|
96
|
-
[Open in StackBlitz](https://stackblitz.com/github/TanStack/query/tree/main/examples/angular/router)
|
|
96
|
+
[Open in StackBlitz](https://stackblitz.com/github/TanStack/query/tree/main/examples/angular/basic-angular-router)
|
|
97
97
|
|
|
98
98
|
```ts
|
|
99
99
|
@Component({})
|
package/hydration-state-key.mjs
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { InjectionToken, makeStateKey } from "@angular/core";
|
|
2
|
-
|
|
2
|
+
// src/hydration-state-key.ts
|
|
3
3
|
var INTERNAL_TANSTACK_QUERY_HYDRATION_STATE_KEY = makeStateKey("tanstack-query-hydration-state");
|
|
4
4
|
var INTERNAL_TANSTACK_QUERY_HYDRATION_TRANSFER_KEY = new InjectionToken("tanstack-query-hydration-transfer-key", {
|
|
5
5
|
providedIn: "root",
|
|
6
6
|
factory: () => INTERNAL_TANSTACK_QUERY_HYDRATION_STATE_KEY
|
|
7
7
|
});
|
|
8
|
-
//#endregion
|
|
9
8
|
export { INTERNAL_TANSTACK_QUERY_HYDRATION_STATE_KEY, INTERNAL_TANSTACK_QUERY_HYDRATION_TRANSFER_KEY };
|
|
10
9
|
|
|
11
10
|
//# sourceMappingURL=hydration-state-key.mjs.map
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from '@tanstack/query-core';
|
|
2
|
-
export
|
|
2
|
+
export type { CreateQueryOptions, BaseQueryNarrowing, DefinedQueryNarrowing, BaseInfiniteQueryNarrowing, DefinedInfiniteQueryNarrowing, CreateInfiniteQueryOptions, CreateBaseQueryResult, CreateQueryResult, DefinedCreateQueryResult, CreateInfiniteQueryResult, DefinedCreateInfiniteQueryResult, CreateMutateFunction, CreateMutateAsyncFunction, CreateBaseMutationResult, BaseMutationNarrowing, CreateMutationResult, } from './types.js';
|
|
3
3
|
export type { QueryResource } from './query-resource.js';
|
|
4
4
|
export { toResource } from './query-resource.js';
|
|
5
5
|
export type { DefinedInitialDataOptions, UndefinedInitialDataOptions, UnusedSkipTokenOptions, } from './query-options.js';
|
package/index.mjs
CHANGED
|
@@ -12,9 +12,8 @@ import { injectQueries } from "./inject-queries.mjs";
|
|
|
12
12
|
import { injectQuery } from "./inject-query.mjs";
|
|
13
13
|
import { provideTanStackQuery, withHydrationKey, withNoQueryHydration } from "./providers.mjs";
|
|
14
14
|
export * from "@tanstack/query-core";
|
|
15
|
-
|
|
15
|
+
// src/index.ts
|
|
16
16
|
/* istanbul ignore file */
|
|
17
|
-
//#endregion
|
|
18
17
|
export { infiniteQueryOptions, injectInfiniteQuery, injectIsFetching, injectIsMutating, injectIsRestoring, injectMutation, injectMutationState, injectQueries, injectQuery, mutationOptions, provideTanStackQuery, queryOptions, toResource, withHydrationKey, withNoQueryHydration };
|
|
19
18
|
|
|
20
19
|
//# sourceMappingURL=index.mjs.map
|
package/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\n// Re-export core\nexport * from '@tanstack/query-core'\n\nexport
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\n// Re-export core\n// oxlint-disable-next-line tanstack-query/no-restricted-syntax\nexport * from '@tanstack/query-core'\n\nexport type {\n CreateQueryOptions,\n BaseQueryNarrowing,\n DefinedQueryNarrowing,\n BaseInfiniteQueryNarrowing,\n DefinedInfiniteQueryNarrowing,\n CreateInfiniteQueryOptions,\n CreateBaseQueryResult,\n CreateQueryResult,\n DefinedCreateQueryResult,\n CreateInfiniteQueryResult,\n DefinedCreateInfiniteQueryResult,\n CreateMutateFunction,\n CreateMutateAsyncFunction,\n CreateBaseMutationResult,\n BaseMutationNarrowing,\n CreateMutationResult,\n} from './types'\nexport type { QueryResource } from './query-resource'\nexport { toResource } from './query-resource'\n\nexport type {\n DefinedInitialDataOptions,\n UndefinedInitialDataOptions,\n UnusedSkipTokenOptions,\n} from './query-options'\nexport { queryOptions } from './query-options'\n\nexport type { CreateMutationOptions } from './types'\nexport { mutationOptions } from './mutation-options'\n\nexport type {\n DefinedInitialDataInfiniteOptions,\n UndefinedInitialDataInfiniteOptions,\n UnusedSkipTokenInfiniteOptions,\n} from './infinite-query-options'\nexport { infiniteQueryOptions } from './infinite-query-options'\n\nexport { injectInfiniteQuery } from './inject-infinite-query'\n\nexport { injectIsFetching } from './inject-is-fetching'\n\nexport { injectIsMutating } from './inject-is-mutating'\n\nexport { injectIsRestoring } from './inject-is-restoring'\n\nexport { injectMutation } from './inject-mutation'\n\nexport type { MutationStateOptions } from './inject-mutation-state'\nexport { injectMutationState } from './inject-mutation-state'\n\nexport type {\n InjectQueriesOptions,\n QueriesOptions,\n QueriesResults,\n} from './inject-queries.types'\nexport { injectQueries } from './inject-queries'\n\nexport { injectQuery } from './inject-query'\n\nexport type { QueryFeature } from './providers'\nexport {\n provideTanStackQuery,\n withHydrationKey,\n withNoQueryHydration,\n} from './providers'\n"],"mappings":""}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { DefaultError, InfiniteData, InitialDataFunction, NonUndefinedGuard, OmitKeyof, QueryKey, QueryKeyWithDataTag, SkipToken } from '@tanstack/query-core';
|
|
2
2
|
import { CreateInfiniteQueryOptions } from './types.js';
|
|
3
3
|
/**
|
|
4
|
-
* The options accepted by the `infiniteQueryOptions` overload selected when
|
|
5
|
-
* may be `undefined` while the query is
|
|
4
|
+
* The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is omitted, may be
|
|
5
|
+
* `undefined`, or is a function that may return `undefined` — `data` may be `undefined` while the query is
|
|
6
|
+
* `pending`.
|
|
6
7
|
* @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
|
|
7
8
|
* @template TError - The type of errors your `queryFn` may throw.
|
|
8
9
|
* @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,
|
|
@@ -21,9 +22,9 @@ export type UndefinedInitialDataInfiniteOptions<TQueryFnData, TError = DefaultEr
|
|
|
21
22
|
initialData?: undefined | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>> | InitialDataFunction<NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>>;
|
|
22
23
|
};
|
|
23
24
|
/**
|
|
24
|
-
* The options accepted by the `infiniteQueryOptions` overload selected when
|
|
25
|
-
* `
|
|
26
|
-
* `skipToken`.
|
|
25
|
+
* The options accepted by the `infiniteQueryOptions` overload selected when `queryFn` is not `skipToken` and
|
|
26
|
+
* `initialData` is omitted, may be `undefined`, or is a function that may return `undefined` — same as
|
|
27
|
+
* {@link UndefinedInitialDataInfiniteOptions}, but `queryFn` may not be `skipToken`.
|
|
27
28
|
* @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
|
|
28
29
|
* @template TError - The type of errors your `queryFn` may throw.
|
|
29
30
|
* @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,
|
|
@@ -33,16 +34,17 @@ export type UndefinedInitialDataInfiniteOptions<TQueryFnData, TError = DefaultEr
|
|
|
33
34
|
*/
|
|
34
35
|
export type UnusedSkipTokenInfiniteOptions<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> = OmitKeyof<CreateInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, 'queryFn'> & {
|
|
35
36
|
/**
|
|
36
|
-
* `skipToken` is not allowed as a value here — this overload is selected when
|
|
37
|
-
* you don't intend to run the query yet,
|
|
38
|
-
*
|
|
39
|
-
*
|
|
37
|
+
* `skipToken` is not allowed as a value here — this overload is selected when `initialData` is omitted, may
|
|
38
|
+
* be `undefined`, or is a function that may return `undefined`. If you don't intend to run the query yet,
|
|
39
|
+
* set `enabled: false` — omitting `queryFn` alone still triggers a fetch that fails with "Missing queryFn"
|
|
40
|
+
* unless `enabled` is `false` or a default query function has been defined. A default query function only
|
|
41
|
+
* supplies `queryFn`; it doesn't defer the fetch on its own.
|
|
40
42
|
*/
|
|
41
43
|
queryFn?: Exclude<CreateInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>['queryFn'], SkipToken | undefined>;
|
|
42
44
|
};
|
|
43
45
|
/**
|
|
44
|
-
* The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is
|
|
45
|
-
* never `undefined` (unless a `select` changes `TData` to include `undefined`).
|
|
46
|
+
* The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is known to be
|
|
47
|
+
* defined — `data` is never `undefined` (unless a `select` changes `TData` to include `undefined`).
|
|
46
48
|
* @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
|
|
47
49
|
* @template TError - The type of errors your `queryFn` may throw.
|
|
48
50
|
* @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,
|
|
@@ -66,9 +68,9 @@ export type DefinedInitialDataInfiniteOptions<TQueryFnData, TError = DefaultErro
|
|
|
66
68
|
* `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options
|
|
67
69
|
* for.
|
|
68
70
|
*
|
|
69
|
-
* This overload is selected when `initialData` is
|
|
71
|
+
* This overload is selected when `initialData` is known to be defined.
|
|
70
72
|
* @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to
|
|
71
|
-
* `injectInfiniteQuery`, with `initialData`
|
|
73
|
+
* `injectInfiniteQuery`, with `initialData` known to be defined.
|
|
72
74
|
* @returns The same options object, typed so that `queryKey` carries the inferred data type.
|
|
73
75
|
* @remarks See {@link injectInfiniteQuery} for examples that fetch further pages, from a button click or
|
|
74
76
|
* automatically as the user scrolls.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infinite-query-options.mjs","names":[],"sources":["../src/infinite-query-options.ts"],"sourcesContent":["import type {\n DefaultError,\n InfiniteData,\n InitialDataFunction,\n NonUndefinedGuard,\n OmitKeyof,\n QueryKey,\n QueryKeyWithDataTag,\n SkipToken,\n} from '@tanstack/query-core'\nimport type { CreateInfiniteQueryOptions } from './types'\n\n/**\n * The options accepted by the `infiniteQueryOptions` overload selected when no `initialData` is set — `data`\n * may be `undefined` while the query is `pending`.\n * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.\n * @template TError - The type of errors your `queryFn` may throw.\n * @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,\n * the shape of all fetched pages plus their page params.\n * @template TQueryKey - The type of your `queryKey`.\n * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.\n */\nexport type UndefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n> = CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n> & {\n /**\n * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been\n * created or cached yet). If set to a function, the function will be called **once** during the shared/root\n * query initialization, and be expected to synchronously return the initial data. Initial data is\n * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the\n * cache.\n */\n initialData?:\n | undefined\n | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>\n | InitialDataFunction<\n NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>\n >\n}\n\n/**\n * The options accepted by the `infiniteQueryOptions` overload selected when no `initialData` is set and\n * `queryFn` is not `skipToken` — same as {@link UndefinedInitialDataInfiniteOptions}, but `queryFn` may not be\n * `skipToken`.\n * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.\n * @template TError - The type of errors your `queryFn` may throw.\n * @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,\n * the shape of all fetched pages plus their page params.\n * @template TQueryKey - The type of your `queryKey`.\n * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.\n */\nexport type UnusedSkipTokenInfiniteOptions<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n> = OmitKeyof<\n CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n 'queryFn'\n> & {\n /**\n * `skipToken` is not allowed as a value here — this overload is selected when no `initialData` is set. If\n * you don't intend to run the query yet, set `enabled: false` — omitting `queryFn` alone still triggers a\n * fetch that fails with \"Missing queryFn\" unless `enabled` is `false` or a default query function has been\n * defined. A default query function only supplies `queryFn`; it doesn't defer the fetch on its own.\n */\n queryFn?: Exclude<\n CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >['queryFn'],\n SkipToken | undefined\n >\n}\n\n/**\n * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is set — `data` is\n * never `undefined` (unless a `select` changes `TData` to include `undefined`).\n * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.\n * @template TError - The type of errors your `queryFn` may throw.\n * @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,\n * the shape of all fetched pages plus their page params.\n * @template TQueryKey - The type of your `queryKey`.\n * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.\n */\nexport type DefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n> = CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n> & {\n /**\n * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been\n * created or cached yet). If set to a function, the function will be called **once** during the shared/root\n * query initialization, and be expected to synchronously return the initial data. Initial data is\n * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the\n * cache.\n */\n initialData:\n | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>\n | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)\n}\n\n/**\n * You can generally pass everything to `infiniteQueryOptions` that you can also pass to\n * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as\n * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options\n * for.\n *\n * This overload is selected when `initialData` is set.\n * @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to\n * `injectInfiniteQuery`, with `initialData` set.\n * @returns The same options object, typed so that `queryKey` carries the inferred data type.\n * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages, from a button click or\n * automatically as the user scrolls.\n * @see {@link injectInfiniteQuery} to run an infinite query with these options.\n * @example\n * ```angular-ts\n * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query'\n *\n * export const projectsOptions = infiniteQueryOptions({\n * queryKey: ['projects'],\n * queryFn: ({ pageParam }) => fetchProjects(pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * initialData: { pages: [], pageParams: [] },\n * })\n *\n * @Component({\n * selector: 'projects',\n * template: `\n * <!-- `projectsQuery.data()` is never `undefined`, thanks to `initialData` — even if a\n * refetch fails, so the list stays visible alongside the error. -->\n * <ul>\n * @for (page of projectsQuery.data().pages; track $index) {\n * @for (project of page.projects; track project.id) {\n * <li>{{ project.name }}</li>\n * }\n * }\n * </ul>\n * `,\n * })\n * export class Projects {\n * readonly projectsQuery = injectInfiniteQuery(() => projectsOptions)\n * }\n * ```\n */\nexport function infiniteQueryOptions<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n options: DefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n): DefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n> &\n QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>\n\n/**\n * You can generally pass everything to `infiniteQueryOptions` that you can also pass to\n * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as\n * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options\n * for.\n * @param options - The {@link UnusedSkipTokenInfiniteOptions} to use — everything you can pass to\n * `injectInfiniteQuery`.\n * @returns The same options object, typed so that `queryKey` carries the inferred data type.\n * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages, from a button click or\n * automatically as the user scrolls.\n * @see {@link injectInfiniteQuery} to run an infinite query with these options.\n * @example\n * A parameterized factory, so the same options object can be reused per `postId`:\n * ```angular-ts\n * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query'\n *\n * export const commentsOptions = (postId: string) =>\n * infiniteQueryOptions({\n * queryKey: ['post', postId, 'comments'],\n * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * })\n *\n * @Component({\n * selector: 'comments',\n * template: `\n * @if (commentsQuery.isPending()) {\n * Loading...\n * } @else if (commentsQuery.isError()) {\n * <span>Error: {{ commentsQuery.error()?.message }}</span>\n * } @else {\n * <ul>\n * @for (page of commentsQuery.data().pages; track $index) {\n * @for (comment of page.comments; track comment.id) {\n * <li>{{ comment.text }}</li>\n * }\n * }\n * </ul>\n * }\n * `,\n * })\n * export class Comments {\n * readonly postId = signal('1')\n * readonly commentsQuery = injectInfiniteQuery(() => commentsOptions(this.postId()))\n * }\n * ```\n */\nexport function infiniteQueryOptions<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n options: UnusedSkipTokenInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n): UnusedSkipTokenInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n> &\n QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>\n\n/**\n * You can generally pass everything to `infiniteQueryOptions` that you can also pass to\n * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as\n * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options\n * for.\n * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to\n * `injectInfiniteQuery`.\n * @returns The same options object, typed so that `queryKey` carries the inferred data type.\n * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages (from a button click or\n * automatically as the user scrolls) and that use `skipToken` to disable the query until `postId` is set.\n * @see {@link injectInfiniteQuery} to run an infinite query with these options.\n * @example\n * A parameterized factory, so the same options object can be reused per `postId`:\n * ```angular-ts\n * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query'\n *\n * export const commentsOptions = (postId: string) =>\n * infiniteQueryOptions({\n * queryKey: ['post', postId, 'comments'],\n * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * })\n *\n * @Component({\n * selector: 'comments',\n * template: `\n * @if (commentsQuery.isPending()) {\n * Loading...\n * } @else if (commentsQuery.isError()) {\n * <span>Error: {{ commentsQuery.error()?.message }}</span>\n * } @else {\n * <ul>\n * @for (page of commentsQuery.data().pages; track $index) {\n * @for (comment of page.comments; track comment.id) {\n * <li>{{ comment.text }}</li>\n * }\n * }\n * </ul>\n * }\n * `,\n * })\n * export class Comments {\n * readonly postId = signal('1')\n * readonly commentsQuery = injectInfiniteQuery(() => commentsOptions(this.postId()))\n * }\n * ```\n */\nexport function infiniteQueryOptions<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n options: UndefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n): UndefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n> &\n QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>\n\nexport function infiniteQueryOptions(options: unknown) {\n return options\n}\n"],"mappings":";AAmVA,SAAgB,qBAAqB,SAAkB;CACrD,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"infinite-query-options.mjs","names":[],"sources":["../src/infinite-query-options.ts"],"sourcesContent":["import type {\n DefaultError,\n InfiniteData,\n InitialDataFunction,\n NonUndefinedGuard,\n OmitKeyof,\n QueryKey,\n QueryKeyWithDataTag,\n SkipToken,\n} from '@tanstack/query-core'\nimport type { CreateInfiniteQueryOptions } from './types'\n\n/**\n * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is omitted, may be\n * `undefined`, or is a function that may return `undefined` — `data` may be `undefined` while the query is\n * `pending`.\n * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.\n * @template TError - The type of errors your `queryFn` may throw.\n * @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,\n * the shape of all fetched pages plus their page params.\n * @template TQueryKey - The type of your `queryKey`.\n * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.\n */\nexport type UndefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n> = CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n> & {\n /**\n * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been\n * created or cached yet). If set to a function, the function will be called **once** during the shared/root\n * query initialization, and be expected to synchronously return the initial data. Initial data is\n * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the\n * cache.\n */\n initialData?:\n | undefined\n | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>\n | InitialDataFunction<\n NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>\n >\n}\n\n/**\n * The options accepted by the `infiniteQueryOptions` overload selected when `queryFn` is not `skipToken` and\n * `initialData` is omitted, may be `undefined`, or is a function that may return `undefined` — same as\n * {@link UndefinedInitialDataInfiniteOptions}, but `queryFn` may not be `skipToken`.\n * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.\n * @template TError - The type of errors your `queryFn` may throw.\n * @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,\n * the shape of all fetched pages plus their page params.\n * @template TQueryKey - The type of your `queryKey`.\n * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.\n */\nexport type UnusedSkipTokenInfiniteOptions<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n> = OmitKeyof<\n CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n 'queryFn'\n> & {\n /**\n * `skipToken` is not allowed as a value here — this overload is selected when `initialData` is omitted, may\n * be `undefined`, or is a function that may return `undefined`. If you don't intend to run the query yet,\n * set `enabled: false` — omitting `queryFn` alone still triggers a fetch that fails with \"Missing queryFn\"\n * unless `enabled` is `false` or a default query function has been defined. A default query function only\n * supplies `queryFn`; it doesn't defer the fetch on its own.\n */\n queryFn?: Exclude<\n CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >['queryFn'],\n SkipToken | undefined\n >\n}\n\n/**\n * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is known to be\n * defined — `data` is never `undefined` (unless a `select` changes `TData` to include `undefined`).\n * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.\n * @template TError - The type of errors your `queryFn` may throw.\n * @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,\n * the shape of all fetched pages plus their page params.\n * @template TQueryKey - The type of your `queryKey`.\n * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.\n */\nexport type DefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n> = CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n> & {\n /**\n * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been\n * created or cached yet). If set to a function, the function will be called **once** during the shared/root\n * query initialization, and be expected to synchronously return the initial data. Initial data is\n * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the\n * cache.\n */\n initialData:\n | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>\n | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)\n}\n\n/**\n * You can generally pass everything to `infiniteQueryOptions` that you can also pass to\n * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as\n * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options\n * for.\n *\n * This overload is selected when `initialData` is known to be defined.\n * @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to\n * `injectInfiniteQuery`, with `initialData` known to be defined.\n * @returns The same options object, typed so that `queryKey` carries the inferred data type.\n * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages, from a button click or\n * automatically as the user scrolls.\n * @see {@link injectInfiniteQuery} to run an infinite query with these options.\n * @example\n * ```angular-ts\n * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query'\n *\n * export const projectsOptions = infiniteQueryOptions({\n * queryKey: ['projects'],\n * queryFn: ({ pageParam }) => fetchProjects(pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * initialData: { pages: [], pageParams: [] },\n * })\n *\n * @Component({\n * selector: 'projects',\n * template: `\n * <!-- `projectsQuery.data()` is never `undefined`, thanks to `initialData` — even if a\n * refetch fails, so the list stays visible alongside the error. -->\n * <ul>\n * @for (page of projectsQuery.data().pages; track $index) {\n * @for (project of page.projects; track project.id) {\n * <li>{{ project.name }}</li>\n * }\n * }\n * </ul>\n * `,\n * })\n * export class Projects {\n * readonly projectsQuery = injectInfiniteQuery(() => projectsOptions)\n * }\n * ```\n */\nexport function infiniteQueryOptions<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n options: DefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n): DefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n> &\n QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>\n\n/**\n * You can generally pass everything to `infiniteQueryOptions` that you can also pass to\n * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as\n * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options\n * for.\n * @param options - The {@link UnusedSkipTokenInfiniteOptions} to use — everything you can pass to\n * `injectInfiniteQuery`.\n * @returns The same options object, typed so that `queryKey` carries the inferred data type.\n * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages, from a button click or\n * automatically as the user scrolls.\n * @see {@link injectInfiniteQuery} to run an infinite query with these options.\n * @example\n * A parameterized factory, so the same options object can be reused per `postId`:\n * ```angular-ts\n * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query'\n *\n * export const commentsOptions = (postId: string) =>\n * infiniteQueryOptions({\n * queryKey: ['post', postId, 'comments'],\n * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * })\n *\n * @Component({\n * selector: 'comments',\n * template: `\n * @if (commentsQuery.isPending()) {\n * Loading...\n * } @else if (commentsQuery.isError()) {\n * <span>Error: {{ commentsQuery.error()?.message }}</span>\n * } @else {\n * <ul>\n * @for (page of commentsQuery.data().pages; track $index) {\n * @for (comment of page.comments; track comment.id) {\n * <li>{{ comment.text }}</li>\n * }\n * }\n * </ul>\n * }\n * `,\n * })\n * export class Comments {\n * readonly postId = signal('1')\n * readonly commentsQuery = injectInfiniteQuery(() => commentsOptions(this.postId()))\n * }\n * ```\n */\nexport function infiniteQueryOptions<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n options: UnusedSkipTokenInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n): UnusedSkipTokenInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n> &\n QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>\n\n/**\n * You can generally pass everything to `infiniteQueryOptions` that you can also pass to\n * `injectInfiniteQuery`. These options can be shared across functions and imperative APIs such as\n * `queryClient.fetchInfiniteQuery`. `options.queryKey` is required and is the query key to generate options\n * for.\n * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to\n * `injectInfiniteQuery`.\n * @returns The same options object, typed so that `queryKey` carries the inferred data type.\n * @remarks See {@link injectInfiniteQuery} for examples that fetch further pages (from a button click or\n * automatically as the user scrolls) and that use `skipToken` to disable the query until `postId` is set.\n * @see {@link injectInfiniteQuery} to run an infinite query with these options.\n * @example\n * A parameterized factory, so the same options object can be reused per `postId`:\n * ```angular-ts\n * import { infiniteQueryOptions, injectInfiniteQuery } from '@tanstack/angular-query'\n *\n * export const commentsOptions = (postId: string) =>\n * infiniteQueryOptions({\n * queryKey: ['post', postId, 'comments'],\n * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * })\n *\n * @Component({\n * selector: 'comments',\n * template: `\n * @if (commentsQuery.isPending()) {\n * Loading...\n * } @else if (commentsQuery.isError()) {\n * <span>Error: {{ commentsQuery.error()?.message }}</span>\n * } @else {\n * <ul>\n * @for (page of commentsQuery.data().pages; track $index) {\n * @for (comment of page.comments; track comment.id) {\n * <li>{{ comment.text }}</li>\n * }\n * }\n * </ul>\n * }\n * `,\n * })\n * export class Comments {\n * readonly postId = signal('1')\n * readonly commentsQuery = injectInfiniteQuery(() => commentsOptions(this.postId()))\n * }\n * ```\n */\nexport function infiniteQueryOptions<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n options: UndefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n): UndefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n> &\n QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>\n\nexport function infiniteQueryOptions(options: unknown) {\n return options\n}\n"],"mappings":";AAqVA,SAAgB,qBAAqB,SAAkB;CACrD,OAAO;AACT"}
|
package/inject-base-query.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { injectQueryZone } from "./utils/inject-query-zone.mjs";
|
|
2
1
|
import { injectIsRestoring } from "./inject-is-restoring.mjs";
|
|
3
|
-
import {
|
|
2
|
+
import { injectQueryZone } from "./utils/inject-query-zone.mjs";
|
|
4
3
|
import { injectExternalStore } from "./utils/inject-external-store.mjs";
|
|
4
|
+
import { injectPendingTasksLifecycle } from "./utils/inject-pending-tasks-lifecycle.mjs";
|
|
5
5
|
import { QueryClient } from "@tanstack/query-core";
|
|
6
6
|
import { computed, effect, inject, untracked } from "@angular/core";
|
|
7
|
-
|
|
7
|
+
// src/inject-base-query.ts
|
|
8
8
|
/**
|
|
9
9
|
* Base implementation for `injectQuery` and `injectInfiniteQuery`.
|
|
10
10
|
* @param optionsFn - Produces the reactive query options.
|
|
@@ -62,7 +62,6 @@ function injectBaseQuery(optionsFn, Observer) {
|
|
|
62
62
|
});
|
|
63
63
|
return [resultSignal, getObserver];
|
|
64
64
|
}
|
|
65
|
-
//#endregion
|
|
66
65
|
export { injectBaseQuery };
|
|
67
66
|
|
|
68
67
|
//# sourceMappingURL=inject-base-query.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject-base-query.mjs","names":[],"sources":["../src/inject-base-query.ts"],"sourcesContent":["import { computed, effect, inject, untracked } from '@angular/core'\nimport { QueryClient } from '@tanstack/query-core'\nimport {
|
|
1
|
+
{"version":3,"file":"inject-base-query.mjs","names":[],"sources":["../src/inject-base-query.ts"],"sourcesContent":["import { computed, effect, inject, untracked } from '@angular/core'\nimport { QueryClient } from '@tanstack/query-core'\nimport { injectIsRestoring } from './inject-is-restoring'\nimport { injectExternalStore } from './utils/inject-external-store'\nimport { injectPendingTasksLifecycle } from './utils/inject-pending-tasks-lifecycle'\nimport { injectQueryZone } from './utils/inject-query-zone'\nimport type {\n QueryKey,\n QueryObserver,\n QueryObserverOptions,\n QueryObserverResult,\n} from '@tanstack/query-core'\n\n/**\n * Base implementation for `injectQuery` and `injectInfiniteQuery`.\n * @param optionsFn - Produces the reactive query options.\n * @param Observer - The core observer constructor for this query kind.\n * @returns The current result signal and a function that reads its active observer.\n */\nexport function injectBaseQuery<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey extends QueryKey,\n>(\n optionsFn: () => QueryObserverOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n Observer: typeof QueryObserver,\n) {\n const outsideZone = injectQueryZone()\n const queryClient = inject(QueryClient)\n const isRestoring = injectIsRestoring()\n const lifecycle = injectPendingTasksLifecycle()\n\n const shouldBlockPendingTasks = (\n observer: QueryObserver<TQueryFnData, TError, TData, TQueryData, TQueryKey>,\n result: QueryObserverResult<TData, TError>,\n ) => {\n return (\n observer.getCurrentQuery().state.fetchStatus !== 'idle' ||\n (result.fetchStatus !== 'idle' && result.isEnabled)\n )\n }\n\n const defaultedOptionsSignal = computed(() => {\n const defaultedOptions = queryClient.defaultQueryOptions(optionsFn())\n defaultedOptions._optimisticResults = isRestoring()\n ? 'isRestoring'\n : 'optimistic'\n defaultedOptions.notifyOnChangeProps = 'all'\n return defaultedOptions\n })\n\n const observerSignal = computed(\n () => new Observer(queryClient, untracked(defaultedOptionsSignal)),\n )\n\n effect(() => {\n const options = defaultedOptionsSignal()\n outsideZone(() => untracked(() => observerSignal().setOptions(options)))\n })\n\n const resultSignal = injectExternalStore(() => {\n const observer = observerSignal()\n const restoring = isRestoring()\n return {\n getSnapshot: () => observer.getOptimisticResult(defaultedOptionsSignal()),\n subscribe: restoring\n ? undefined\n : (onStoreChange) => {\n const unsubscribe = observer.subscribe(() => {\n if (lifecycle.destroyed) return\n onStoreChange()\n })\n return () => {\n lifecycle.setPending(false)\n unsubscribe()\n }\n },\n }\n })\n\n effect((onCleanup) => {\n const result = resultSignal()\n if (isRestoring()) {\n lifecycle.setPending(false)\n return\n }\n\n lifecycle.setPending(shouldBlockPendingTasks(observerSignal(), result))\n onCleanup(() => lifecycle.setPending(false))\n })\n\n // Imperative methods use current options before starting work, even when\n // invoked before the subscription effect runs.\n const getObserver = () =>\n untracked(() => {\n const observer = observerSignal()\n observer.setOptions(defaultedOptionsSignal())\n return observer\n })\n\n return [resultSignal, getObserver] as const\n}\n"],"mappings":";;;;;;;;;;;;;AAmBA,SAAgB,gBAOd,WAOA,UACA;CACA,MAAM,cAAc,gBAAgB;CACpC,MAAM,cAAc,OAAO,WAAW;CACtC,MAAM,cAAc,kBAAkB;CACtC,MAAM,YAAY,4BAA4B;CAE9C,MAAM,2BACJ,UACA,WACG;EACH,OACE,SAAS,gBAAgB,CAAC,CAAC,MAAM,gBAAgB,UAChD,OAAO,gBAAgB,UAAU,OAAO;CAE7C;CAEA,MAAM,yBAAyB,eAAe;EAC5C,MAAM,mBAAmB,YAAY,oBAAoB,UAAU,CAAC;EACpE,iBAAiB,qBAAqB,YAAY,IAC9C,gBACA;EACJ,iBAAiB,sBAAsB;EACvC,OAAO;CACT,CAAC;CAED,MAAM,iBAAiB,eACf,IAAI,SAAS,aAAa,UAAU,sBAAsB,CAAC,CACnE;CAEA,aAAa;EACX,MAAM,UAAU,uBAAuB;EACvC,kBAAkB,gBAAgB,eAAe,CAAC,CAAC,WAAW,OAAO,CAAC,CAAC;CACzE,CAAC;CAED,MAAM,eAAe,0BAA0B;EAC7C,MAAM,WAAW,eAAe;EAEhC,OAAO;GACL,mBAAmB,SAAS,oBAAoB,uBAAuB,CAAC;GACxE,WAHgB,YAGL,IACP,KAAA,KACC,kBAAkB;IACjB,MAAM,cAAc,SAAS,gBAAgB;KAC3C,IAAI,UAAU,WAAW;KACzB,cAAc;IAChB,CAAC;IACD,aAAa;KACX,UAAU,WAAW,KAAK;KAC1B,YAAY;IACd;GACF;EACN;CACF,CAAC;CAED,QAAQ,cAAc;EACpB,MAAM,SAAS,aAAa;EAC5B,IAAI,YAAY,GAAG;GACjB,UAAU,WAAW,KAAK;GAC1B;EACF;EAEA,UAAU,WAAW,wBAAwB,eAAe,GAAG,MAAM,CAAC;EACtE,gBAAgB,UAAU,WAAW,KAAK,CAAC;CAC7C,CAAC;CAID,MAAM,oBACJ,gBAAgB;EACd,MAAM,WAAW,eAAe;EAChC,SAAS,WAAW,uBAAuB,CAAC;EAC5C,OAAO;CACT,CAAC;CAEH,OAAO,CAAC,cAAc,WAAW;AACnC"}
|
|
@@ -1,51 +1,187 @@
|
|
|
1
1
|
import { DefaultError, InfiniteData, QueryKey } from '@tanstack/query-core';
|
|
2
|
-
import { CreateInfiniteQueryOptions, CreateInfiniteQueryResult, DefinedCreateInfiniteQueryResult } from './types.js';
|
|
3
2
|
import { DefinedInitialDataInfiniteOptions, UndefinedInitialDataInfiniteOptions } from './infinite-query-options.js';
|
|
3
|
+
import { CreateInfiniteQueryOptions, CreateInfiniteQueryResult, DefinedCreateInfiniteQueryResult } from './types.js';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* The options for `injectInfiniteQuery` are identical to `injectQuery`, with the addition of
|
|
6
|
+
* `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`. Infinite queries can
|
|
7
|
+
* additively "load more" data onto an existing set of data, or "infinite scroll".
|
|
8
|
+
*
|
|
9
|
+
* This overload is selected when `initialData` is known to be defined on the options returned by
|
|
10
|
+
* `optionsFn`, so the resulting `data` signal is never `undefined` (unless a `select` changes
|
|
11
|
+
* `TData` to include `undefined`).
|
|
12
|
+
* @param optionsFn - A function returning the {@link DefinedInitialDataInfiniteOptions} to use —
|
|
13
|
+
* everything you can pass to `injectInfiniteQuery`, with `initialData` known to be defined. Similar to
|
|
14
|
+
* `computed` from Angular, this function runs in the reactive context, so signals read inside it drive the
|
|
15
|
+
* query.
|
|
16
|
+
* @returns The same signals as `injectQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
|
|
17
|
+
* `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data().pages` and
|
|
18
|
+
* `data().pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
|
|
19
|
+
* `InfiniteData<TQueryFnData>` shape.
|
|
20
|
+
* @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
|
|
21
|
+
* refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
|
|
22
|
+
* actions, or add conditions like `hasNextPage() && !isFetching()`.
|
|
10
23
|
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/infinite-queries
|
|
11
|
-
* @see {@link infiniteQueryOptions} to share these options between `injectInfiniteQuery` and
|
|
12
|
-
* `queryClient.infiniteQuery`.
|
|
24
|
+
* @see {@link infiniteQueryOptions} to share these options between `injectInfiniteQuery` and imperative APIs
|
|
25
|
+
* like `queryClient.infiniteQuery`.
|
|
26
|
+
* @example
|
|
27
|
+
* ```angular-ts
|
|
28
|
+
* @Component({
|
|
29
|
+
* selector: 'projects',
|
|
30
|
+
* template: `
|
|
31
|
+
* <!-- projectsQuery.data() is never undefined, thanks to initialData — even if a
|
|
32
|
+
* refetch fails, so the list stays visible alongside the error. -->
|
|
33
|
+
* <ul>
|
|
34
|
+
* @for (page of projectsQuery.data().pages; track $index) {
|
|
35
|
+
* @for (project of page.projects; track project.id) {
|
|
36
|
+
* <li>{{ project.name }}</li>
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
* </ul>
|
|
40
|
+
* `,
|
|
41
|
+
* })
|
|
42
|
+
* export class Projects {
|
|
43
|
+
* readonly projectsQuery = injectInfiniteQuery(() => ({
|
|
44
|
+
* queryKey: ['projects'],
|
|
45
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
46
|
+
* initialPageParam: 0,
|
|
47
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
48
|
+
* initialData: { pages: [], pageParams: [] },
|
|
49
|
+
* }))
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
13
52
|
*/
|
|
14
53
|
export declare function injectInfiniteQuery<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(optionsFn: () => DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): DefinedCreateInfiniteQueryResult<TData, TError>;
|
|
15
54
|
/**
|
|
16
|
-
* Injects an infinite query: a declarative dependency on an asynchronous source of data that is tied to a
|
|
17
|
-
* Infinite queries can additively "load more" data onto an existing set of data or
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
55
|
+
* Injects an infinite query: a declarative dependency on an asynchronous source of data that is tied to a
|
|
56
|
+
* unique key. Infinite queries can additively "load more" data onto an existing set of data, or
|
|
57
|
+
* "infinite scroll".
|
|
58
|
+
* @param optionsFn - A function returning the {@link UndefinedInitialDataInfiniteOptions} to use
|
|
59
|
+
* — everything you can pass to `injectInfiniteQuery`. Similar to `computed` from Angular, this function runs
|
|
60
|
+
* in the reactive context, so signals read inside it drive the query.
|
|
61
|
+
* @returns The same signals as `injectQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
|
|
62
|
+
* `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data().pages` and
|
|
63
|
+
* `data().pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
|
|
64
|
+
* `InfiniteData<TQueryFnData>` shape.
|
|
65
|
+
* @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
|
|
66
|
+
* refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
|
|
67
|
+
* actions, or add conditions like `hasNextPage() && !isFetching()`. `queryFn: skipToken` is also accepted,
|
|
68
|
+
* shown below.
|
|
21
69
|
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/infinite-queries
|
|
22
|
-
* @see {@link infiniteQueryOptions} to share these options between `injectInfiniteQuery` and
|
|
23
|
-
* `queryClient.infiniteQuery`.
|
|
70
|
+
* @see {@link infiniteQueryOptions} to share these options between `injectInfiniteQuery` and imperative APIs
|
|
71
|
+
* like `queryClient.infiniteQuery`.
|
|
24
72
|
* @example
|
|
73
|
+
* Fetching the next page from a button click:
|
|
25
74
|
* ```angular-ts
|
|
26
75
|
* @Component({
|
|
27
|
-
* selector: 'projects',
|
|
76
|
+
* selector: 'projects-list',
|
|
28
77
|
* template: `
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* } @else {
|
|
34
|
-
* @for (page of query.data().pages; track $index) {
|
|
35
|
-
* @for (project of page; track project.id) {
|
|
36
|
-
* <p>{{ project.name }}</p>
|
|
78
|
+
* <ul>
|
|
79
|
+
* @for (page of projectsQuery.data()?.pages; track $index) {
|
|
80
|
+
* @for (project of page.projects; track project.id) {
|
|
81
|
+
* <li>{{ project.name }}</li>
|
|
37
82
|
* }
|
|
38
83
|
* }
|
|
39
|
-
*
|
|
40
|
-
*
|
|
84
|
+
* </ul>
|
|
85
|
+
* <button
|
|
86
|
+
* [disabled]="!projectsQuery.hasNextPage() || projectsQuery.isFetching()"
|
|
87
|
+
* (click)="projectsQuery.fetchNextPage()"
|
|
88
|
+
* >
|
|
89
|
+
* Load More
|
|
90
|
+
* </button>
|
|
41
91
|
* `,
|
|
42
92
|
* })
|
|
43
|
-
* export class
|
|
44
|
-
* readonly
|
|
93
|
+
* export class ProjectsList {
|
|
94
|
+
* readonly projectsQuery = injectInfiniteQuery(() => ({
|
|
45
95
|
* queryKey: ['projects'],
|
|
46
96
|
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
47
97
|
* initialPageParam: 0,
|
|
48
|
-
* getNextPageParam: (lastPage) => lastPage.
|
|
98
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
99
|
+
* }))
|
|
100
|
+
* }
|
|
101
|
+
* ```
|
|
102
|
+
* @example
|
|
103
|
+
* Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a sentinel
|
|
104
|
+
* element after the list:
|
|
105
|
+
* ```angular-ts
|
|
106
|
+
* @Component({
|
|
107
|
+
* selector: 'projects-list',
|
|
108
|
+
* template: `
|
|
109
|
+
* <ul>
|
|
110
|
+
* @for (page of projectsQuery.data()?.pages; track $index) {
|
|
111
|
+
* @for (project of page.projects; track project.id) {
|
|
112
|
+
* <li>{{ project.name }}</li>
|
|
113
|
+
* }
|
|
114
|
+
* }
|
|
115
|
+
* </ul>
|
|
116
|
+
* <div #sentinel>{{ projectsQuery.isFetchingNextPage() ? 'Loading more...' : '' }}</div>
|
|
117
|
+
* `,
|
|
118
|
+
* })
|
|
119
|
+
* export class ProjectsList {
|
|
120
|
+
* readonly sentinel = viewChild<ElementRef<HTMLElement>>('sentinel')
|
|
121
|
+
*
|
|
122
|
+
* readonly projectsQuery = injectInfiniteQuery(() => ({
|
|
123
|
+
* queryKey: ['projects'],
|
|
124
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
125
|
+
* initialPageParam: 0,
|
|
126
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
127
|
+
* }))
|
|
128
|
+
*
|
|
129
|
+
* constructor() {
|
|
130
|
+
* effect((onCleanup) => {
|
|
131
|
+
* const sentinel = this.sentinel()?.nativeElement
|
|
132
|
+
* if (
|
|
133
|
+
* sentinel == null ||
|
|
134
|
+
* !this.projectsQuery.hasNextPage() ||
|
|
135
|
+
* this.projectsQuery.isFetching()
|
|
136
|
+
* ) {
|
|
137
|
+
* return
|
|
138
|
+
* }
|
|
139
|
+
*
|
|
140
|
+
* const observer = new IntersectionObserver(([entry]) => {
|
|
141
|
+
* if (entry?.isIntersecting) this.projectsQuery.fetchNextPage()
|
|
142
|
+
* })
|
|
143
|
+
* observer.observe(sentinel)
|
|
144
|
+
*
|
|
145
|
+
* onCleanup(() => observer.disconnect())
|
|
146
|
+
* })
|
|
147
|
+
* }
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
150
|
+
* @example
|
|
151
|
+
* A query that's disabled, type safe, until `postId` is set — pass `skipToken` as `queryFn` instead of
|
|
152
|
+
* setting `enabled: false`:
|
|
153
|
+
* ```angular-ts
|
|
154
|
+
* @Component({
|
|
155
|
+
* selector: 'comments',
|
|
156
|
+
* template: `
|
|
157
|
+
* @if (postId() == null) {
|
|
158
|
+
* Select a post
|
|
159
|
+
* } @else if (commentsQuery.isPending()) {
|
|
160
|
+
* Loading...
|
|
161
|
+
* } @else if (commentsQuery.isError()) {
|
|
162
|
+
* <span>Error: {{ commentsQuery.error()?.message }}</span>
|
|
163
|
+
* } @else {
|
|
164
|
+
* <ul>
|
|
165
|
+
* @for (page of commentsQuery.data().pages; track $index) {
|
|
166
|
+
* @for (comment of page.comments; track comment.id) {
|
|
167
|
+
* <li>{{ comment.text }}</li>
|
|
168
|
+
* }
|
|
169
|
+
* }
|
|
170
|
+
* </ul>
|
|
171
|
+
* }
|
|
172
|
+
* `,
|
|
173
|
+
* })
|
|
174
|
+
* export class Comments {
|
|
175
|
+
* readonly postId = signal<string | undefined>(undefined)
|
|
176
|
+
*
|
|
177
|
+
* readonly commentsQuery = injectInfiniteQuery(() => ({
|
|
178
|
+
* queryKey: ['post', this.postId(), 'comments'],
|
|
179
|
+
* queryFn:
|
|
180
|
+
* this.postId() != null
|
|
181
|
+
* ? ({ pageParam }) => fetchComments(this.postId()!, pageParam)
|
|
182
|
+
* : skipToken,
|
|
183
|
+
* initialPageParam: 0,
|
|
184
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
49
185
|
* }))
|
|
50
186
|
* }
|
|
51
187
|
* ```
|
|
@@ -53,10 +189,11 @@ export declare function injectInfiniteQuery<TQueryFnData, TError = DefaultError,
|
|
|
53
189
|
export declare function injectInfiniteQuery<TQueryFnData, TError = DefaultError, TData = InfiniteData<TQueryFnData>, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown>(optionsFn: () => UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): CreateInfiniteQueryResult<TData, TError>;
|
|
54
190
|
/**
|
|
55
191
|
* This overload accepts the general {@link CreateInfiniteQueryOptions} shape rather than the
|
|
56
|
-
* `initialData`-aware overloads above, so whether `data` is defined can't be inferred from the call
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
192
|
+
* `initialData`-aware overloads above, so whether `data` is defined can't be inferred from the call site —
|
|
193
|
+
* useful when wrapping `injectInfiniteQuery` in your own helper function that forwards caller-provided
|
|
194
|
+
* options.
|
|
195
|
+
* @param optionsFn - A function that returns infinite query options. Similar to `computed` from
|
|
196
|
+
* Angular, this function runs in the reactive context, so signals read inside it drive the query.
|
|
60
197
|
* @returns The infinite query result.
|
|
61
198
|
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/infinite-queries
|
|
62
199
|
*/
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { injectQueryZone } from "./utils/inject-query-zone.mjs";
|
|
2
2
|
import { injectBaseQuery } from "./inject-base-query.mjs";
|
|
3
|
-
import { signalProxy } from "./utils/signal-proxy.mjs";
|
|
4
3
|
import { infiniteQueryResultFields } from "./utils/result-fields.mjs";
|
|
4
|
+
import { signalProxy } from "./utils/signal-proxy.mjs";
|
|
5
5
|
import { InfiniteQueryObserver } from "@tanstack/query-core";
|
|
6
6
|
import { assertInInjectionContext, untracked } from "@angular/core";
|
|
7
|
-
|
|
7
|
+
// src/inject-infinite-query.ts
|
|
8
8
|
function injectInfiniteQuery(optionsFn) {
|
|
9
9
|
if (typeof ngDevMode === "undefined" || ngDevMode) assertInInjectionContext(injectInfiniteQuery);
|
|
10
10
|
const outsideZone = injectQueryZone();
|
|
@@ -16,7 +16,6 @@ function injectInfiniteQuery(optionsFn) {
|
|
|
16
16
|
fetchPreviousPage: (options) => outsideZone(() => untracked(() => getInfiniteObserver().fetchPreviousPage(options)))
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
|
-
//#endregion
|
|
20
19
|
export { injectInfiniteQuery };
|
|
21
20
|
|
|
22
21
|
//# sourceMappingURL=inject-infinite-query.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject-infinite-query.mjs","names":[],"sources":["../src/inject-infinite-query.ts"],"sourcesContent":["import { InfiniteQueryObserver } from '@tanstack/query-core'\nimport { assertInInjectionContext, untracked } from '@angular/core'\nimport { injectQueryZone } from './utils/inject-query-zone'\nimport { injectBaseQuery } from './inject-base-query'\nimport { signalProxy } from './utils/signal-proxy'\nimport { infiniteQueryResultFields } from './utils/result-fields'\nimport type {\n DefaultError,\n FetchNextPageOptions,\n FetchPreviousPageOptions,\n InfiniteData,\n InfiniteQueryObserverResult,\n QueryKey,\n QueryObserver,\n RefetchOptions,\n} from '@tanstack/query-core'\nimport type { Signal } from '@angular/core'\nimport type {\n CreateInfiniteQueryOptions,\n CreateInfiniteQueryResult,\n DefinedCreateInfiniteQueryResult,\n} from './types'\nimport type {\n DefinedInitialDataInfiniteOptions,\n UndefinedInitialDataInfiniteOptions,\n} from './infinite-query-options'\n\n/**\n * This overload is selected when `initialData` is set, so the resulting `data` signal is never `undefined`\n * (unless a `select` changes `TData` to include `undefined`).\n * @param optionsFn - A function returning infinite-query options with `initialData` set. Similar to\n * `computed` from Angular, this function runs in the reactive context.\n * @returns The infinite query result, typed so that `data` is never `undefined`.\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/infinite-queries\n * @see {@link infiniteQueryOptions} to share these options between `injectInfiniteQuery` and\n * `queryClient.infiniteQuery`.\n */\nexport function injectInfiniteQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n optionsFn: () => DefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n): DefinedCreateInfiniteQueryResult<TData, TError>\n\n/**\n * Injects an infinite query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n * Infinite queries can additively \"load more\" data onto an existing set of data or support infinite scroll.\n * @param optionsFn - A function that returns infinite query options. Similar to `computed` from Angular,\n * this function runs in the reactive context, so signals read inside it drive the query.\n * @returns The infinite query result.\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/infinite-queries\n * @see {@link infiniteQueryOptions} to share these options between `injectInfiniteQuery` and\n * `queryClient.infiniteQuery`.\n * @example\n * ```angular-ts\n * @Component({\n * selector: 'projects',\n * template: `\n * @if (query.isPending()) {\n * Loading...\n * } @else if (query.isError()) {\n * <span>Error: {{ query.error()?.message }}</span>\n * } @else {\n * @for (page of query.data().pages; track $index) {\n * @for (project of page; track project.id) {\n * <p>{{ project.name }}</p>\n * }\n * }\n * <button (click)=\"query.fetchNextPage()\">Load more</button>\n * }\n * `,\n * })\n * export class Projects {\n * readonly query = injectInfiniteQuery(() => ({\n * queryKey: ['projects'],\n * queryFn: ({ pageParam }) => fetchProjects(pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextCursor,\n * }))\n * }\n * ```\n */\nexport function injectInfiniteQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n optionsFn: () => UndefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n): CreateInfiniteQueryResult<TData, TError>\n\n/**\n * This overload accepts the general {@link CreateInfiniteQueryOptions} shape rather than the\n * `initialData`-aware overloads above, so whether `data` is defined can't be inferred from the call\n * site — useful when wrapping `injectInfiniteQuery` in your own helper.\n * @param optionsFn - A function that returns infinite query options. Similar to `computed` from Angular,\n * this function runs in the reactive context, so signals read inside it drive the query.\n * @returns The infinite query result.\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/infinite-queries\n */\nexport function injectInfiniteQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n optionsFn: () => CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n): CreateInfiniteQueryResult<TData, TError>\n\nexport function injectInfiniteQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n optionsFn: () =>\n | DefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >\n | CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n):\n | DefinedCreateInfiniteQueryResult<TData, TError>\n | CreateInfiniteQueryResult<TData, TError> {\n if (typeof ngDevMode === 'undefined' || ngDevMode) {\n assertInInjectionContext(injectInfiniteQuery)\n }\n const outsideZone = injectQueryZone()\n const [resultSignal, getObserver] = injectBaseQuery(\n optionsFn,\n InfiniteQueryObserver as typeof QueryObserver,\n )\n const getInfiniteObserver = () =>\n getObserver() as InfiniteQueryObserver<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >\n return Object.assign(\n signalProxy(\n resultSignal as Signal<InfiniteQueryObserverResult<TData, TError>>,\n infiniteQueryResultFields,\n ),\n {\n refetch: (options?: RefetchOptions) =>\n outsideZone(() =>\n untracked(() => getInfiniteObserver().refetch(options)),\n ),\n fetchNextPage: (options?: FetchNextPageOptions) =>\n outsideZone(() =>\n untracked(() => getInfiniteObserver().fetchNextPage(options)),\n ),\n fetchPreviousPage: (options?: FetchPreviousPageOptions) =>\n outsideZone(() =>\n untracked(() => getInfiniteObserver().fetchPreviousPage(options)),\n ),\n },\n ) as unknown as\n | DefinedCreateInfiniteQueryResult<TData, TError>\n | CreateInfiniteQueryResult<TData, TError>\n}\n"],"mappings":";;;;;;;AAoIA,SAAgB,oBAOd,WAiB2C;CAC3C,IAAI,OAAO,cAAc,eAAe,WACtC,yBAAyB,mBAAmB;CAE9C,MAAM,cAAc,gBAAgB;CACpC,MAAM,CAAC,cAAc,eAAe,gBAClC,WACA,qBACF;CACA,MAAM,4BACJ,YAAY;CAOd,OAAO,OAAO,OACZ,YACE,cACA,yBACF,GACA;EACE,UAAU,YACR,kBACE,gBAAgB,oBAAoB,CAAC,CAAC,QAAQ,OAAO,CAAC,CACxD;EACF,gBAAgB,YACd,kBACE,gBAAgB,oBAAoB,CAAC,CAAC,cAAc,OAAO,CAAC,CAC9D;EACF,oBAAoB,YAClB,kBACE,gBAAgB,oBAAoB,CAAC,CAAC,kBAAkB,OAAO,CAAC,CAClE;CACJ,CACF;AAGF"}
|
|
1
|
+
{"version":3,"file":"inject-infinite-query.mjs","names":[],"sources":["../src/inject-infinite-query.ts"],"sourcesContent":["import { assertInInjectionContext, untracked } from '@angular/core'\nimport { InfiniteQueryObserver } from '@tanstack/query-core'\nimport { injectBaseQuery } from './inject-base-query'\nimport { injectQueryZone } from './utils/inject-query-zone'\nimport { infiniteQueryResultFields } from './utils/result-fields'\nimport { signalProxy } from './utils/signal-proxy'\nimport type { Signal } from '@angular/core'\nimport type {\n DefaultError,\n FetchNextPageOptions,\n FetchPreviousPageOptions,\n InfiniteData,\n InfiniteQueryObserverResult,\n QueryKey,\n QueryObserver,\n RefetchOptions,\n} from '@tanstack/query-core'\nimport type {\n DefinedInitialDataInfiniteOptions,\n UndefinedInitialDataInfiniteOptions,\n} from './infinite-query-options'\nimport type {\n CreateInfiniteQueryOptions,\n CreateInfiniteQueryResult,\n DefinedCreateInfiniteQueryResult,\n} from './types'\n\n/**\n * The options for `injectInfiniteQuery` are identical to `injectQuery`, with the addition of\n * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`. Infinite queries can\n * additively \"load more\" data onto an existing set of data, or \"infinite scroll\".\n *\n * This overload is selected when `initialData` is known to be defined on the options returned by\n * `optionsFn`, so the resulting `data` signal is never `undefined` (unless a `select` changes\n * `TData` to include `undefined`).\n * @param optionsFn - A function returning the {@link DefinedInitialDataInfiniteOptions} to use —\n * everything you can pass to `injectInfiniteQuery`, with `initialData` known to be defined. Similar to\n * `computed` from Angular, this function runs in the reactive context, so signals read inside it drive the\n * query.\n * @returns The same signals as `injectQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,\n * `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data().pages` and\n * `data().pageParams` are also added, as long as a `select` doesn't change `TData` away from its default\n * `InfiniteData<TQueryFnData>` shape.\n * @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default\n * refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user\n * actions, or add conditions like `hasNextPage() && !isFetching()`.\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/infinite-queries\n * @see {@link infiniteQueryOptions} to share these options between `injectInfiniteQuery` and imperative APIs\n * like `queryClient.infiniteQuery`.\n * @example\n * ```angular-ts\n * @Component({\n * selector: 'projects',\n * template: `\n * <!-- projectsQuery.data() is never undefined, thanks to initialData — even if a\n * refetch fails, so the list stays visible alongside the error. -->\n * <ul>\n * @for (page of projectsQuery.data().pages; track $index) {\n * @for (project of page.projects; track project.id) {\n * <li>{{ project.name }}</li>\n * }\n * }\n * </ul>\n * `,\n * })\n * export class Projects {\n * readonly projectsQuery = injectInfiniteQuery(() => ({\n * queryKey: ['projects'],\n * queryFn: ({ pageParam }) => fetchProjects(pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * initialData: { pages: [], pageParams: [] },\n * }))\n * }\n * ```\n */\nexport function injectInfiniteQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n optionsFn: () => DefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n): DefinedCreateInfiniteQueryResult<TData, TError>\n\n/**\n * Injects an infinite query: a declarative dependency on an asynchronous source of data that is tied to a\n * unique key. Infinite queries can additively \"load more\" data onto an existing set of data, or\n * \"infinite scroll\".\n * @param optionsFn - A function returning the {@link UndefinedInitialDataInfiniteOptions} to use\n * — everything you can pass to `injectInfiniteQuery`. Similar to `computed` from Angular, this function runs\n * in the reactive context, so signals read inside it drive the query.\n * @returns The same signals as `injectQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,\n * `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data().pages` and\n * `data().pageParams` are also added, as long as a `select` doesn't change `TData` away from its default\n * `InfiniteData<TQueryFnData>` shape.\n * @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default\n * refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user\n * actions, or add conditions like `hasNextPage() && !isFetching()`. `queryFn: skipToken` is also accepted,\n * shown below.\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/infinite-queries\n * @see {@link infiniteQueryOptions} to share these options between `injectInfiniteQuery` and imperative APIs\n * like `queryClient.infiniteQuery`.\n * @example\n * Fetching the next page from a button click:\n * ```angular-ts\n * @Component({\n * selector: 'projects-list',\n * template: `\n * <ul>\n * @for (page of projectsQuery.data()?.pages; track $index) {\n * @for (project of page.projects; track project.id) {\n * <li>{{ project.name }}</li>\n * }\n * }\n * </ul>\n * <button\n * [disabled]=\"!projectsQuery.hasNextPage() || projectsQuery.isFetching()\"\n * (click)=\"projectsQuery.fetchNextPage()\"\n * >\n * Load More\n * </button>\n * `,\n * })\n * export class ProjectsList {\n * readonly projectsQuery = injectInfiniteQuery(() => ({\n * queryKey: ['projects'],\n * queryFn: ({ pageParam }) => fetchProjects(pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * }))\n * }\n * ```\n * @example\n * Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a sentinel\n * element after the list:\n * ```angular-ts\n * @Component({\n * selector: 'projects-list',\n * template: `\n * <ul>\n * @for (page of projectsQuery.data()?.pages; track $index) {\n * @for (project of page.projects; track project.id) {\n * <li>{{ project.name }}</li>\n * }\n * }\n * </ul>\n * <div #sentinel>{{ projectsQuery.isFetchingNextPage() ? 'Loading more...' : '' }}</div>\n * `,\n * })\n * export class ProjectsList {\n * readonly sentinel = viewChild<ElementRef<HTMLElement>>('sentinel')\n *\n * readonly projectsQuery = injectInfiniteQuery(() => ({\n * queryKey: ['projects'],\n * queryFn: ({ pageParam }) => fetchProjects(pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * }))\n *\n * constructor() {\n * effect((onCleanup) => {\n * const sentinel = this.sentinel()?.nativeElement\n * if (\n * sentinel == null ||\n * !this.projectsQuery.hasNextPage() ||\n * this.projectsQuery.isFetching()\n * ) {\n * return\n * }\n *\n * const observer = new IntersectionObserver(([entry]) => {\n * if (entry?.isIntersecting) this.projectsQuery.fetchNextPage()\n * })\n * observer.observe(sentinel)\n *\n * onCleanup(() => observer.disconnect())\n * })\n * }\n * }\n * ```\n * @example\n * A query that's disabled, type safe, until `postId` is set — pass `skipToken` as `queryFn` instead of\n * setting `enabled: false`:\n * ```angular-ts\n * @Component({\n * selector: 'comments',\n * template: `\n * @if (postId() == null) {\n * Select a post\n * } @else if (commentsQuery.isPending()) {\n * Loading...\n * } @else if (commentsQuery.isError()) {\n * <span>Error: {{ commentsQuery.error()?.message }}</span>\n * } @else {\n * <ul>\n * @for (page of commentsQuery.data().pages; track $index) {\n * @for (comment of page.comments; track comment.id) {\n * <li>{{ comment.text }}</li>\n * }\n * }\n * </ul>\n * }\n * `,\n * })\n * export class Comments {\n * readonly postId = signal<string | undefined>(undefined)\n *\n * readonly commentsQuery = injectInfiniteQuery(() => ({\n * queryKey: ['post', this.postId(), 'comments'],\n * queryFn:\n * this.postId() != null\n * ? ({ pageParam }) => fetchComments(this.postId()!, pageParam)\n * : skipToken,\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * }))\n * }\n * ```\n */\nexport function injectInfiniteQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n optionsFn: () => UndefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n): CreateInfiniteQueryResult<TData, TError>\n\n/**\n * This overload accepts the general {@link CreateInfiniteQueryOptions} shape rather than the\n * `initialData`-aware overloads above, so whether `data` is defined can't be inferred from the call site —\n * useful when wrapping `injectInfiniteQuery` in your own helper function that forwards caller-provided\n * options.\n * @param optionsFn - A function that returns infinite query options. Similar to `computed` from\n * Angular, this function runs in the reactive context, so signals read inside it drive the query.\n * @returns The infinite query result.\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/infinite-queries\n */\nexport function injectInfiniteQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n optionsFn: () => CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n): CreateInfiniteQueryResult<TData, TError>\n\nexport function injectInfiniteQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n optionsFn: () =>\n | DefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >\n | CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n):\n | DefinedCreateInfiniteQueryResult<TData, TError>\n | CreateInfiniteQueryResult<TData, TError> {\n if (typeof ngDevMode === 'undefined' || ngDevMode) {\n assertInInjectionContext(injectInfiniteQuery)\n }\n const outsideZone = injectQueryZone()\n const [resultSignal, getObserver] = injectBaseQuery(\n optionsFn,\n InfiniteQueryObserver as typeof QueryObserver,\n )\n const getInfiniteObserver = () =>\n getObserver() as InfiniteQueryObserver<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >\n return Object.assign(\n signalProxy(\n resultSignal as Signal<InfiniteQueryObserverResult<TData, TError>>,\n infiniteQueryResultFields,\n ),\n {\n refetch: (options?: RefetchOptions) =>\n outsideZone(() =>\n untracked(() => getInfiniteObserver().refetch(options)),\n ),\n fetchNextPage: (options?: FetchNextPageOptions) =>\n outsideZone(() =>\n untracked(() => getInfiniteObserver().fetchNextPage(options)),\n ),\n fetchPreviousPage: (options?: FetchPreviousPageOptions) =>\n outsideZone(() =>\n untracked(() => getInfiniteObserver().fetchPreviousPage(options)),\n ),\n },\n ) as unknown as\n | DefinedCreateInfiniteQueryResult<TData, TError>\n | CreateInfiniteQueryResult<TData, TError>\n}\n"],"mappings":";;;;;;;AA6QA,SAAgB,oBAOd,WAiB2C;CAC3C,IAAI,OAAO,cAAc,eAAe,WACtC,yBAAyB,mBAAmB;CAE9C,MAAM,cAAc,gBAAgB;CACpC,MAAM,CAAC,cAAc,eAAe,gBAClC,WACA,qBACF;CACA,MAAM,4BACJ,YAAY;CAOd,OAAO,OAAO,OACZ,YACE,cACA,yBACF,GACA;EACE,UAAU,YACR,kBACE,gBAAgB,oBAAoB,CAAC,CAAC,QAAQ,OAAO,CAAC,CACxD;EACF,gBAAgB,YACd,kBACE,gBAAgB,oBAAoB,CAAC,CAAC,cAAc,OAAO,CAAC,CAC9D;EACF,oBAAoB,YAClB,kBACE,gBAAgB,oBAAoB,CAAC,CAAC,kBAAkB,OAAO,CAAC,CAClE;CACJ,CACF;AAGF"}
|
package/inject-is-fetching.d.ts
CHANGED
|
@@ -1,11 +1,42 @@
|
|
|
1
|
-
import { QueryFilters } from '@tanstack/query-core';
|
|
2
1
|
import { Signal } from '@angular/core';
|
|
2
|
+
import { QueryFilters } from '@tanstack/query-core';
|
|
3
3
|
/**
|
|
4
4
|
* Injects a signal that tracks the number of queries that your application is loading or fetching in the
|
|
5
5
|
* background (useful for app-wide loading indicators).
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* @returns
|
|
6
|
+
* @param filters - A function returning the {@link QueryFilters} to narrow down the matched queries. Similar
|
|
7
|
+
* to `computed` from Angular, this function runs in the reactive context, so signals read inside it
|
|
8
|
+
* re-narrow the matched queries.
|
|
9
|
+
* @returns A `Signal` with the number of queries that your application is currently loading or fetching in
|
|
10
|
+
* the background.
|
|
11
|
+
* @example
|
|
12
|
+
* ```angular-ts
|
|
13
|
+
* @Component({
|
|
14
|
+
* selector: 'posts-fetching-indicator',
|
|
15
|
+
* template: `
|
|
16
|
+
* @if (isFetchingPosts()) {
|
|
17
|
+
* <span>Refreshing posts...</span>
|
|
18
|
+
* }
|
|
19
|
+
* `,
|
|
20
|
+
* })
|
|
21
|
+
* export class PostsFetchingIndicator {
|
|
22
|
+
* // How many queries matching the posts prefix are fetching?
|
|
23
|
+
* readonly isFetchingPosts = injectIsFetching(() => ({ queryKey: ['posts'] }))
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
* @example
|
|
27
|
+
* A global loading indicator for any query fetching in the background, not just the ones on screen:
|
|
28
|
+
* ```angular-ts
|
|
29
|
+
* @Component({
|
|
30
|
+
* selector: 'global-loading-indicator',
|
|
31
|
+
* template: `
|
|
32
|
+
* @if (isFetching()) {
|
|
33
|
+
* <div>Queries are fetching in the background...</div>
|
|
34
|
+
* }
|
|
35
|
+
* `,
|
|
36
|
+
* })
|
|
37
|
+
* export class GlobalLoadingIndicator {
|
|
38
|
+
* readonly isFetching = injectIsFetching()
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
10
41
|
*/
|
|
11
|
-
export declare function injectIsFetching(filters?: () => QueryFilters): Signal<number>;
|
|
42
|
+
export declare function injectIsFetching(filters?: () => Omit<QueryFilters, 'fetchStatus'>): Signal<number>;
|