@tanstack/angular-query-experimental 5.80.7 → 5.80.8
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/dist/create-base-query.mjs +74 -0
- package/dist/create-base-query.mjs.map +1 -0
- package/dist/index.mjs +35 -0
- package/dist/index.mjs.map +1 -0
- package/dist/infinite-query-options.mjs +7 -0
- package/dist/infinite-query-options.mjs.map +1 -0
- package/dist/inject-infinite-query.mjs +18 -0
- package/dist/inject-infinite-query.mjs.map +1 -0
- package/dist/inject-is-fetching.mjs +31 -0
- package/dist/inject-is-fetching.mjs.map +1 -0
- package/dist/inject-is-mutating.mjs +31 -0
- package/dist/inject-is-mutating.mjs.map +1 -0
- package/dist/inject-is-restoring.mjs +24 -0
- package/dist/inject-is-restoring.mjs.map +1 -0
- package/dist/inject-mutation-state.mjs +51 -0
- package/dist/inject-mutation-state.mjs.map +1 -0
- package/dist/inject-mutation.mjs +79 -0
- package/dist/inject-mutation.mjs.map +1 -0
- package/dist/inject-queries.mjs +49 -0
- package/dist/inject-queries.mjs.map +1 -0
- package/dist/inject-query-client.mjs +9 -0
- package/dist/inject-query-client.mjs.map +1 -0
- package/dist/inject-query.mjs +14 -0
- package/dist/inject-query.mjs.map +1 -0
- package/dist/mutation-options.mjs +7 -0
- package/dist/mutation-options.mjs.map +1 -0
- package/dist/providers.mjs +109 -0
- package/dist/providers.mjs.map +1 -0
- package/dist/query-options.mjs +7 -0
- package/dist/query-options.mjs.map +1 -0
- package/dist/signal-proxy.mjs +29 -0
- package/dist/signal-proxy.mjs.map +1 -0
- package/dist-ts/eslint.config.d.ts +3 -0
- package/dist-ts/root.eslint.config.d.ts +3 -0
- package/dist-ts/src/__tests__/inject-infinite-query.test-d.d.ts +2 -0
- package/dist-ts/src/__tests__/inject-infinite-query.test.d.ts +2 -0
- package/dist-ts/src/__tests__/inject-is-fetching.test.d.ts +2 -0
- package/dist-ts/src/__tests__/inject-is-mutating.test.d.ts +2 -0
- package/dist-ts/src/__tests__/inject-mutation-state.test-d.d.ts +2 -0
- package/dist-ts/src/__tests__/inject-mutation-state.test.d.ts +2 -0
- package/dist-ts/src/__tests__/inject-mutation.test-d.d.ts +2 -0
- package/dist-ts/src/__tests__/inject-mutation.test.d.ts +2 -0
- package/dist-ts/src/__tests__/inject-query.test-d.d.ts +2 -0
- package/dist-ts/src/__tests__/inject-query.test.d.ts +2 -0
- package/dist-ts/src/__tests__/mutation-options.test-d.d.ts +2 -0
- package/dist-ts/src/__tests__/provide-query-client.test.d.ts +2 -0
- package/dist-ts/src/__tests__/provide-tanstack-query.test.d.ts +2 -0
- package/dist-ts/src/__tests__/providers.test.d.ts +2 -0
- package/dist-ts/src/__tests__/query-options.test-d.d.ts +2 -0
- package/dist-ts/src/__tests__/signal-proxy.test.d.ts +2 -0
- package/dist-ts/src/__tests__/test-utils.d.ts +16 -0
- package/dist-ts/src/create-base-query.d.ts +9 -0
- package/dist-ts/src/index.d.ts +27 -0
- package/dist-ts/src/infinite-query-options.d.ts +45 -0
- package/dist-ts/src/inject-infinite-query.d.ts +40 -0
- package/dist-ts/src/inject-is-fetching.d.ts +23 -0
- package/dist-ts/src/inject-is-mutating.d.ts +22 -0
- package/{src/inject-is-restoring.ts → dist-ts/src/inject-is-restoring.d.ts} +7 -33
- package/dist-ts/src/inject-mutation-state.d.ts +28 -0
- package/dist-ts/src/inject-mutation.d.ts +23 -0
- package/dist-ts/src/inject-queries.d.ts +78 -0
- package/{src/inject-query-client.ts → dist-ts/src/inject-query-client.d.ts} +7 -9
- package/{src/inject-query.ts → dist-ts/src/inject-query.d.ts} +14 -117
- package/{src/mutation-options.ts → dist-ts/src/mutation-options.d.ts} +5 -26
- package/dist-ts/src/providers.d.ts +217 -0
- package/dist-ts/src/query-options.d.ts +88 -0
- package/dist-ts/src/signal-proxy.d.ts +12 -0
- package/dist-ts/src/test-setup.d.ts +2 -0
- package/dist-ts/src/types.d.ts +90 -0
- package/dist-ts/src/util/is-dev-mode/is-dev-mode.d.ts +2 -0
- package/dist-ts/vite.config.d.ts +5 -0
- package/package.json +13 -11
- package/build/index.d.ts +0 -821
- package/build/index.mjs +0 -622
- package/build/index.mjs.map +0 -1
- package/src/create-base-query.ts +0 -142
- package/src/index.ts +0 -64
- package/src/infinite-query-options.ts +0 -187
- package/src/inject-infinite-query.ts +0 -129
- package/src/inject-is-fetching.ts +0 -66
- package/src/inject-is-mutating.ts +0 -65
- package/src/inject-mutation-state.ts +0 -125
- package/src/inject-mutation.ts +0 -169
- package/src/inject-queries.ts +0 -271
- package/src/providers.ts +0 -368
- package/src/query-options.ts +0 -178
- package/src/signal-proxy.ts +0 -46
- package/src/test-setup.ts +0 -10
- package/src/types.ts +0 -309
- package/src/util/is-dev-mode/is-dev-mode.ts +0 -3
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { inject, NgZone, computed, signal, effect, untracked, VERSION } from "@angular/core";
|
|
2
|
+
import { QueryClient, notifyManager, shouldThrowError } from "@tanstack/query-core";
|
|
3
|
+
import { signalProxy } from "./signal-proxy.mjs";
|
|
4
|
+
import { injectIsRestoring } from "./inject-is-restoring.mjs";
|
|
5
|
+
function createBaseQuery(optionsFn, Observer) {
|
|
6
|
+
const ngZone = inject(NgZone);
|
|
7
|
+
const queryClient = inject(QueryClient);
|
|
8
|
+
const isRestoring = injectIsRestoring();
|
|
9
|
+
const defaultedOptionsSignal = computed(() => {
|
|
10
|
+
const defaultedOptions = queryClient.defaultQueryOptions(optionsFn());
|
|
11
|
+
defaultedOptions._optimisticResults = isRestoring() ? "isRestoring" : "optimistic";
|
|
12
|
+
return defaultedOptions;
|
|
13
|
+
});
|
|
14
|
+
const observerSignal = (() => {
|
|
15
|
+
let instance = null;
|
|
16
|
+
return computed(() => {
|
|
17
|
+
return instance || (instance = new Observer(queryClient, defaultedOptionsSignal()));
|
|
18
|
+
});
|
|
19
|
+
})();
|
|
20
|
+
const optimisticResultSignal = computed(
|
|
21
|
+
() => observerSignal().getOptimisticResult(defaultedOptionsSignal())
|
|
22
|
+
);
|
|
23
|
+
const resultFromSubscriberSignal = signal(null);
|
|
24
|
+
effect(
|
|
25
|
+
(onCleanup) => {
|
|
26
|
+
const observer = observerSignal();
|
|
27
|
+
const defaultedOptions = defaultedOptionsSignal();
|
|
28
|
+
untracked(() => {
|
|
29
|
+
observer.setOptions(defaultedOptions);
|
|
30
|
+
});
|
|
31
|
+
onCleanup(() => {
|
|
32
|
+
ngZone.run(() => resultFromSubscriberSignal.set(null));
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
// Set allowSignalWrites to support Angular < v19
|
|
37
|
+
// Set to undefined to avoid warning on newer versions
|
|
38
|
+
allowSignalWrites: VERSION.major < "19" || void 0
|
|
39
|
+
}
|
|
40
|
+
);
|
|
41
|
+
effect((onCleanup) => {
|
|
42
|
+
const observer = observerSignal();
|
|
43
|
+
const unsubscribe = isRestoring() ? () => void 0 : untracked(
|
|
44
|
+
() => ngZone.runOutsideAngular(
|
|
45
|
+
() => observer.subscribe(
|
|
46
|
+
notifyManager.batchCalls((state) => {
|
|
47
|
+
ngZone.run(() => {
|
|
48
|
+
if (state.isError && !state.isFetching && shouldThrowError(observer.options.throwOnError, [
|
|
49
|
+
state.error,
|
|
50
|
+
observer.getCurrentQuery()
|
|
51
|
+
])) {
|
|
52
|
+
ngZone.onError.emit(state.error);
|
|
53
|
+
throw state.error;
|
|
54
|
+
}
|
|
55
|
+
resultFromSubscriberSignal.set(state);
|
|
56
|
+
});
|
|
57
|
+
})
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
);
|
|
61
|
+
onCleanup(unsubscribe);
|
|
62
|
+
});
|
|
63
|
+
return signalProxy(
|
|
64
|
+
computed(() => {
|
|
65
|
+
const subscriberResult = resultFromSubscriberSignal();
|
|
66
|
+
const optimisticResult = optimisticResultSignal();
|
|
67
|
+
return subscriberResult ?? optimisticResult;
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
export {
|
|
72
|
+
createBaseQuery
|
|
73
|
+
};
|
|
74
|
+
//# sourceMappingURL=create-base-query.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-base-query.mjs","sources":["../src/create-base-query.ts"],"sourcesContent":["import {\n NgZone,\n VERSION,\n computed,\n effect,\n inject,\n signal,\n untracked,\n} from '@angular/core'\nimport {\n QueryClient,\n notifyManager,\n shouldThrowError,\n} from '@tanstack/query-core'\nimport { signalProxy } from './signal-proxy'\nimport { injectIsRestoring } from './inject-is-restoring'\nimport type {\n QueryKey,\n QueryObserver,\n QueryObserverResult,\n} from '@tanstack/query-core'\nimport type { CreateBaseQueryOptions } from './types'\n\n/**\n * Base implementation for `injectQuery` and `injectInfiniteQuery`.\n * @param optionsFn\n * @param Observer\n */\nexport function createBaseQuery<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey extends QueryKey,\n>(\n optionsFn: () => CreateBaseQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n >,\n Observer: typeof QueryObserver,\n) {\n const ngZone = inject(NgZone)\n const queryClient = inject(QueryClient)\n const isRestoring = injectIsRestoring()\n\n /**\n * Signal that has the default options from query client applied\n * computed() is used so signals can be inserted into the options\n * making it reactive. Wrapping options in a function ensures embedded expressions\n * are preserved and can keep being applied after signal changes\n */\n const defaultedOptionsSignal = computed(() => {\n const defaultedOptions = queryClient.defaultQueryOptions(optionsFn())\n defaultedOptions._optimisticResults = isRestoring()\n ? 'isRestoring'\n : 'optimistic'\n return defaultedOptions\n })\n\n const observerSignal = (() => {\n let instance: QueryObserver<\n TQueryFnData,\n TError,\n TData,\n TQueryData,\n TQueryKey\n > | null = null\n\n return computed(() => {\n return (instance ||= new Observer(queryClient, defaultedOptionsSignal()))\n })\n })()\n\n const optimisticResultSignal = computed(() =>\n observerSignal().getOptimisticResult(defaultedOptionsSignal()),\n )\n\n const resultFromSubscriberSignal = signal<QueryObserverResult<\n TData,\n TError\n > | null>(null)\n\n effect(\n (onCleanup) => {\n const observer = observerSignal()\n const defaultedOptions = defaultedOptionsSignal()\n\n untracked(() => {\n observer.setOptions(defaultedOptions)\n })\n onCleanup(() => {\n ngZone.run(() => resultFromSubscriberSignal.set(null))\n })\n },\n {\n // Set allowSignalWrites to support Angular < v19\n // Set to undefined to avoid warning on newer versions\n allowSignalWrites: VERSION.major < '19' || undefined,\n },\n )\n\n effect((onCleanup) => {\n // observer.trackResult is not used as this optimization is not needed for Angular\n const observer = observerSignal()\n const unsubscribe = isRestoring()\n ? () => undefined\n : untracked(() =>\n ngZone.runOutsideAngular(() =>\n observer.subscribe(\n notifyManager.batchCalls((state) => {\n ngZone.run(() => {\n if (\n state.isError &&\n !state.isFetching &&\n shouldThrowError(observer.options.throwOnError, [\n state.error,\n observer.getCurrentQuery(),\n ])\n ) {\n ngZone.onError.emit(state.error)\n throw state.error\n }\n resultFromSubscriberSignal.set(state)\n })\n }),\n ),\n ),\n )\n onCleanup(unsubscribe)\n })\n\n return signalProxy(\n computed(() => {\n const subscriberResult = resultFromSubscriberSignal()\n const optimisticResult = optimisticResultSignal()\n return subscriberResult ?? optimisticResult\n }),\n )\n}\n"],"names":[],"mappings":";;;;AA4BgB,SAAA,gBAOd,WAOA,UACA;AACM,QAAA,SAAS,OAAO,MAAM;AACtB,QAAA,cAAc,OAAO,WAAW;AACtC,QAAM,cAAc,kBAAkB;AAQhC,QAAA,yBAAyB,SAAS,MAAM;AAC5C,UAAM,mBAAmB,YAAY,oBAAoB,UAAA,CAAW;AACnD,qBAAA,qBAAqB,YAAY,IAC9C,gBACA;AACG,WAAA;AAAA,EAAA,CACR;AAED,QAAM,kBAAkB,MAAM;AAC5B,QAAI,WAMO;AAEX,WAAO,SAAS,MAAM;AACpB,aAAQ,wBAAa,IAAI,SAAS,aAAa,wBAAwB;AAAA,IAAA,CACxE;AAAA,EAAA,GACA;AAEH,QAAM,yBAAyB;AAAA,IAAS,MACtC,eAAA,EAAiB,oBAAoB,uBAAwB,CAAA;AAAA,EAC/D;AAEM,QAAA,6BAA6B,OAGzB,IAAI;AAEd;AAAA,IACE,CAAC,cAAc;AACb,YAAM,WAAW,eAAe;AAChC,YAAM,mBAAmB,uBAAuB;AAEhD,gBAAU,MAAM;AACd,iBAAS,WAAW,gBAAgB;AAAA,MAAA,CACrC;AACD,gBAAU,MAAM;AACd,eAAO,IAAI,MAAM,2BAA2B,IAAI,IAAI,CAAC;AAAA,MAAA,CACtD;AAAA,IACH;AAAA,IACA;AAAA;AAAA;AAAA,MAGE,mBAAmB,QAAQ,QAAQ,QAAQ;AAAA,IAAA;AAAA,EAE/C;AAEA,SAAO,CAAC,cAAc;AAEpB,UAAM,WAAW,eAAe;AAChC,UAAM,cAAc,gBAChB,MAAM,SACN;AAAA,MAAU,MACR,OAAO;AAAA,QAAkB,MACvB,SAAS;AAAA,UACP,cAAc,WAAW,CAAC,UAAU;AAClC,mBAAO,IAAI,MAAM;AAEb,kBAAA,MAAM,WACN,CAAC,MAAM,cACP,iBAAiB,SAAS,QAAQ,cAAc;AAAA,gBAC9C,MAAM;AAAA,gBACN,SAAS,gBAAgB;AAAA,cAAA,CAC1B,GACD;AACO,uBAAA,QAAQ,KAAK,MAAM,KAAK;AAC/B,sBAAM,MAAM;AAAA,cAAA;AAEd,yCAA2B,IAAI,KAAK;AAAA,YAAA,CACrC;AAAA,UACF,CAAA;AAAA,QAAA;AAAA,MACH;AAAA,IAEJ;AACJ,cAAU,WAAW;AAAA,EAAA,CACtB;AAEM,SAAA;AAAA,IACL,SAAS,MAAM;AACb,YAAM,mBAAmB,2BAA2B;AACpD,YAAM,mBAAmB,uBAAuB;AAChD,aAAO,oBAAoB;AAAA,IAC5B,CAAA;AAAA,EACH;AACF;"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export * from "@tanstack/query-core";
|
|
2
|
+
import { queryOptions } from "./query-options.mjs";
|
|
3
|
+
import { mutationOptions } from "./mutation-options.mjs";
|
|
4
|
+
import { infiniteQueryOptions } from "./infinite-query-options.mjs";
|
|
5
|
+
import { injectInfiniteQuery } from "./inject-infinite-query.mjs";
|
|
6
|
+
import { injectIsFetching } from "./inject-is-fetching.mjs";
|
|
7
|
+
import { injectIsMutating } from "./inject-is-mutating.mjs";
|
|
8
|
+
import { injectIsRestoring, provideIsRestoring } from "./inject-is-restoring.mjs";
|
|
9
|
+
import { injectMutation } from "./inject-mutation.mjs";
|
|
10
|
+
import { injectMutationState } from "./inject-mutation-state.mjs";
|
|
11
|
+
import { injectQueries } from "./inject-queries.mjs";
|
|
12
|
+
import { injectQuery } from "./inject-query.mjs";
|
|
13
|
+
import { injectQueryClient } from "./inject-query-client.mjs";
|
|
14
|
+
import { provideQueryClient, provideTanStackQuery, queryFeature, queryFeatures, withDevtools } from "./providers.mjs";
|
|
15
|
+
export {
|
|
16
|
+
infiniteQueryOptions,
|
|
17
|
+
injectInfiniteQuery,
|
|
18
|
+
injectIsFetching,
|
|
19
|
+
injectIsMutating,
|
|
20
|
+
injectIsRestoring,
|
|
21
|
+
injectMutation,
|
|
22
|
+
injectMutationState,
|
|
23
|
+
injectQueries,
|
|
24
|
+
injectQuery,
|
|
25
|
+
injectQueryClient,
|
|
26
|
+
mutationOptions,
|
|
27
|
+
provideIsRestoring,
|
|
28
|
+
provideQueryClient,
|
|
29
|
+
provideTanStackQuery,
|
|
30
|
+
queryFeature,
|
|
31
|
+
queryFeatures,
|
|
32
|
+
queryOptions,
|
|
33
|
+
withDevtools
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"infinite-query-options.mjs","sources":["../src/infinite-query-options.ts"],"sourcesContent":["import type {\n DataTag,\n DefaultError,\n InfiniteData,\n InitialDataFunction,\n NonUndefinedGuard,\n OmitKeyof,\n QueryKey,\n SkipToken,\n} from '@tanstack/query-core'\nimport type { CreateInfiniteQueryOptions } from './types'\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 initialData?:\n | undefined\n | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>\n | InitialDataFunction<\n NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>\n >\n}\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 queryFn?: Exclude<\n CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >['queryFn'],\n SkipToken | undefined\n >\n}\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 initialData:\n | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>\n | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)\n | undefined\n}\n\n/**\n * Allows to share and re-use infinite query options in a type-safe way.\n *\n * The `queryKey` will be tagged with the type from `queryFn`.\n * @param options - The infinite query options to tag with the type from `queryFn`.\n * @returns The tagged infinite query options.\n * @public\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 queryKey: DataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>\n}\n\n/**\n * Allows to share and re-use infinite query options in a type-safe way.\n *\n * The `queryKey` will be tagged with the type from `queryFn`.\n * @param options - The infinite query options to tag with the type from `queryFn`.\n * @returns The tagged infinite query options.\n * @public\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 queryKey: DataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>\n}\n\n/**\n * Allows to share and re-use infinite query options in a type-safe way.\n *\n * The `queryKey` will be tagged with the type from `queryFn`.\n * @param options - The infinite query options to tag with the type from `queryFn`.\n * @returns The tagged infinite query options.\n * @public\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 queryKey: DataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>\n}\n\n/**\n * Allows to share and re-use infinite query options in a type-safe way.\n *\n * The `queryKey` will be tagged with the type from `queryFn`.\n * @param options - The infinite query options to tag with the type from `queryFn`.\n * @returns The tagged infinite query options.\n * @public\n */\nexport function infiniteQueryOptions(options: unknown) {\n return options\n}\n"],"names":[],"mappings":"AAwLO,SAAS,qBAAqB,SAAkB;AAC9C,SAAA;AACT;"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { InfiniteQueryObserver } from "@tanstack/query-core";
|
|
2
|
+
import { assertInInjectionContext, inject, Injector, runInInjectionContext } from "@angular/core";
|
|
3
|
+
import { createBaseQuery } from "./create-base-query.mjs";
|
|
4
|
+
function injectInfiniteQuery(injectInfiniteQueryFn, options) {
|
|
5
|
+
!(options == null ? void 0 : options.injector) && assertInInjectionContext(injectInfiniteQuery);
|
|
6
|
+
const injector = (options == null ? void 0 : options.injector) ?? inject(Injector);
|
|
7
|
+
return runInInjectionContext(
|
|
8
|
+
injector,
|
|
9
|
+
() => createBaseQuery(
|
|
10
|
+
injectInfiniteQueryFn,
|
|
11
|
+
InfiniteQueryObserver
|
|
12
|
+
)
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
injectInfiniteQuery
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=inject-infinite-query.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-infinite-query.mjs","sources":["../src/inject-infinite-query.ts"],"sourcesContent":["import { InfiniteQueryObserver } from '@tanstack/query-core'\nimport {\n Injector,\n assertInInjectionContext,\n inject,\n runInInjectionContext,\n} from '@angular/core'\nimport { createBaseQuery } from './create-base-query'\nimport type {\n DefaultError,\n InfiniteData,\n QueryKey,\n QueryObserver,\n} from '@tanstack/query-core'\nimport type {\n CreateInfiniteQueryOptions,\n CreateInfiniteQueryResult,\n DefinedCreateInfiniteQueryResult,\n} from './types'\nimport type {\n DefinedInitialDataInfiniteOptions,\n UndefinedInitialDataInfiniteOptions,\n} from './infinite-query-options'\n\nexport interface InjectInfiniteQueryOptions {\n /**\n * The `Injector` in which to create the infinite query.\n *\n * If this is not provided, the current injection context will be used instead (via `inject`).\n */\n injector?: Injector\n}\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 \"infinite scroll\"\n * @param injectInfiniteQueryFn - A function that returns infinite query options.\n * @param options - Additional configuration.\n * @returns The infinite query result.\n * @public\n */\nexport function injectInfiniteQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n injectInfiniteQueryFn: () => DefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n options?: InjectInfiniteQueryOptions,\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 \"infinite scroll\"\n * @param injectInfiniteQueryFn - A function that returns infinite query options.\n * @param options - Additional configuration.\n * @returns The infinite query result.\n * @public\n */\nexport function injectInfiniteQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n injectInfiniteQueryFn: () => UndefinedInitialDataInfiniteOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n options?: InjectInfiniteQueryOptions,\n): CreateInfiniteQueryResult<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 \"infinite scroll\"\n * @param injectInfiniteQueryFn - A function that returns infinite query options.\n * @param options - Additional configuration.\n * @returns The infinite query result.\n * @public\n */\nexport function injectInfiniteQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n injectInfiniteQueryFn: () => CreateInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n options?: InjectInfiniteQueryOptions,\n): CreateInfiniteQueryResult<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 \"infinite scroll\"\n * @param injectInfiniteQueryFn - A function that returns infinite query options.\n * @param options - Additional configuration.\n * @returns The infinite query result.\n * @public\n */\nexport function injectInfiniteQuery(\n injectInfiniteQueryFn: () => CreateInfiniteQueryOptions,\n options?: InjectInfiniteQueryOptions,\n) {\n !options?.injector && assertInInjectionContext(injectInfiniteQuery)\n const injector = options?.injector ?? inject(Injector)\n return runInInjectionContext(injector, () =>\n createBaseQuery(\n injectInfiniteQueryFn,\n InfiniteQueryObserver as typeof QueryObserver,\n ),\n )\n}\n"],"names":[],"mappings":";;;AAoHgB,SAAA,oBACd,uBACA,SACA;AACC,IAAA,mCAAS,aAAY,yBAAyB,mBAAmB;AAClE,QAAM,YAAW,mCAAS,aAAY,OAAO,QAAQ;AAC9C,SAAA;AAAA,IAAsB;AAAA,IAAU,MACrC;AAAA,MACE;AAAA,MACA;AAAA,IAAA;AAAA,EAEJ;AACF;"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { assertInInjectionContext, inject, Injector, DestroyRef, NgZone, signal } from "@angular/core";
|
|
2
|
+
import { QueryClient, notifyManager } from "@tanstack/query-core";
|
|
3
|
+
function injectIsFetching(filters, options) {
|
|
4
|
+
!(options == null ? void 0 : options.injector) && assertInInjectionContext(injectIsFetching);
|
|
5
|
+
const injector = (options == null ? void 0 : options.injector) ?? inject(Injector);
|
|
6
|
+
const destroyRef = injector.get(DestroyRef);
|
|
7
|
+
const ngZone = injector.get(NgZone);
|
|
8
|
+
const queryClient = injector.get(QueryClient);
|
|
9
|
+
const cache = queryClient.getQueryCache();
|
|
10
|
+
let isFetching = queryClient.isFetching(filters);
|
|
11
|
+
const result = signal(isFetching);
|
|
12
|
+
const unsubscribe = ngZone.runOutsideAngular(
|
|
13
|
+
() => cache.subscribe(
|
|
14
|
+
notifyManager.batchCalls(() => {
|
|
15
|
+
const newIsFetching = queryClient.isFetching(filters);
|
|
16
|
+
if (isFetching !== newIsFetching) {
|
|
17
|
+
isFetching = newIsFetching;
|
|
18
|
+
ngZone.run(() => {
|
|
19
|
+
result.set(isFetching);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
)
|
|
24
|
+
);
|
|
25
|
+
destroyRef.onDestroy(unsubscribe);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
export {
|
|
29
|
+
injectIsFetching
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=inject-is-fetching.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-is-fetching.mjs","sources":["../src/inject-is-fetching.ts"],"sourcesContent":["import {\n DestroyRef,\n Injector,\n NgZone,\n assertInInjectionContext,\n inject,\n signal,\n} from '@angular/core'\nimport { QueryClient, notifyManager } from '@tanstack/query-core'\nimport type { QueryFilters } from '@tanstack/query-core'\nimport type { Signal } from '@angular/core'\n\nexport interface InjectIsFetchingOptions {\n /**\n * The `Injector` in which to create the isFetching signal.\n *\n * If this is not provided, the current injection context will be used instead (via `inject`).\n */\n injector?: Injector\n}\n\n/**\n * Injects a signal that tracks the number of queries that your application is loading or\n * fetching in the background.\n *\n * Can be used for app-wide loading indicators\n * @param filters - The filters to apply to the query.\n * @param options - Additional configuration\n * @returns signal with number of loading or fetching queries.\n * @public\n */\nexport function injectIsFetching(\n filters?: QueryFilters,\n options?: InjectIsFetchingOptions,\n): Signal<number> {\n !options?.injector && assertInInjectionContext(injectIsFetching)\n const injector = options?.injector ?? inject(Injector)\n const destroyRef = injector.get(DestroyRef)\n const ngZone = injector.get(NgZone)\n const queryClient = injector.get(QueryClient)\n\n const cache = queryClient.getQueryCache()\n // isFetching is the prev value initialized on mount *\n let isFetching = queryClient.isFetching(filters)\n\n const result = signal(isFetching)\n\n const unsubscribe = ngZone.runOutsideAngular(() =>\n cache.subscribe(\n notifyManager.batchCalls(() => {\n const newIsFetching = queryClient.isFetching(filters)\n if (isFetching !== newIsFetching) {\n // * and update with each change\n isFetching = newIsFetching\n ngZone.run(() => {\n result.set(isFetching)\n })\n }\n }),\n ),\n )\n\n destroyRef.onDestroy(unsubscribe)\n\n return result\n}\n"],"names":[],"mappings":";;AA+BgB,SAAA,iBACd,SACA,SACgB;AACf,IAAA,mCAAS,aAAY,yBAAyB,gBAAgB;AAC/D,QAAM,YAAW,mCAAS,aAAY,OAAO,QAAQ;AAC/C,QAAA,aAAa,SAAS,IAAI,UAAU;AACpC,QAAA,SAAS,SAAS,IAAI,MAAM;AAC5B,QAAA,cAAc,SAAS,IAAI,WAAW;AAEtC,QAAA,QAAQ,YAAY,cAAc;AAEpC,MAAA,aAAa,YAAY,WAAW,OAAO;AAEzC,QAAA,SAAS,OAAO,UAAU;AAEhC,QAAM,cAAc,OAAO;AAAA,IAAkB,MAC3C,MAAM;AAAA,MACJ,cAAc,WAAW,MAAM;AACvB,cAAA,gBAAgB,YAAY,WAAW,OAAO;AACpD,YAAI,eAAe,eAAe;AAEnB,uBAAA;AACb,iBAAO,IAAI,MAAM;AACf,mBAAO,IAAI,UAAU;AAAA,UAAA,CACtB;AAAA,QAAA;AAAA,MAEJ,CAAA;AAAA,IAAA;AAAA,EAEL;AAEA,aAAW,UAAU,WAAW;AAEzB,SAAA;AACT;"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { assertInInjectionContext, inject, Injector, DestroyRef, NgZone, signal } from "@angular/core";
|
|
2
|
+
import { QueryClient, notifyManager } from "@tanstack/query-core";
|
|
3
|
+
function injectIsMutating(filters, options) {
|
|
4
|
+
!(options == null ? void 0 : options.injector) && assertInInjectionContext(injectIsMutating);
|
|
5
|
+
const injector = (options == null ? void 0 : options.injector) ?? inject(Injector);
|
|
6
|
+
const destroyRef = injector.get(DestroyRef);
|
|
7
|
+
const ngZone = injector.get(NgZone);
|
|
8
|
+
const queryClient = injector.get(QueryClient);
|
|
9
|
+
const cache = queryClient.getMutationCache();
|
|
10
|
+
let isMutating = queryClient.isMutating(filters);
|
|
11
|
+
const result = signal(isMutating);
|
|
12
|
+
const unsubscribe = ngZone.runOutsideAngular(
|
|
13
|
+
() => cache.subscribe(
|
|
14
|
+
notifyManager.batchCalls(() => {
|
|
15
|
+
const newIsMutating = queryClient.isMutating(filters);
|
|
16
|
+
if (isMutating !== newIsMutating) {
|
|
17
|
+
isMutating = newIsMutating;
|
|
18
|
+
ngZone.run(() => {
|
|
19
|
+
result.set(isMutating);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
)
|
|
24
|
+
);
|
|
25
|
+
destroyRef.onDestroy(unsubscribe);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
export {
|
|
29
|
+
injectIsMutating
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=inject-is-mutating.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-is-mutating.mjs","sources":["../src/inject-is-mutating.ts"],"sourcesContent":["import {\n DestroyRef,\n Injector,\n NgZone,\n assertInInjectionContext,\n inject,\n signal,\n} from '@angular/core'\nimport { QueryClient, notifyManager } from '@tanstack/query-core'\nimport type { MutationFilters } from '@tanstack/query-core'\nimport type { Signal } from '@angular/core'\n\nexport interface InjectIsMutatingOptions {\n /**\n * The `Injector` in which to create the isMutating signal.\n *\n * If this is not provided, the current injection context will be used instead (via `inject`).\n */\n injector?: Injector\n}\n\n/**\n * Injects a signal that tracks the number of mutations that your application is fetching.\n *\n * Can be used for app-wide loading indicators\n * @param filters - The filters to apply to the query.\n * @param options - Additional configuration\n * @returns signal with number of fetching mutations.\n * @public\n */\nexport function injectIsMutating(\n filters?: MutationFilters,\n options?: InjectIsMutatingOptions,\n): Signal<number> {\n !options?.injector && assertInInjectionContext(injectIsMutating)\n const injector = options?.injector ?? inject(Injector)\n const destroyRef = injector.get(DestroyRef)\n const ngZone = injector.get(NgZone)\n const queryClient = injector.get(QueryClient)\n\n const cache = queryClient.getMutationCache()\n // isMutating is the prev value initialized on mount *\n let isMutating = queryClient.isMutating(filters)\n\n const result = signal(isMutating)\n\n const unsubscribe = ngZone.runOutsideAngular(() =>\n cache.subscribe(\n notifyManager.batchCalls(() => {\n const newIsMutating = queryClient.isMutating(filters)\n if (isMutating !== newIsMutating) {\n // * and update with each change\n isMutating = newIsMutating\n ngZone.run(() => {\n result.set(isMutating)\n })\n }\n }),\n ),\n )\n\n destroyRef.onDestroy(unsubscribe)\n\n return result\n}\n"],"names":[],"mappings":";;AA8BgB,SAAA,iBACd,SACA,SACgB;AACf,IAAA,mCAAS,aAAY,yBAAyB,gBAAgB;AAC/D,QAAM,YAAW,mCAAS,aAAY,OAAO,QAAQ;AAC/C,QAAA,aAAa,SAAS,IAAI,UAAU;AACpC,QAAA,SAAS,SAAS,IAAI,MAAM;AAC5B,QAAA,cAAc,SAAS,IAAI,WAAW;AAEtC,QAAA,QAAQ,YAAY,iBAAiB;AAEvC,MAAA,aAAa,YAAY,WAAW,OAAO;AAEzC,QAAA,SAAS,OAAO,UAAU;AAEhC,QAAM,cAAc,OAAO;AAAA,IAAkB,MAC3C,MAAM;AAAA,MACJ,cAAc,WAAW,MAAM;AACvB,cAAA,gBAAgB,YAAY,WAAW,OAAO;AACpD,YAAI,eAAe,eAAe;AAEnB,uBAAA;AACb,iBAAO,IAAI,MAAM;AACf,mBAAO,IAAI,UAAU;AAAA,UAAA,CACtB;AAAA,QAAA;AAAA,MAEJ,CAAA;AAAA,IAAA;AAAA,EAEL;AAEA,aAAW,UAAU,WAAW;AAEzB,SAAA;AACT;"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { assertInInjectionContext, inject, Injector, InjectionToken, signal } from "@angular/core";
|
|
2
|
+
const IS_RESTORING = new InjectionToken(
|
|
3
|
+
typeof ngDevMode === "undefined" || ngDevMode ? "TANSTACK_QUERY_IS_RESTORING" : "",
|
|
4
|
+
{
|
|
5
|
+
// Default value when not provided
|
|
6
|
+
factory: () => signal(false).asReadonly()
|
|
7
|
+
}
|
|
8
|
+
);
|
|
9
|
+
function injectIsRestoring(options) {
|
|
10
|
+
!(options == null ? void 0 : options.injector) && assertInInjectionContext(injectIsRestoring);
|
|
11
|
+
const injector = (options == null ? void 0 : options.injector) ?? inject(Injector);
|
|
12
|
+
return injector.get(IS_RESTORING);
|
|
13
|
+
}
|
|
14
|
+
function provideIsRestoring(isRestoring) {
|
|
15
|
+
return {
|
|
16
|
+
provide: IS_RESTORING,
|
|
17
|
+
useValue: isRestoring
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
injectIsRestoring,
|
|
22
|
+
provideIsRestoring
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=inject-is-restoring.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-is-restoring.mjs","sources":["../src/inject-is-restoring.ts"],"sourcesContent":["import {\n InjectionToken,\n Injector,\n assertInInjectionContext,\n inject,\n signal,\n} from '@angular/core'\nimport type { Provider, Signal } from '@angular/core'\n\nconst IS_RESTORING = new InjectionToken(\n typeof ngDevMode === 'undefined' || ngDevMode\n ? 'TANSTACK_QUERY_IS_RESTORING'\n : '',\n {\n // Default value when not provided\n factory: () => signal(false).asReadonly(),\n },\n)\n\n/**\n * The `Injector` in which to create the isRestoring signal.\n *\n * If this is not provided, the current injection context will be used instead (via `inject`).\n */\ninterface InjectIsRestoringOptions {\n injector?: Injector\n}\n\n/**\n * Injects a signal that tracks whether a restore is currently in progress. {@link injectQuery} and friends also check this internally to avoid race conditions between the restore and initializing queries.\n * @param options - Options for injectIsRestoring.\n * @returns signal with boolean that indicates whether a restore is in progress.\n * @public\n */\nexport function injectIsRestoring(options?: InjectIsRestoringOptions) {\n !options?.injector && assertInInjectionContext(injectIsRestoring)\n const injector = options?.injector ?? inject(Injector)\n return injector.get(IS_RESTORING)\n}\n\n/**\n * Used by TanStack Query Angular persist client plugin to provide the signal that tracks the restore state\n * @param isRestoring - a readonly signal that returns a boolean\n * @returns Provider for the `isRestoring` signal\n * @public\n */\nexport function provideIsRestoring(isRestoring: Signal<boolean>): Provider {\n return {\n provide: IS_RESTORING,\n useValue: isRestoring,\n }\n}\n"],"names":[],"mappings":";AASA,MAAM,eAAe,IAAI;AAAA,EACvB,OAAO,cAAc,eAAe,YAChC,gCACA;AAAA,EACJ;AAAA;AAAA,IAEE,SAAS,MAAM,OAAO,KAAK,EAAE,WAAW;AAAA,EAAA;AAE5C;AAiBO,SAAS,kBAAkB,SAAoC;AACnE,IAAA,mCAAS,aAAY,yBAAyB,iBAAiB;AAChE,QAAM,YAAW,mCAAS,aAAY,OAAO,QAAQ;AAC9C,SAAA,SAAS,IAAI,YAAY;AAClC;AAQO,SAAS,mBAAmB,aAAwC;AAClE,SAAA;AAAA,IACL,SAAS;AAAA,IACT,UAAU;AAAA,EACZ;AACF;"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { assertInInjectionContext, inject, Injector, DestroyRef, NgZone, computed, signal } from "@angular/core";
|
|
2
|
+
import { QueryClient, notifyManager, replaceEqualDeep } from "@tanstack/query-core";
|
|
3
|
+
function getResult(mutationCache, options) {
|
|
4
|
+
return mutationCache.findAll(options.filters).map(
|
|
5
|
+
(mutation) => options.select ? options.select(mutation) : mutation.state
|
|
6
|
+
);
|
|
7
|
+
}
|
|
8
|
+
function injectMutationState(injectMutationStateFn = () => ({}), options) {
|
|
9
|
+
!(options == null ? void 0 : options.injector) && assertInInjectionContext(injectMutationState);
|
|
10
|
+
const injector = (options == null ? void 0 : options.injector) ?? inject(Injector);
|
|
11
|
+
const destroyRef = injector.get(DestroyRef);
|
|
12
|
+
const ngZone = injector.get(NgZone);
|
|
13
|
+
const queryClient = injector.get(QueryClient);
|
|
14
|
+
const mutationCache = queryClient.getMutationCache();
|
|
15
|
+
const resultFromOptionsSignal = computed(() => {
|
|
16
|
+
return [
|
|
17
|
+
getResult(mutationCache, injectMutationStateFn()),
|
|
18
|
+
performance.now()
|
|
19
|
+
];
|
|
20
|
+
});
|
|
21
|
+
const resultFromSubscriberSignal = signal(
|
|
22
|
+
null
|
|
23
|
+
);
|
|
24
|
+
const effectiveResultSignal = computed(() => {
|
|
25
|
+
const optionsResult = resultFromOptionsSignal();
|
|
26
|
+
const subscriberResult = resultFromSubscriberSignal();
|
|
27
|
+
return subscriberResult && subscriberResult[1] > optionsResult[1] ? subscriberResult[0] : optionsResult[0];
|
|
28
|
+
});
|
|
29
|
+
const unsubscribe = ngZone.runOutsideAngular(
|
|
30
|
+
() => mutationCache.subscribe(
|
|
31
|
+
notifyManager.batchCalls(() => {
|
|
32
|
+
const [lastResult] = effectiveResultSignal();
|
|
33
|
+
const nextResult = replaceEqualDeep(
|
|
34
|
+
lastResult,
|
|
35
|
+
getResult(mutationCache, injectMutationStateFn())
|
|
36
|
+
);
|
|
37
|
+
if (lastResult !== nextResult) {
|
|
38
|
+
ngZone.run(() => {
|
|
39
|
+
resultFromSubscriberSignal.set([nextResult, performance.now()]);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
)
|
|
44
|
+
);
|
|
45
|
+
destroyRef.onDestroy(unsubscribe);
|
|
46
|
+
return effectiveResultSignal;
|
|
47
|
+
}
|
|
48
|
+
export {
|
|
49
|
+
injectMutationState
|
|
50
|
+
};
|
|
51
|
+
//# sourceMappingURL=inject-mutation-state.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-mutation-state.mjs","sources":["../src/inject-mutation-state.ts"],"sourcesContent":["import {\n DestroyRef,\n Injector,\n NgZone,\n assertInInjectionContext,\n computed,\n inject,\n signal,\n} from '@angular/core'\nimport {\n QueryClient,\n notifyManager,\n replaceEqualDeep,\n} from '@tanstack/query-core'\nimport type { Signal } from '@angular/core'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n} from '@tanstack/query-core'\n\ntype MutationStateOptions<TResult = MutationState> = {\n filters?: MutationFilters\n select?: (mutation: Mutation) => TResult\n}\n\n/**\n *\n * @param mutationCache\n * @param options\n */\nfunction getResult<TResult = MutationState>(\n mutationCache: MutationCache,\n options: MutationStateOptions<TResult>,\n): Array<TResult> {\n return mutationCache\n .findAll(options.filters)\n .map(\n (mutation): TResult =>\n (options.select ? options.select(mutation) : mutation.state) as TResult,\n )\n}\n\n/**\n * @public\n */\nexport interface InjectMutationStateOptions {\n /**\n * The `Injector` in which to create the mutation state signal.\n *\n * If this is not provided, the current injection context will be used instead (via `inject`).\n */\n injector?: Injector\n}\n\n/**\n * Injects a signal that tracks the state of all mutations.\n * @param injectMutationStateFn - A function that returns mutation state options.\n * @param options - The Angular injector to use.\n * @returns The signal that tracks the state of all mutations.\n * @public\n */\nexport function injectMutationState<TResult = MutationState>(\n injectMutationStateFn: () => MutationStateOptions<TResult> = () => ({}),\n options?: InjectMutationStateOptions,\n): Signal<Array<TResult>> {\n !options?.injector && assertInInjectionContext(injectMutationState)\n const injector = options?.injector ?? inject(Injector)\n const destroyRef = injector.get(DestroyRef)\n const ngZone = injector.get(NgZone)\n const queryClient = injector.get(QueryClient)\n const mutationCache = queryClient.getMutationCache()\n\n /**\n * Computed signal that gets result from mutation cache based on passed options\n * First element is the result, second element is the time when the result was set\n */\n const resultFromOptionsSignal = computed(() => {\n return [\n getResult(mutationCache, injectMutationStateFn()),\n performance.now(),\n ] as const\n })\n\n /**\n * Signal that contains result set by subscriber\n * First element is the result, second element is the time when the result was set\n */\n const resultFromSubscriberSignal = signal<[Array<TResult>, number] | null>(\n null,\n )\n\n /**\n * Returns the last result by either subscriber or options\n */\n const effectiveResultSignal = computed(() => {\n const optionsResult = resultFromOptionsSignal()\n const subscriberResult = resultFromSubscriberSignal()\n return subscriberResult && subscriberResult[1] > optionsResult[1]\n ? subscriberResult[0]\n : optionsResult[0]\n })\n\n const unsubscribe = ngZone.runOutsideAngular(() =>\n mutationCache.subscribe(\n notifyManager.batchCalls(() => {\n const [lastResult] = effectiveResultSignal()\n const nextResult = replaceEqualDeep(\n lastResult,\n getResult(mutationCache, injectMutationStateFn()),\n )\n if (lastResult !== nextResult) {\n ngZone.run(() => {\n resultFromSubscriberSignal.set([nextResult, performance.now()])\n })\n }\n }),\n ),\n )\n\n destroyRef.onDestroy(unsubscribe)\n\n return effectiveResultSignal\n}\n"],"names":[],"mappings":";;AAgCA,SAAS,UACP,eACA,SACgB;AAChB,SAAO,cACJ,QAAQ,QAAQ,OAAO,EACvB;AAAA,IACC,CAAC,aACE,QAAQ,SAAS,QAAQ,OAAO,QAAQ,IAAI,SAAS;AAAA,EAC1D;AACJ;AAqBO,SAAS,oBACd,wBAA6D,OAAO,KACpE,SACwB;AACvB,IAAA,mCAAS,aAAY,yBAAyB,mBAAmB;AAClE,QAAM,YAAW,mCAAS,aAAY,OAAO,QAAQ;AAC/C,QAAA,aAAa,SAAS,IAAI,UAAU;AACpC,QAAA,SAAS,SAAS,IAAI,MAAM;AAC5B,QAAA,cAAc,SAAS,IAAI,WAAW;AACtC,QAAA,gBAAgB,YAAY,iBAAiB;AAM7C,QAAA,0BAA0B,SAAS,MAAM;AACtC,WAAA;AAAA,MACL,UAAU,eAAe,uBAAuB;AAAA,MAChD,YAAY,IAAI;AAAA,IAClB;AAAA,EAAA,CACD;AAMD,QAAM,6BAA6B;AAAA,IACjC;AAAA,EACF;AAKM,QAAA,wBAAwB,SAAS,MAAM;AAC3C,UAAM,gBAAgB,wBAAwB;AAC9C,UAAM,mBAAmB,2BAA2B;AAC7C,WAAA,oBAAoB,iBAAiB,CAAC,IAAI,cAAc,CAAC,IAC5D,iBAAiB,CAAC,IAClB,cAAc,CAAC;AAAA,EAAA,CACpB;AAED,QAAM,cAAc,OAAO;AAAA,IAAkB,MAC3C,cAAc;AAAA,MACZ,cAAc,WAAW,MAAM;AACvB,cAAA,CAAC,UAAU,IAAI,sBAAsB;AAC3C,cAAM,aAAa;AAAA,UACjB;AAAA,UACA,UAAU,eAAe,sBAAuB,CAAA;AAAA,QAClD;AACA,YAAI,eAAe,YAAY;AAC7B,iBAAO,IAAI,MAAM;AACf,uCAA2B,IAAI,CAAC,YAAY,YAAY,IAAK,CAAA,CAAC;AAAA,UAAA,CAC/D;AAAA,QAAA;AAAA,MAEJ,CAAA;AAAA,IAAA;AAAA,EAEL;AAEA,aAAW,UAAU,WAAW;AAEzB,SAAA;AACT;"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { assertInInjectionContext, inject, Injector, DestroyRef, NgZone, computed, signal, effect, untracked } from "@angular/core";
|
|
2
|
+
import { QueryClient, MutationObserver, noop, notifyManager, shouldThrowError } from "@tanstack/query-core";
|
|
3
|
+
import { signalProxy } from "./signal-proxy.mjs";
|
|
4
|
+
function injectMutation(injectMutationFn, options) {
|
|
5
|
+
!(options == null ? void 0 : options.injector) && assertInInjectionContext(injectMutation);
|
|
6
|
+
const injector = (options == null ? void 0 : options.injector) ?? inject(Injector);
|
|
7
|
+
const destroyRef = injector.get(DestroyRef);
|
|
8
|
+
const ngZone = injector.get(NgZone);
|
|
9
|
+
const queryClient = injector.get(QueryClient);
|
|
10
|
+
const optionsSignal = computed(injectMutationFn);
|
|
11
|
+
const observerSignal = (() => {
|
|
12
|
+
let instance = null;
|
|
13
|
+
return computed(() => {
|
|
14
|
+
return instance || (instance = new MutationObserver(queryClient, optionsSignal()));
|
|
15
|
+
});
|
|
16
|
+
})();
|
|
17
|
+
const mutateFnSignal = computed(() => {
|
|
18
|
+
const observer = observerSignal();
|
|
19
|
+
return (variables, mutateOptions) => {
|
|
20
|
+
observer.mutate(variables, mutateOptions).catch(noop);
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
const resultFromInitialOptionsSignal = computed(() => {
|
|
24
|
+
const observer = observerSignal();
|
|
25
|
+
return observer.getCurrentResult();
|
|
26
|
+
});
|
|
27
|
+
const resultFromSubscriberSignal = signal(null);
|
|
28
|
+
effect(
|
|
29
|
+
() => {
|
|
30
|
+
const observer = observerSignal();
|
|
31
|
+
const observerOptions = optionsSignal();
|
|
32
|
+
untracked(() => {
|
|
33
|
+
observer.setOptions(observerOptions);
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
injector
|
|
38
|
+
}
|
|
39
|
+
);
|
|
40
|
+
effect(
|
|
41
|
+
() => {
|
|
42
|
+
const observer = observerSignal();
|
|
43
|
+
untracked(() => {
|
|
44
|
+
const unsubscribe = ngZone.runOutsideAngular(
|
|
45
|
+
() => observer.subscribe(
|
|
46
|
+
notifyManager.batchCalls((state) => {
|
|
47
|
+
ngZone.run(() => {
|
|
48
|
+
if (state.isError && shouldThrowError(observer.options.throwOnError, [state.error])) {
|
|
49
|
+
ngZone.onError.emit(state.error);
|
|
50
|
+
throw state.error;
|
|
51
|
+
}
|
|
52
|
+
resultFromSubscriberSignal.set(state);
|
|
53
|
+
});
|
|
54
|
+
})
|
|
55
|
+
)
|
|
56
|
+
);
|
|
57
|
+
destroyRef.onDestroy(unsubscribe);
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
injector
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
const resultSignal = computed(() => {
|
|
65
|
+
const resultFromSubscriber = resultFromSubscriberSignal();
|
|
66
|
+
const resultFromInitialOptions = resultFromInitialOptionsSignal();
|
|
67
|
+
const result = resultFromSubscriber ?? resultFromInitialOptions;
|
|
68
|
+
return {
|
|
69
|
+
...result,
|
|
70
|
+
mutate: mutateFnSignal(),
|
|
71
|
+
mutateAsync: result.mutate
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
return signalProxy(resultSignal);
|
|
75
|
+
}
|
|
76
|
+
export {
|
|
77
|
+
injectMutation
|
|
78
|
+
};
|
|
79
|
+
//# sourceMappingURL=inject-mutation.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-mutation.mjs","sources":["../src/inject-mutation.ts"],"sourcesContent":["import {\n DestroyRef,\n Injector,\n NgZone,\n assertInInjectionContext,\n computed,\n effect,\n inject,\n signal,\n untracked,\n} from '@angular/core'\nimport {\n MutationObserver,\n QueryClient,\n noop,\n notifyManager,\n shouldThrowError,\n} from '@tanstack/query-core'\nimport { signalProxy } from './signal-proxy'\nimport type { DefaultError, MutationObserverResult } from '@tanstack/query-core'\nimport type { CreateMutateFunction, CreateMutationResult } from './types'\nimport type { CreateMutationOptions } from './mutation-options'\n\nexport interface InjectMutationOptions {\n /**\n * The `Injector` in which to create the mutation.\n *\n * If this is not provided, the current injection context will be used instead (via `inject`).\n */\n injector?: Injector\n}\n\n/**\n * Injects a mutation: an imperative function that can be invoked which typically performs server side effects.\n *\n * Unlike queries, mutations are not run automatically.\n * @param injectMutationFn - A function that returns mutation options.\n * @param options - Additional configuration\n * @returns The mutation.\n * @public\n */\nexport function injectMutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n>(\n injectMutationFn: () => CreateMutationOptions<\n TData,\n TError,\n TVariables,\n TContext\n >,\n options?: InjectMutationOptions,\n): CreateMutationResult<TData, TError, TVariables, TContext> {\n !options?.injector && assertInInjectionContext(injectMutation)\n const injector = options?.injector ?? inject(Injector)\n const destroyRef = injector.get(DestroyRef)\n const ngZone = injector.get(NgZone)\n const queryClient = injector.get(QueryClient)\n\n /**\n * computed() is used so signals can be inserted into the options\n * making it reactive. Wrapping options in a function ensures embedded expressions\n * are preserved and can keep being applied after signal changes\n */\n const optionsSignal = computed(injectMutationFn)\n\n const observerSignal = (() => {\n let instance: MutationObserver<TData, TError, TVariables, TContext> | null =\n null\n\n return computed(() => {\n return (instance ||= new MutationObserver(queryClient, optionsSignal()))\n })\n })()\n\n const mutateFnSignal = computed<\n CreateMutateFunction<TData, TError, TVariables, TContext>\n >(() => {\n const observer = observerSignal()\n return (variables, mutateOptions) => {\n observer.mutate(variables, mutateOptions).catch(noop)\n }\n })\n\n /**\n * Computed signal that gets result from mutation cache based on passed options\n */\n const resultFromInitialOptionsSignal = computed(() => {\n const observer = observerSignal()\n return observer.getCurrentResult()\n })\n\n /**\n * Signal that contains result set by subscriber\n */\n const resultFromSubscriberSignal = signal<MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > | null>(null)\n\n effect(\n () => {\n const observer = observerSignal()\n const observerOptions = optionsSignal()\n\n untracked(() => {\n observer.setOptions(observerOptions)\n })\n },\n {\n injector,\n },\n )\n\n effect(\n () => {\n // observer.trackResult is not used as this optimization is not needed for Angular\n const observer = observerSignal()\n\n untracked(() => {\n const unsubscribe = ngZone.runOutsideAngular(() =>\n observer.subscribe(\n notifyManager.batchCalls((state) => {\n ngZone.run(() => {\n if (\n state.isError &&\n shouldThrowError(observer.options.throwOnError, [state.error])\n ) {\n ngZone.onError.emit(state.error)\n throw state.error\n }\n\n resultFromSubscriberSignal.set(state)\n })\n }),\n ),\n )\n destroyRef.onDestroy(unsubscribe)\n })\n },\n {\n injector,\n },\n )\n\n const resultSignal = computed(() => {\n const resultFromSubscriber = resultFromSubscriberSignal()\n const resultFromInitialOptions = resultFromInitialOptionsSignal()\n\n const result = resultFromSubscriber ?? resultFromInitialOptions\n\n return {\n ...result,\n mutate: mutateFnSignal(),\n mutateAsync: result.mutate,\n }\n })\n\n return signalProxy(resultSignal) as CreateMutationResult<\n TData,\n TError,\n TVariables,\n TContext\n >\n}\n"],"names":[],"mappings":";;;AAyCgB,SAAA,eAMd,kBAMA,SAC2D;AAC1D,IAAA,mCAAS,aAAY,yBAAyB,cAAc;AAC7D,QAAM,YAAW,mCAAS,aAAY,OAAO,QAAQ;AAC/C,QAAA,aAAa,SAAS,IAAI,UAAU;AACpC,QAAA,SAAS,SAAS,IAAI,MAAM;AAC5B,QAAA,cAAc,SAAS,IAAI,WAAW;AAOtC,QAAA,gBAAgB,SAAS,gBAAgB;AAE/C,QAAM,kBAAkB,MAAM;AAC5B,QAAI,WACF;AAEF,WAAO,SAAS,MAAM;AACpB,aAAQ,wBAAa,IAAI,iBAAiB,aAAa,eAAe;AAAA,IAAA,CACvE;AAAA,EAAA,GACA;AAEG,QAAA,iBAAiB,SAErB,MAAM;AACN,UAAM,WAAW,eAAe;AACzB,WAAA,CAAC,WAAW,kBAAkB;AACnC,eAAS,OAAO,WAAW,aAAa,EAAE,MAAM,IAAI;AAAA,IACtD;AAAA,EAAA,CACD;AAKK,QAAA,iCAAiC,SAAS,MAAM;AACpD,UAAM,WAAW,eAAe;AAChC,WAAO,SAAS,iBAAiB;AAAA,EAAA,CAClC;AAKK,QAAA,6BAA6B,OAKzB,IAAI;AAEd;AAAA,IACE,MAAM;AACJ,YAAM,WAAW,eAAe;AAChC,YAAM,kBAAkB,cAAc;AAEtC,gBAAU,MAAM;AACd,iBAAS,WAAW,eAAe;AAAA,MAAA,CACpC;AAAA,IACH;AAAA,IACA;AAAA,MACE;AAAA,IAAA;AAAA,EAEJ;AAEA;AAAA,IACE,MAAM;AAEJ,YAAM,WAAW,eAAe;AAEhC,gBAAU,MAAM;AACd,cAAM,cAAc,OAAO;AAAA,UAAkB,MAC3C,SAAS;AAAA,YACP,cAAc,WAAW,CAAC,UAAU;AAClC,qBAAO,IAAI,MAAM;AAEb,oBAAA,MAAM,WACN,iBAAiB,SAAS,QAAQ,cAAc,CAAC,MAAM,KAAK,CAAC,GAC7D;AACO,yBAAA,QAAQ,KAAK,MAAM,KAAK;AAC/B,wBAAM,MAAM;AAAA,gBAAA;AAGd,2CAA2B,IAAI,KAAK;AAAA,cAAA,CACrC;AAAA,YACF,CAAA;AAAA,UAAA;AAAA,QAEL;AACA,mBAAW,UAAU,WAAW;AAAA,MAAA,CACjC;AAAA,IACH;AAAA,IACA;AAAA,MACE;AAAA,IAAA;AAAA,EAEJ;AAEM,QAAA,eAAe,SAAS,MAAM;AAClC,UAAM,uBAAuB,2BAA2B;AACxD,UAAM,2BAA2B,+BAA+B;AAEhE,UAAM,SAAS,wBAAwB;AAEhC,WAAA;AAAA,MACL,GAAG;AAAA,MACH,QAAQ,eAAe;AAAA,MACvB,aAAa,OAAO;AAAA,IACtB;AAAA,EAAA,CACD;AAED,SAAO,YAAY,YAAY;AAMjC;"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { QueryClient, QueriesObserver, notifyManager } from "@tanstack/query-core";
|
|
2
|
+
import { assertInInjectionContext, runInInjectionContext, inject, Injector, DestroyRef, NgZone, computed, effect, signal } from "@angular/core";
|
|
3
|
+
import { injectIsRestoring } from "./inject-is-restoring.mjs";
|
|
4
|
+
function injectQueries({
|
|
5
|
+
queries,
|
|
6
|
+
...options
|
|
7
|
+
}, injector) {
|
|
8
|
+
!injector && assertInInjectionContext(injectQueries);
|
|
9
|
+
return runInInjectionContext(injector ?? inject(Injector), () => {
|
|
10
|
+
const destroyRef = inject(DestroyRef);
|
|
11
|
+
const ngZone = inject(NgZone);
|
|
12
|
+
const queryClient = inject(QueryClient);
|
|
13
|
+
const isRestoring = injectIsRestoring();
|
|
14
|
+
const defaultedQueries = computed(() => {
|
|
15
|
+
return queries().map((opts) => {
|
|
16
|
+
const defaultedOptions = queryClient.defaultQueryOptions(opts);
|
|
17
|
+
defaultedOptions._optimisticResults = isRestoring() ? "isRestoring" : "optimistic";
|
|
18
|
+
return defaultedOptions;
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
const observer = new QueriesObserver(
|
|
22
|
+
queryClient,
|
|
23
|
+
defaultedQueries(),
|
|
24
|
+
options
|
|
25
|
+
);
|
|
26
|
+
effect(() => {
|
|
27
|
+
observer.setQueries(
|
|
28
|
+
defaultedQueries(),
|
|
29
|
+
options
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
const [, getCombinedResult] = observer.getOptimisticResult(
|
|
33
|
+
defaultedQueries(),
|
|
34
|
+
options.combine
|
|
35
|
+
);
|
|
36
|
+
const result = signal(getCombinedResult());
|
|
37
|
+
effect(() => {
|
|
38
|
+
const unsubscribe = isRestoring() ? () => void 0 : ngZone.runOutsideAngular(
|
|
39
|
+
() => observer.subscribe(notifyManager.batchCalls(result.set))
|
|
40
|
+
);
|
|
41
|
+
destroyRef.onDestroy(unsubscribe);
|
|
42
|
+
});
|
|
43
|
+
return result;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
export {
|
|
47
|
+
injectQueries
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=inject-queries.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-queries.mjs","sources":["../src/inject-queries.ts"],"sourcesContent":["import {\n QueriesObserver,\n QueryClient,\n notifyManager,\n} from '@tanstack/query-core'\nimport {\n DestroyRef,\n Injector,\n NgZone,\n assertInInjectionContext,\n computed,\n effect,\n inject,\n runInInjectionContext,\n signal,\n} from '@angular/core'\nimport { injectIsRestoring } from './inject-is-restoring'\nimport type { Signal } from '@angular/core'\nimport type {\n DefaultError,\n OmitKeyof,\n QueriesObserverOptions,\n QueriesPlaceholderDataFunction,\n QueryFunction,\n QueryKey,\n QueryObserverOptions,\n QueryObserverResult,\n ThrowOnError,\n} from '@tanstack/query-core'\n\n// This defines the `CreateQueryOptions` that are accepted in `QueriesOptions` & `GetOptions`.\n// `placeholderData` function does not have a parameter\ntype QueryObserverOptionsForCreateQueries<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> = OmitKeyof<\n QueryObserverOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>,\n 'placeholderData'\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 GetOptions<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 ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? QueryObserverOptionsForCreateQueries<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 ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError, TData>\n : T extends [infer TQueryFnData, infer TError]\n ? QueryObserverOptionsForCreateQueries<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? QueryObserverOptionsForCreateQueries<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 ? QueryObserverOptionsForCreateQueries<\n TQueryFnData,\n unknown extends TError ? DefaultError : TError,\n unknown extends TData ? TQueryFnData : TData,\n TQueryKey\n >\n : // Fallback\n QueryObserverOptionsForCreateQueries\n\ntype GetResults<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 ? QueryObserverResult<TData, TError>\n : T extends { queryFnData: infer TQueryFnData; error?: infer TError }\n ? QueryObserverResult<TQueryFnData, TError>\n : T extends { data: infer TData; error?: infer TError }\n ? QueryObserverResult<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 ? QueryObserverResult<TData, TError>\n : T extends [infer TQueryFnData, infer TError]\n ? QueryObserverResult<TQueryFnData, TError>\n : T extends [infer TQueryFnData]\n ? QueryObserverResult<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 ? QueryObserverResult<\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >\n : // Fallback\n QueryObserverResult\n\n/**\n * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param\n * @public\n */\nexport type QueriesOptions<\n T extends Array<any>,\n TResult extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<QueryObserverOptionsForCreateQueries>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResult, GetOptions<Head>]\n : T extends [infer Head, ...infer Tail]\n ? QueriesOptions<\n [...Tail],\n [...TResult, GetOptions<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/homogenous type!\n // use this to infer the param types in the case of Array.map() argument\n T extends Array<\n QueryObserverOptionsForCreateQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n infer TQueryKey\n >\n >\n ? Array<\n QueryObserverOptionsForCreateQueries<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >\n >\n : // Fallback\n Array<QueryObserverOptionsForCreateQueries>\n\n/**\n * QueriesResults reducer recursively maps type param to results\n * @public\n */\nexport type QueriesResults<\n T extends Array<any>,\n TResult extends Array<any> = [],\n TDepth extends ReadonlyArray<number> = [],\n> = TDepth['length'] extends MAXIMUM_DEPTH\n ? Array<QueryObserverResult>\n : T extends []\n ? []\n : T extends [infer Head]\n ? [...TResult, GetResults<Head>]\n : T extends [infer Head, ...infer Tail]\n ? QueriesResults<\n [...Tail],\n [...TResult, GetResults<Head>],\n [...TDepth, 1]\n >\n : T extends Array<\n QueryObserverOptionsForCreateQueries<\n infer TQueryFnData,\n infer TError,\n infer TData,\n any\n >\n >\n ? // Dynamic-size (homogenous) CreateQueryOptions array: map directly to array of results\n Array<\n QueryObserverResult<\n unknown extends TData ? TQueryFnData : TData,\n unknown extends TError ? DefaultError : TError\n >\n >\n : // Fallback\n Array<QueryObserverResult>\n\n/**\n * @param root0\n * @param root0.queries\n * @param root0.combine\n * @param injector\n * @param injector\n * @public\n */\nexport function injectQueries<\n T extends Array<any>,\n TCombinedResult = QueriesResults<T>,\n>(\n {\n queries,\n ...options\n }: {\n queries: Signal<[...QueriesOptions<T>]>\n combine?: (result: QueriesResults<T>) => TCombinedResult\n },\n injector?: Injector,\n): Signal<TCombinedResult> {\n !injector && assertInInjectionContext(injectQueries)\n return runInInjectionContext(injector ?? inject(Injector), () => {\n const destroyRef = inject(DestroyRef)\n const ngZone = inject(NgZone)\n const queryClient = inject(QueryClient)\n const isRestoring = injectIsRestoring()\n\n const defaultedQueries = computed(() => {\n return queries().map((opts) => {\n const defaultedOptions = queryClient.defaultQueryOptions(opts)\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 as QueryObserverOptions\n })\n })\n\n const observer = new QueriesObserver<TCombinedResult>(\n queryClient,\n defaultedQueries(),\n options as QueriesObserverOptions<TCombinedResult>,\n )\n\n // Do not notify on updates because of changes in the options because\n // these changes should already be reflected in the optimistic result.\n effect(() => {\n observer.setQueries(\n defaultedQueries(),\n options as QueriesObserverOptions<TCombinedResult>,\n )\n })\n\n const [, getCombinedResult] = observer.getOptimisticResult(\n defaultedQueries(),\n (options as QueriesObserverOptions<TCombinedResult>).combine,\n )\n\n const result = signal(getCombinedResult() as any)\n\n effect(() => {\n const unsubscribe = isRestoring()\n ? () => undefined\n : ngZone.runOutsideAngular(() =>\n observer.subscribe(notifyManager.batchCalls(result.set)),\n )\n destroyRef.onDestroy(unsubscribe)\n })\n\n return result\n })\n}\n"],"names":[],"mappings":";;;AA6MO,SAAS,cAId;AAAA,EACE;AAAA,EACA,GAAG;AACL,GAIA,UACyB;AACxB,GAAA,YAAY,yBAAyB,aAAa;AACnD,SAAO,sBAAsB,YAAY,OAAO,QAAQ,GAAG,MAAM;AACzD,UAAA,aAAa,OAAO,UAAU;AAC9B,UAAA,SAAS,OAAO,MAAM;AACtB,UAAA,cAAc,OAAO,WAAW;AACtC,UAAM,cAAc,kBAAkB;AAEhC,UAAA,mBAAmB,SAAS,MAAM;AACtC,aAAO,QAAQ,EAAE,IAAI,CAAC,SAAS;AACvB,cAAA,mBAAmB,YAAY,oBAAoB,IAAI;AAE5C,yBAAA,qBAAqB,YAAY,IAC9C,gBACA;AAEG,eAAA;AAAA,MAAA,CACR;AAAA,IAAA,CACF;AAED,UAAM,WAAW,IAAI;AAAA,MACnB;AAAA,MACA,iBAAiB;AAAA,MACjB;AAAA,IACF;AAIA,WAAO,MAAM;AACF,eAAA;AAAA,QACP,iBAAiB;AAAA,QACjB;AAAA,MACF;AAAA,IAAA,CACD;AAED,UAAM,CAAG,EAAA,iBAAiB,IAAI,SAAS;AAAA,MACrC,iBAAiB;AAAA,MAChB,QAAoD;AAAA,IACvD;AAEM,UAAA,SAAS,OAAO,mBAA0B;AAEhD,WAAO,MAAM;AACX,YAAM,cAAc,YAAA,IAChB,MAAM,SACN,OAAO;AAAA,QAAkB,MACvB,SAAS,UAAU,cAAc,WAAW,OAAO,GAAG,CAAC;AAAA,MACzD;AACJ,iBAAW,UAAU,WAAW;AAAA,IAAA,CACjC;AAEM,WAAA;AAAA,EAAA,CACR;AACH;"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { inject, Injector } from "@angular/core";
|
|
2
|
+
import { QueryClient } from "@tanstack/query-core";
|
|
3
|
+
function injectQueryClient(injectOptions = {}) {
|
|
4
|
+
return (injectOptions.injector ?? inject(Injector)).get(QueryClient);
|
|
5
|
+
}
|
|
6
|
+
export {
|
|
7
|
+
injectQueryClient
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=inject-query-client.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-query-client.mjs","sources":["../src/inject-query-client.ts"],"sourcesContent":["import { Injector, inject } from '@angular/core'\nimport { QueryClient } from '@tanstack/query-core'\nimport type { InjectOptions } from '@angular/core'\n\n/**\n * Injects a `QueryClient` instance and allows passing a custom injector.\n * @param injectOptions - Type of the options argument to inject and optionally a custom injector.\n * @returns The `QueryClient` instance.\n * @public\n * @deprecated Use `inject(QueryClient)` instead.\n * If you need to get a `QueryClient` from a custom injector, use `injector.get(QueryClient)`.\n *\n *\n * **Example**\n * ```ts\n * const queryClient = injectQueryClient();\n * ```\n */\nexport function injectQueryClient(\n injectOptions: InjectOptions & { injector?: Injector } = {},\n) {\n return (injectOptions.injector ?? inject(Injector)).get(QueryClient)\n}\n"],"names":[],"mappings":";;AAkBgB,SAAA,kBACd,gBAAyD,IACzD;AACA,UAAQ,cAAc,YAAY,OAAO,QAAQ,GAAG,IAAI,WAAW;AACrE;"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { QueryObserver } from "@tanstack/query-core";
|
|
2
|
+
import { assertInInjectionContext, runInInjectionContext, inject, Injector } from "@angular/core";
|
|
3
|
+
import { createBaseQuery } from "./create-base-query.mjs";
|
|
4
|
+
function injectQuery(injectQueryFn, options) {
|
|
5
|
+
!(options == null ? void 0 : options.injector) && assertInInjectionContext(injectQuery);
|
|
6
|
+
return runInInjectionContext(
|
|
7
|
+
(options == null ? void 0 : options.injector) ?? inject(Injector),
|
|
8
|
+
() => createBaseQuery(injectQueryFn, QueryObserver)
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
injectQuery
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=inject-query.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inject-query.mjs","sources":["../src/inject-query.ts"],"sourcesContent":["import { QueryObserver } from '@tanstack/query-core'\nimport {\n Injector,\n assertInInjectionContext,\n inject,\n runInInjectionContext,\n} from '@angular/core'\nimport { createBaseQuery } from './create-base-query'\nimport type { DefaultError, QueryKey } from '@tanstack/query-core'\nimport type {\n CreateQueryOptions,\n CreateQueryResult,\n DefinedCreateQueryResult,\n} from './types'\nimport type {\n DefinedInitialDataOptions,\n UndefinedInitialDataOptions,\n} from './query-options'\n\nexport interface InjectQueryOptions {\n /**\n * The `Injector` in which to create the query.\n *\n * If this is not provided, the current injection context will be used instead (via `inject`).\n */\n injector?: Injector\n}\n\n/**\n * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n *\n * **Basic example**\n * ```ts\n * class ServiceOrComponent {\n * query = injectQuery(() => ({\n * queryKey: ['repoData'],\n * queryFn: () =>\n * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),\n * }))\n * }\n * ```\n *\n * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.\n * In the example below, the query will be automatically enabled and executed when the filter signal changes\n * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.\n *\n * **Reactive example**\n * ```ts\n * class ServiceOrComponent {\n * filter = signal('')\n *\n * todosQuery = injectQuery(() => ({\n * queryKey: ['todos', this.filter()],\n * queryFn: () => fetchTodos(this.filter()),\n * // Signals can be combined with expressions\n * enabled: !!this.filter(),\n * }))\n * }\n * ```\n * @param injectQueryFn - A function that returns query options.\n * @param options - Additional configuration\n * @returns The query result.\n * @public\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries\n */\nexport function injectQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n injectQueryFn: () => DefinedInitialDataOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >,\n options?: InjectQueryOptions,\n): DefinedCreateQueryResult<TData, TError>\n\n/**\n * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n *\n * **Basic example**\n * ```ts\n * class ServiceOrComponent {\n * query = injectQuery(() => ({\n * queryKey: ['repoData'],\n * queryFn: () =>\n * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),\n * }))\n * }\n * ```\n *\n * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.\n * In the example below, the query will be automatically enabled and executed when the filter signal changes\n * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.\n *\n * **Reactive example**\n * ```ts\n * class ServiceOrComponent {\n * filter = signal('')\n *\n * todosQuery = injectQuery(() => ({\n * queryKey: ['todos', this.filter()],\n * queryFn: () => fetchTodos(this.filter()),\n * // Signals can be combined with expressions\n * enabled: !!this.filter(),\n * }))\n * }\n * ```\n * @param injectQueryFn - A function that returns query options.\n * @param options - Additional configuration\n * @returns The query result.\n * @public\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries\n */\nexport function injectQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n injectQueryFn: () => UndefinedInitialDataOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >,\n options?: InjectQueryOptions,\n): CreateQueryResult<TData, TError>\n\n/**\n * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n *\n * **Basic example**\n * ```ts\n * class ServiceOrComponent {\n * query = injectQuery(() => ({\n * queryKey: ['repoData'],\n * queryFn: () =>\n * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),\n * }))\n * }\n * ```\n *\n * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.\n * In the example below, the query will be automatically enabled and executed when the filter signal changes\n * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.\n *\n * **Reactive example**\n * ```ts\n * class ServiceOrComponent {\n * filter = signal('')\n *\n * todosQuery = injectQuery(() => ({\n * queryKey: ['todos', this.filter()],\n * queryFn: () => fetchTodos(this.filter()),\n * // Signals can be combined with expressions\n * enabled: !!this.filter(),\n * }))\n * }\n * ```\n * @param injectQueryFn - A function that returns query options.\n * @param options - Additional configuration\n * @returns The query result.\n * @public\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries\n */\nexport function injectQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n injectQueryFn: () => CreateQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >,\n options?: InjectQueryOptions,\n): CreateQueryResult<TData, TError>\n\n/**\n * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n *\n * **Basic example**\n * ```ts\n * class ServiceOrComponent {\n * query = injectQuery(() => ({\n * queryKey: ['repoData'],\n * queryFn: () =>\n * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),\n * }))\n * }\n * ```\n *\n * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.\n * In the example below, the query will be automatically enabled and executed when the filter signal changes\n * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.\n *\n * **Reactive example**\n * ```ts\n * class ServiceOrComponent {\n * filter = signal('')\n *\n * todosQuery = injectQuery(() => ({\n * queryKey: ['todos', this.filter()],\n * queryFn: () => fetchTodos(this.filter()),\n * // Signals can be combined with expressions\n * enabled: !!this.filter(),\n * }))\n * }\n * ```\n * @param injectQueryFn - A function that returns query options.\n * @param options - Additional configuration\n * @returns The query result.\n * @public\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries\n */\nexport function injectQuery(\n injectQueryFn: () => CreateQueryOptions,\n options?: InjectQueryOptions,\n) {\n !options?.injector && assertInInjectionContext(injectQuery)\n return runInInjectionContext(options?.injector ?? inject(Injector), () =>\n createBaseQuery(injectQueryFn, QueryObserver),\n ) as unknown as CreateQueryResult\n}\n"],"names":[],"mappings":";;;AA6NgB,SAAA,YACd,eACA,SACA;AACC,IAAA,mCAAS,aAAY,yBAAyB,WAAW;AACnD,SAAA;AAAA,KAAsB,mCAAS,aAAY,OAAO,QAAQ;AAAA,IAAG,MAClE,gBAAgB,eAAe,aAAa;AAAA,EAC9C;AACF;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutation-options.mjs","sources":["../src/mutation-options.ts"],"sourcesContent":["import type {\n DefaultError,\n MutationObserverOptions,\n OmitKeyof,\n} from '@tanstack/query-core'\n\n/**\n * Allows to share and re-use mutation options in a type-safe way.\n *\n * **Example**\n *\n * ```ts\n * export class QueriesService {\n * private http = inject(HttpClient);\n *\n * updatePost(id: number) {\n * return mutationOptions({\n * mutationFn: (post: Post) => Promise.resolve(post),\n * mutationKey: [\"updatePost\", id],\n * onSuccess: (newPost) => {\n * // ^? newPost: Post\n * this.queryClient.setQueryData([\"posts\", id], newPost);\n * },\n * });\n * }\n * }\n *\n * queries = inject(QueriesService)\n * idSignal = new Signal(0);\n * mutation = injectMutation(() => this.queries.updatePost(this.idSignal()))\n *\n * mutation.mutate({ title: 'New Title' })\n * ```\n * @param options - The mutation options.\n * @returns Mutation options.\n * @public\n */\nexport function mutationOptions<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n>(\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n): CreateMutationOptions<TData, TError, TVariables, TContext> {\n return options\n}\n\n/**\n * @public\n */\nexport interface CreateMutationOptions<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends OmitKeyof<\n MutationObserverOptions<TData, TError, TVariables, TContext>,\n '_defaulted'\n > {}\n"],"names":[],"mappings":"AAqCO,SAAS,gBAMd,SAC4D;AACrD,SAAA;AACT;"}
|