@tanstack/angular-query 5.0.0-rc.3 → 5.0.0-rc.5
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/hydration-state-key.mjs +10 -13
- package/hydration-state-key.mjs.map +1 -1
- package/index.mjs +8 -20
- package/index.mjs.map +1 -1
- package/infinite-query-options.d.ts +7 -18
- package/infinite-query-options.mjs +6 -5
- package/infinite-query-options.mjs.map +1 -1
- package/inject-base-query.d.ts +3 -0
- package/inject-base-query.mjs +62 -58
- package/inject-base-query.mjs.map +1 -1
- package/inject-infinite-query.d.ts +7 -11
- package/inject-infinite-query.mjs +16 -32
- package/inject-infinite-query.mjs.map +1 -1
- package/inject-is-fetching.d.ts +1 -1
- package/inject-is-fetching.mjs +22 -15
- package/inject-is-fetching.mjs.map +1 -1
- package/inject-is-mutating.d.ts +1 -1
- package/inject-is-mutating.mjs +22 -15
- package/inject-is-mutating.mjs.map +1 -1
- package/inject-is-restoring.d.ts +2 -0
- package/inject-is-restoring.mjs +28 -17
- package/inject-is-restoring.mjs.map +1 -1
- package/inject-mutation-state.d.ts +9 -0
- package/inject-mutation-state.mjs +25 -23
- package/inject-mutation-state.mjs.map +1 -1
- package/inject-mutation.d.ts +2 -4
- package/inject-mutation.mjs +87 -67
- package/inject-mutation.mjs.map +1 -1
- package/inject-queries.d.ts +1 -3
- package/inject-queries.mjs +122 -114
- package/inject-queries.mjs.map +1 -1
- package/inject-queries.types.d.ts +17 -1
- package/inject-query.d.ts +7 -13
- package/inject-query.mjs +11 -14
- package/inject-query.mjs.map +1 -1
- package/internal.mjs +2 -7
- package/mutation-options.d.ts +2 -6
- package/mutation-options.mjs +6 -5
- package/mutation-options.mjs.map +1 -1
- package/package.json +2 -2
- package/providers.d.ts +16 -14
- package/providers.mjs +138 -75
- package/providers.mjs.map +1 -1
- package/query-options.d.ts +6 -16
- package/query-options.mjs +6 -5
- package/query-options.mjs.map +1 -1
- package/query-resource.d.ts +4 -3
- package/query-resource.mjs +72 -63
- package/query-resource.mjs.map +1 -1
- package/schematics/migrate-from-experimental/index.js +38 -9
- package/schematics/migrate-from-experimental/workspace.js +48 -12
- package/schematics/ng-add/index.js +1 -1
- package/types.d.ts +76 -16
- package/utils/inject-external-store.d.ts +3 -0
- package/utils/inject-external-store.mjs +54 -38
- package/utils/inject-external-store.mjs.map +1 -1
- package/utils/inject-pending-tasks-lifecycle.d.ts +13 -1
- package/utils/inject-pending-tasks-lifecycle.mjs +32 -29
- package/utils/inject-pending-tasks-lifecycle.mjs.map +1 -1
- package/utils/inject-query-zone.d.ts +4 -1
- package/utils/inject-query-zone.mjs +12 -7
- package/utils/inject-query-zone.mjs.map +1 -1
- package/utils/resource-types.d.ts +3 -0
- package/utils/result-fields.d.ts +9 -0
- package/utils/result-fields.mjs +55 -62
- package/utils/result-fields.mjs.map +1 -1
- package/utils/signal-proxy.d.ts +6 -0
- package/utils/signal-proxy.mjs +16 -9
- package/utils/signal-proxy.mjs.map +1 -1
- package/internal.mjs.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject-is-fetching.mjs","sources":["../src/inject-is-fetching.ts"],"sourcesContent":["import { assertInInjectionContext, inject } from '@angular/core'\nimport { QueryClient } from '@tanstack/query-core'\nimport { injectExternalStore } from './utils/inject-external-store'\nimport type { QueryFilters } from '@tanstack/query-core'\nimport type { Signal } from '@angular/core'\n\n/**\n * Injects a signal that tracks the number of queries that your application is loading or fetching in the\n * background (useful for app-wide loading indicators).\n *\n * Can be used for app-wide loading indicators
|
|
1
|
+
{"version":3,"file":"inject-is-fetching.mjs","names":[],"sources":["../src/inject-is-fetching.ts"],"sourcesContent":["import { assertInInjectionContext, inject } from '@angular/core'\nimport { QueryClient } from '@tanstack/query-core'\nimport { injectExternalStore } from './utils/inject-external-store'\nimport type { QueryFilters } from '@tanstack/query-core'\nimport type { Signal } from '@angular/core'\n\n/**\n * Injects a signal that tracks the number of queries that your application is loading or fetching in the\n * background (useful for app-wide loading indicators).\n *\n * Can be used for app-wide loading indicators.\n * @param filters - A reactive factory for the filters.\n * @returns signal with number of loading or fetching queries.\n */\nexport function injectIsFetching(\n filters: () => QueryFilters = () => ({}),\n): Signal<number> {\n if (typeof ngDevMode === 'undefined' || ngDevMode) {\n assertInInjectionContext(injectIsFetching)\n }\n const queryClient = inject(QueryClient)\n const cache = queryClient.getQueryCache()\n\n return injectExternalStore(() => ({\n getSnapshot: () => queryClient.isFetching(filters()),\n subscribe: (onStoreChange) => cache.subscribe(onStoreChange),\n }))\n}\n"],"mappings":";;;;;;;;;;;;AAcA,SAAgB,iBACd,iBAAqC,CAAC,IACtB;CAChB,IAAI,OAAO,cAAc,eAAe,WACtC,yBAAyB,gBAAgB;CAE3C,MAAM,cAAc,OAAO,WAAW;CACtC,MAAM,QAAQ,YAAY,cAAc;CAExC,OAAO,2BAA2B;EAChC,mBAAmB,YAAY,WAAW,QAAQ,CAAC;EACnD,YAAY,kBAAkB,MAAM,UAAU,aAAa;CAC7D,EAAE;AACJ"}
|
package/inject-is-mutating.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { Signal } from '@angular/core';
|
|
|
4
4
|
* Injects a signal that tracks the number of mutations that your application currently has `pending`
|
|
5
5
|
* (useful for app-wide loading indicators).
|
|
6
6
|
*
|
|
7
|
-
* Can be used for app-wide loading indicators
|
|
7
|
+
* Can be used for app-wide loading indicators.
|
|
8
8
|
* @param filters - A reactive factory for the filters.
|
|
9
9
|
* @returns A read-only signal with the number of fetching mutations.
|
|
10
10
|
*/
|
package/inject-is-mutating.mjs
CHANGED
|
@@ -1,18 +1,25 @@
|
|
|
1
|
-
import { assertInInjectionContext, inject } from "@angular/core";
|
|
2
|
-
import { QueryClient } from "@tanstack/query-core";
|
|
3
1
|
import { injectExternalStore } from "./utils/inject-external-store.mjs";
|
|
2
|
+
import { QueryClient } from "@tanstack/query-core";
|
|
3
|
+
import { assertInInjectionContext, inject } from "@angular/core";
|
|
4
|
+
//#region src/inject-is-mutating.ts
|
|
5
|
+
/**
|
|
6
|
+
* Injects a signal that tracks the number of mutations that your application currently has `pending`
|
|
7
|
+
* (useful for app-wide loading indicators).
|
|
8
|
+
*
|
|
9
|
+
* Can be used for app-wide loading indicators.
|
|
10
|
+
* @param filters - A reactive factory for the filters.
|
|
11
|
+
* @returns A read-only signal with the number of fetching mutations.
|
|
12
|
+
*/
|
|
4
13
|
function injectIsMutating(filters = () => ({})) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
subscribe: (onStoreChange) => cache.subscribe(onStoreChange)
|
|
13
|
-
}));
|
|
14
|
+
if (typeof ngDevMode === "undefined" || ngDevMode) assertInInjectionContext(injectIsMutating);
|
|
15
|
+
const queryClient = inject(QueryClient);
|
|
16
|
+
const cache = queryClient.getMutationCache();
|
|
17
|
+
return injectExternalStore(() => ({
|
|
18
|
+
getSnapshot: () => queryClient.isMutating(filters()),
|
|
19
|
+
subscribe: (onStoreChange) => cache.subscribe(onStoreChange)
|
|
20
|
+
}));
|
|
14
21
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//# sourceMappingURL=inject-is-mutating.mjs.map
|
|
22
|
+
//#endregion
|
|
23
|
+
export { injectIsMutating };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=inject-is-mutating.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject-is-mutating.mjs","sources":["../src/inject-is-mutating.ts"],"sourcesContent":["import { assertInInjectionContext, inject } from '@angular/core'\nimport { QueryClient } from '@tanstack/query-core'\nimport { injectExternalStore } from './utils/inject-external-store'\nimport type { MutationFilters } from '@tanstack/query-core'\nimport type { Signal } from '@angular/core'\n\n/**\n * Injects a signal that tracks the number of mutations that your application currently has `pending`\n * (useful for app-wide loading indicators).\n *\n * Can be used for app-wide loading indicators
|
|
1
|
+
{"version":3,"file":"inject-is-mutating.mjs","names":[],"sources":["../src/inject-is-mutating.ts"],"sourcesContent":["import { assertInInjectionContext, inject } from '@angular/core'\nimport { QueryClient } from '@tanstack/query-core'\nimport { injectExternalStore } from './utils/inject-external-store'\nimport type { MutationFilters } from '@tanstack/query-core'\nimport type { Signal } from '@angular/core'\n\n/**\n * Injects a signal that tracks the number of mutations that your application currently has `pending`\n * (useful for app-wide loading indicators).\n *\n * Can be used for app-wide loading indicators.\n * @param filters - A reactive factory for the filters.\n * @returns A read-only signal with the number of fetching mutations.\n */\nexport function injectIsMutating(\n filters: () => MutationFilters = () => ({}),\n): Signal<number> {\n if (typeof ngDevMode === 'undefined' || ngDevMode) {\n assertInInjectionContext(injectIsMutating)\n }\n const queryClient = inject(QueryClient)\n const cache = queryClient.getMutationCache()\n\n return injectExternalStore(() => ({\n getSnapshot: () => queryClient.isMutating(filters()),\n subscribe: (onStoreChange) => cache.subscribe(onStoreChange),\n }))\n}\n"],"mappings":";;;;;;;;;;;;AAcA,SAAgB,iBACd,iBAAwC,CAAC,IACzB;CAChB,IAAI,OAAO,cAAc,eAAe,WACtC,yBAAyB,gBAAgB;CAE3C,MAAM,cAAc,OAAO,WAAW;CACtC,MAAM,QAAQ,YAAY,iBAAiB;CAE3C,OAAO,2BAA2B;EAChC,mBAAmB,YAAY,WAAW,QAAQ,CAAC;EACnD,YAAY,kBAAkB,MAAM,UAAU,aAAa;CAC7D,EAAE;AACJ"}
|
package/inject-is-restoring.d.ts
CHANGED
|
@@ -9,5 +9,7 @@ export declare function injectIsRestoring(): Signal<boolean>;
|
|
|
9
9
|
* Supplies injector-scoped restoration state to the Angular Query integrations.
|
|
10
10
|
* The factory runs once in Angular's injection context.
|
|
11
11
|
* @internal
|
|
12
|
+
* @param isRestoringFactory - Produces the restoration signal for this injector.
|
|
13
|
+
* @returns The provider that makes the restoration state injectable.
|
|
12
14
|
*/
|
|
13
15
|
export declare function provideIsRestoring(isRestoringFactory: () => Signal<boolean>): Provider;
|
package/inject-is-restoring.mjs
CHANGED
|
@@ -1,21 +1,32 @@
|
|
|
1
|
-
import { assertInInjectionContext, inject,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { InjectionToken, assertInInjectionContext, inject, signal } from "@angular/core";
|
|
2
|
+
//#region src/inject-is-restoring.ts
|
|
3
|
+
/**
|
|
4
|
+
* Internal token used to track isRestoring state, accessible in public API through `injectIsRestoring` and set via `provideIsRestoring`
|
|
5
|
+
*/
|
|
6
|
+
var IS_RESTORING = new InjectionToken("", { factory: () => signal(false).asReadonly() });
|
|
7
|
+
/**
|
|
8
|
+
* Injects a readonly signal that is true while the persistence integration restores
|
|
9
|
+
* cached query data. Returns false when no restoration is in progress.
|
|
10
|
+
* @returns The current restoration state.
|
|
11
|
+
*/
|
|
5
12
|
function injectIsRestoring() {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
return inject(IS_RESTORING);
|
|
13
|
+
if (typeof ngDevMode === "undefined" || ngDevMode) assertInInjectionContext(injectIsRestoring);
|
|
14
|
+
return inject(IS_RESTORING);
|
|
10
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Supplies injector-scoped restoration state to the Angular Query integrations.
|
|
18
|
+
* The factory runs once in Angular's injection context.
|
|
19
|
+
* @internal
|
|
20
|
+
* @param isRestoringFactory - Produces the restoration signal for this injector.
|
|
21
|
+
* @returns The provider that makes the restoration state injectable.
|
|
22
|
+
*/
|
|
11
23
|
function provideIsRestoring(isRestoringFactory) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
24
|
+
return {
|
|
25
|
+
provide: IS_RESTORING,
|
|
26
|
+
useFactory: isRestoringFactory
|
|
27
|
+
};
|
|
16
28
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
//# sourceMappingURL=inject-is-restoring.mjs.map
|
|
29
|
+
//#endregion
|
|
30
|
+
export { injectIsRestoring, provideIsRestoring };
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=inject-is-restoring.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject-is-restoring.mjs","sources":["../src/inject-is-restoring.ts"],"sourcesContent":["import {\n InjectionToken,\n assertInInjectionContext,\n inject,\n signal,\n} from '@angular/core'\nimport type { Provider, Signal } from '@angular/core'\n\n/**\n * Internal token used to track isRestoring state, accessible in public API through `injectIsRestoring` and set via `provideIsRestoring`\n */\nconst IS_RESTORING = new InjectionToken('', {\n factory: () => signal(false).asReadonly(),\n})\n\n/**\n * Injects a readonly signal that is true while the persistence integration restores\n * cached query data. Returns false when no restoration is in progress.\n * @returns The current restoration state.\n */\nexport function injectIsRestoring() {\n if (typeof ngDevMode === 'undefined' || ngDevMode) {\n assertInInjectionContext(injectIsRestoring)\n }\n return inject(IS_RESTORING)\n}\n\n/**\n * Supplies injector-scoped restoration state to the Angular Query integrations.\n * The factory runs once in Angular's injection context.\n * @internal\n */\nexport function provideIsRestoring(\n isRestoringFactory: () => Signal<boolean>,\n): Provider {\n return {\n provide: IS_RESTORING,\n useFactory: isRestoringFactory,\n }\n}\n"],"
|
|
1
|
+
{"version":3,"file":"inject-is-restoring.mjs","names":[],"sources":["../src/inject-is-restoring.ts"],"sourcesContent":["import {\n InjectionToken,\n assertInInjectionContext,\n inject,\n signal,\n} from '@angular/core'\nimport type { Provider, Signal } from '@angular/core'\n\n/**\n * Internal token used to track isRestoring state, accessible in public API through `injectIsRestoring` and set via `provideIsRestoring`\n */\nconst IS_RESTORING = new InjectionToken('', {\n factory: () => signal(false).asReadonly(),\n})\n\n/**\n * Injects a readonly signal that is true while the persistence integration restores\n * cached query data. Returns false when no restoration is in progress.\n * @returns The current restoration state.\n */\nexport function injectIsRestoring() {\n if (typeof ngDevMode === 'undefined' || ngDevMode) {\n assertInInjectionContext(injectIsRestoring)\n }\n return inject(IS_RESTORING)\n}\n\n/**\n * Supplies injector-scoped restoration state to the Angular Query integrations.\n * The factory runs once in Angular's injection context.\n * @internal\n * @param isRestoringFactory - Produces the restoration signal for this injector.\n * @returns The provider that makes the restoration state injectable.\n */\nexport function provideIsRestoring(\n isRestoringFactory: () => Signal<boolean>,\n): Provider {\n return {\n provide: IS_RESTORING,\n useFactory: isRestoringFactory,\n }\n}\n"],"mappings":";;;;;AAWA,IAAM,eAAe,IAAI,eAAe,IAAI,EAC1C,eAAe,OAAO,KAAK,CAAC,CAAC,WAAW,EAC1C,CAAC;;;;;;AAOD,SAAgB,oBAAoB;CAClC,IAAI,OAAO,cAAc,eAAe,WACtC,yBAAyB,iBAAiB;CAE5C,OAAO,OAAO,YAAY;AAC5B;;;;;;;;AASA,SAAgB,mBACd,oBACU;CACV,OAAO;EACL,SAAS;EACT,YAAY;CACd;AACF"}
|
|
@@ -3,8 +3,17 @@ import { Mutation, MutationFilters, MutationState } from '@tanstack/query-core';
|
|
|
3
3
|
type MutationTypeFromResult<TResult> = [TResult] extends [
|
|
4
4
|
MutationState<infer TData, infer TError, infer TVariables, infer TOnMutateResult>
|
|
5
5
|
] ? Mutation<TData, TError, TVariables, TOnMutateResult> : Mutation;
|
|
6
|
+
/**
|
|
7
|
+
* Filters and optional projection for observing entries in the mutation cache.
|
|
8
|
+
*/
|
|
6
9
|
export type MutationStateOptions<TResult = MutationState, TMutation extends Mutation<any, any, any, any> = MutationTypeFromResult<TResult>> = {
|
|
10
|
+
/**
|
|
11
|
+
* Restricts the mutation cache entries included in the result.
|
|
12
|
+
*/
|
|
7
13
|
filters?: MutationFilters;
|
|
14
|
+
/**
|
|
15
|
+
* Maps each matching mutation to the value exposed in the result signal.
|
|
16
|
+
*/
|
|
8
17
|
select?: (mutation: TMutation) => TResult;
|
|
9
18
|
};
|
|
10
19
|
/**
|
|
@@ -1,28 +1,30 @@
|
|
|
1
|
-
import { assertInInjectionContext, inject } from "@angular/core";
|
|
2
|
-
import { QueryClient } from "@tanstack/query-core";
|
|
3
1
|
import { injectExternalStore } from "./utils/inject-external-store.mjs";
|
|
2
|
+
import { QueryClient } from "@tanstack/query-core";
|
|
3
|
+
import { assertInInjectionContext, inject } from "@angular/core";
|
|
4
|
+
//#region src/inject-mutation-state.ts
|
|
5
|
+
/**
|
|
6
|
+
* Reads and projects the matching entries in a mutation cache.
|
|
7
|
+
* @param mutationCache - The cache to inspect.
|
|
8
|
+
* @param options - Filters and an optional projection for each entry.
|
|
9
|
+
* @returns The selected values, or each matching mutation state.
|
|
10
|
+
*/
|
|
4
11
|
function getResult(mutationCache, options) {
|
|
5
|
-
|
|
6
|
-
(mutation) => options.select ? options.select(mutation) : mutation.state
|
|
7
|
-
);
|
|
12
|
+
return mutationCache.findAll(options.filters).map((mutation) => options.select ? options.select(mutation) : mutation.state);
|
|
8
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Injects a signal that tracks the state of all mutations.
|
|
16
|
+
* @param options - A function that returns mutation state options.
|
|
17
|
+
* @returns The signal that tracks the state of all mutations.
|
|
18
|
+
*/
|
|
9
19
|
function injectMutationState(options = () => ({})) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
() => ({
|
|
17
|
-
getSnapshot: () => getResult(mutationCache, options()),
|
|
18
|
-
subscribe: (onStoreChange) => mutationCache.subscribe(onStoreChange)
|
|
19
|
-
}),
|
|
20
|
-
{
|
|
21
|
-
equal: (previous, next) => previous.length === next.length && previous.every((value, index) => Object.is(value, next[index]))
|
|
22
|
-
}
|
|
23
|
-
);
|
|
20
|
+
if (typeof ngDevMode === "undefined" || ngDevMode) assertInInjectionContext(injectMutationState);
|
|
21
|
+
const mutationCache = inject(QueryClient).getMutationCache();
|
|
22
|
+
return injectExternalStore(() => ({
|
|
23
|
+
getSnapshot: () => getResult(mutationCache, options()),
|
|
24
|
+
subscribe: (onStoreChange) => mutationCache.subscribe(onStoreChange)
|
|
25
|
+
}), { equal: (previous, next) => previous.length === next.length && previous.every((value, index) => Object.is(value, next[index])) });
|
|
24
26
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
//# sourceMappingURL=inject-mutation-state.mjs.map
|
|
27
|
+
//#endregion
|
|
28
|
+
export { injectMutationState };
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=inject-mutation-state.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject-mutation-state.mjs","sources":["../src/inject-mutation-state.ts"],"sourcesContent":["import { assertInInjectionContext, inject } from '@angular/core'\nimport { QueryClient } from '@tanstack/query-core'\nimport { injectExternalStore } from './utils/inject-external-store'\nimport type { Signal } from '@angular/core'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n} from '@tanstack/query-core'\n\ntype MutationTypeFromResult<TResult> = [TResult] extends [\n MutationState<\n infer TData,\n infer TError,\n infer TVariables,\n infer TOnMutateResult\n >,\n]\n ? Mutation<TData, TError, TVariables, TOnMutateResult>\n : Mutation\n\nexport type MutationStateOptions<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n> = {\n filters?: MutationFilters\n select?: (mutation: TMutation) => TResult\n}\n\nfunction getResult<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n mutationCache: MutationCache,\n options: MutationStateOptions<TResult, TMutation>,\n): Array<TResult> {\n return mutationCache\n .findAll(options.filters)\n .map(\n (mutation): TResult =>\n (options.select\n ? options.select(mutation as TMutation)\n : mutation.state) as TResult,\n )\n}\n\n/**\n * Injects a signal that tracks the state of all mutations.\n * @param options - A function that returns mutation state options.\n * @returns The signal that tracks the state of all mutations.\n */\nexport function injectMutationState<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n options: () => MutationStateOptions<TResult, TMutation> = () => ({}),\n): Signal<Array<TResult>> {\n if (typeof ngDevMode === 'undefined' || ngDevMode) {\n assertInInjectionContext(injectMutationState)\n }\n const queryClient = inject(QueryClient)\n const mutationCache = queryClient.getMutationCache()\n\n return injectExternalStore(\n () => ({\n getSnapshot: () => getResult(mutationCache, options()),\n subscribe: (onStoreChange) => mutationCache.subscribe(onStoreChange),\n }),\n {\n equal: (previous, next) =>\n previous.length === next.length &&\n previous.every((value, index) => Object.is(value, next[index])),\n },\n )\n}\n"],"
|
|
1
|
+
{"version":3,"file":"inject-mutation-state.mjs","names":[],"sources":["../src/inject-mutation-state.ts"],"sourcesContent":["import { assertInInjectionContext, inject } from '@angular/core'\nimport { QueryClient } from '@tanstack/query-core'\nimport { injectExternalStore } from './utils/inject-external-store'\nimport type { Signal } from '@angular/core'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n} from '@tanstack/query-core'\n\ntype MutationTypeFromResult<TResult> = [TResult] extends [\n MutationState<\n infer TData,\n infer TError,\n infer TVariables,\n infer TOnMutateResult\n >,\n]\n ? Mutation<TData, TError, TVariables, TOnMutateResult>\n : Mutation\n\n/**\n * Filters and optional projection for observing entries in the mutation cache.\n */\nexport type MutationStateOptions<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n> = {\n /**\n * Restricts the mutation cache entries included in the result.\n */\n filters?: MutationFilters\n /**\n * Maps each matching mutation to the value exposed in the result signal.\n */\n select?: (mutation: TMutation) => TResult\n}\n\n/**\n * Reads and projects the matching entries in a mutation cache.\n * @param mutationCache - The cache to inspect.\n * @param options - Filters and an optional projection for each entry.\n * @returns The selected values, or each matching mutation state.\n */\nfunction getResult<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n mutationCache: MutationCache,\n options: MutationStateOptions<TResult, TMutation>,\n): Array<TResult> {\n return mutationCache\n .findAll(options.filters)\n .map(\n (mutation): TResult =>\n (options.select\n ? options.select(mutation as TMutation)\n : mutation.state) as TResult,\n )\n}\n\n/**\n * Injects a signal that tracks the state of all mutations.\n * @param options - A function that returns mutation state options.\n * @returns The signal that tracks the state of all mutations.\n */\nexport function injectMutationState<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n options: () => MutationStateOptions<TResult, TMutation> = () => ({}),\n): Signal<Array<TResult>> {\n if (typeof ngDevMode === 'undefined' || ngDevMode) {\n assertInInjectionContext(injectMutationState)\n }\n const queryClient = inject(QueryClient)\n const mutationCache = queryClient.getMutationCache()\n\n return injectExternalStore(\n () => ({\n getSnapshot: () => getResult(mutationCache, options()),\n subscribe: (onStoreChange) => mutationCache.subscribe(onStoreChange),\n }),\n {\n equal: (previous, next) =>\n previous.length === next.length &&\n previous.every((value, index) => Object.is(value, next[index])),\n },\n )\n}\n"],"mappings":";;;;;;;;;;AA8CA,SAAS,UAKP,eACA,SACgB;CAChB,OAAO,cACJ,QAAQ,QAAQ,OAAO,CAAC,CACxB,KACE,aACE,QAAQ,SACL,QAAQ,OAAO,QAAqB,IACpC,SAAS,KACjB;AACJ;;;;;;AAOA,SAAgB,oBAKd,iBAAiE,CAAC,IAC1C;CACxB,IAAI,OAAO,cAAc,eAAe,WACtC,yBAAyB,mBAAmB;CAG9C,MAAM,gBADc,OAAO,WACL,CAAA,CAAY,iBAAiB;CAEnD,OAAO,2BACE;EACL,mBAAmB,UAAU,eAAe,QAAQ,CAAC;EACrD,YAAY,kBAAkB,cAAc,UAAU,aAAa;CACrE,IACA,EACE,QAAQ,UAAU,SAChB,SAAS,WAAW,KAAK,UACzB,SAAS,OAAO,OAAO,UAAU,OAAO,GAAG,OAAO,KAAK,MAAM,CAAC,EAClE,CACF;AACF"}
|
package/inject-mutation.d.ts
CHANGED
|
@@ -3,13 +3,11 @@ import { CreateMutationOptions, CreateMutationResult } from './types.js';
|
|
|
3
3
|
/**
|
|
4
4
|
* Injects a mutation: an imperative function that can be invoked which typically performs server side effects.
|
|
5
5
|
* Unlike queries, mutations are not run automatically.
|
|
6
|
-
*
|
|
7
|
-
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/mutations
|
|
8
|
-
* @see {@link mutationOptions} to share these options between `injectMutation` and `injectMutationState`.
|
|
9
6
|
* @param optionsFn - A function that returns mutation options. Similar to `computed` from Angular, this
|
|
10
7
|
* function runs in the reactive context, so signals read inside it drive the mutation.
|
|
11
8
|
* @returns The mutation result, including `mutate` and `mutateAsync`.
|
|
12
|
-
*
|
|
9
|
+
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/mutations
|
|
10
|
+
* @see {@link mutationOptions} to share these options between `injectMutation` and `injectMutationState`.
|
|
13
11
|
* @example
|
|
14
12
|
* ```angular-ts
|
|
15
13
|
* @Component({
|
package/inject-mutation.mjs
CHANGED
|
@@ -1,72 +1,92 @@
|
|
|
1
|
-
import { assertInInjectionContext, inject, computed, untracked, effect } from "@angular/core";
|
|
2
|
-
import { QueryClient, MutationObserver, noop } from "@tanstack/query-core";
|
|
3
1
|
import { injectQueryZone } from "./utils/inject-query-zone.mjs";
|
|
4
|
-
import { signalProxy } from "./utils/signal-proxy.mjs";
|
|
5
|
-
import { mutationResultFields } from "./utils/result-fields.mjs";
|
|
6
2
|
import { injectPendingTasksLifecycle } from "./utils/inject-pending-tasks-lifecycle.mjs";
|
|
7
3
|
import { injectExternalStore } from "./utils/inject-external-store.mjs";
|
|
4
|
+
import { signalProxy } from "./utils/signal-proxy.mjs";
|
|
5
|
+
import { mutationResultFields } from "./utils/result-fields.mjs";
|
|
6
|
+
import { MutationObserver, QueryClient, noop } from "@tanstack/query-core";
|
|
7
|
+
import { assertInInjectionContext, computed, effect, inject, untracked } from "@angular/core";
|
|
8
|
+
//#region src/inject-mutation.ts
|
|
9
|
+
/**
|
|
10
|
+
* Injects a mutation: an imperative function that can be invoked which typically performs server side effects.
|
|
11
|
+
* Unlike queries, mutations are not run automatically.
|
|
12
|
+
* @param optionsFn - A function that returns mutation options. Similar to `computed` from Angular, this
|
|
13
|
+
* function runs in the reactive context, so signals read inside it drive the mutation.
|
|
14
|
+
* @returns The mutation result, including `mutate` and `mutateAsync`.
|
|
15
|
+
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/mutations
|
|
16
|
+
* @see {@link mutationOptions} to share these options between `injectMutation` and `injectMutationState`.
|
|
17
|
+
* @example
|
|
18
|
+
* ```angular-ts
|
|
19
|
+
* @Component({
|
|
20
|
+
* template: `
|
|
21
|
+
* @if (mutation.isPending()) {
|
|
22
|
+
* Saving...
|
|
23
|
+
* } @else if (mutation.isError()) {
|
|
24
|
+
* <span>Error: {{ mutation.error()?.message }}</span>
|
|
25
|
+
* }
|
|
26
|
+
* <button (click)="mutation.mutate({ title: 'New post' })">Create</button>
|
|
27
|
+
* `,
|
|
28
|
+
* })
|
|
29
|
+
* export class CreatePost {
|
|
30
|
+
* readonly mutation = injectMutation(() => ({
|
|
31
|
+
* mutationFn: createPost,
|
|
32
|
+
* }))
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
8
36
|
function injectMutation(optionsFn) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
})
|
|
61
|
-
);
|
|
62
|
-
};
|
|
63
|
-
return Object.assign(signalProxy(mutationStateSignal, mutationResultFields), {
|
|
64
|
-
mutate,
|
|
65
|
-
mutateAsync,
|
|
66
|
-
reset
|
|
67
|
-
});
|
|
37
|
+
if (typeof ngDevMode === "undefined" || ngDevMode) assertInInjectionContext(injectMutation);
|
|
38
|
+
const outsideZone = injectQueryZone();
|
|
39
|
+
const queryClient = inject(QueryClient);
|
|
40
|
+
const lifecycle = injectPendingTasksLifecycle();
|
|
41
|
+
const optionsSignal = computed(optionsFn);
|
|
42
|
+
const observerSignal = computed(() => new MutationObserver(queryClient, untracked(optionsSignal)));
|
|
43
|
+
effect(() => {
|
|
44
|
+
const options = optionsSignal();
|
|
45
|
+
outsideZone(() => untracked(() => observerSignal().setOptions(options)));
|
|
46
|
+
});
|
|
47
|
+
const mutationStateSignal = injectExternalStore(() => {
|
|
48
|
+
const observer = observerSignal();
|
|
49
|
+
return {
|
|
50
|
+
getSnapshot: () => observer.getCurrentResult(),
|
|
51
|
+
subscribe: (onStoreChange) => observer.subscribe(onStoreChange)
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
const mutate = (...args) => {
|
|
55
|
+
mutateAsync(...args).catch(noop);
|
|
56
|
+
};
|
|
57
|
+
let pendingInvocations = 0;
|
|
58
|
+
const mutateAsync = (...args) => {
|
|
59
|
+
return outsideZone(() => untracked(() => {
|
|
60
|
+
const observer = observerSignal();
|
|
61
|
+
observer.setOptions(optionsSignal());
|
|
62
|
+
pendingInvocations++;
|
|
63
|
+
lifecycle.setPending(true);
|
|
64
|
+
const settled = () => {
|
|
65
|
+
pendingInvocations--;
|
|
66
|
+
lifecycle.setPending(pendingInvocations > 0);
|
|
67
|
+
};
|
|
68
|
+
try {
|
|
69
|
+
return observer.mutate(args[0], args[1]).finally(settled);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
settled();
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
}));
|
|
75
|
+
};
|
|
76
|
+
const reset = () => {
|
|
77
|
+
outsideZone(() => untracked(() => {
|
|
78
|
+
const observer = observerSignal();
|
|
79
|
+
observer.setOptions(optionsSignal());
|
|
80
|
+
observer.reset();
|
|
81
|
+
}));
|
|
82
|
+
};
|
|
83
|
+
return Object.assign(signalProxy(mutationStateSignal, mutationResultFields), {
|
|
84
|
+
mutate,
|
|
85
|
+
mutateAsync,
|
|
86
|
+
reset
|
|
87
|
+
});
|
|
68
88
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
//# sourceMappingURL=inject-mutation.mjs.map
|
|
89
|
+
//#endregion
|
|
90
|
+
export { injectMutation };
|
|
91
|
+
|
|
92
|
+
//# sourceMappingURL=inject-mutation.mjs.map
|
package/inject-mutation.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject-mutation.mjs","sources":["../src/inject-mutation.ts"],"sourcesContent":["import {\n assertInInjectionContext,\n computed,\n effect,\n inject,\n untracked,\n} from '@angular/core'\nimport { MutationObserver, QueryClient, noop } from '@tanstack/query-core'\nimport { injectQueryZone } from './utils/inject-query-zone'\nimport { signalProxy } from './utils/signal-proxy'\nimport { mutationResultFields } from './utils/result-fields'\nimport { injectPendingTasksLifecycle } from './utils/inject-pending-tasks-lifecycle'\nimport { injectExternalStore } from './utils/inject-external-store'\nimport type { DefaultError } from '@tanstack/query-core'\nimport type {\n CreateMutateAsyncFunction,\n CreateMutateFunction,\n CreateMutationOptions,\n CreateMutationResult,\n} from './types'\n\n/**\n * Injects a mutation: an imperative function that can be invoked which typically performs server side effects.\n * Unlike queries, mutations are not run automatically.\n
|
|
1
|
+
{"version":3,"file":"inject-mutation.mjs","names":[],"sources":["../src/inject-mutation.ts"],"sourcesContent":["import {\n assertInInjectionContext,\n computed,\n effect,\n inject,\n untracked,\n} from '@angular/core'\nimport { MutationObserver, QueryClient, noop } from '@tanstack/query-core'\nimport { injectQueryZone } from './utils/inject-query-zone'\nimport { signalProxy } from './utils/signal-proxy'\nimport { mutationResultFields } from './utils/result-fields'\nimport { injectPendingTasksLifecycle } from './utils/inject-pending-tasks-lifecycle'\nimport { injectExternalStore } from './utils/inject-external-store'\nimport type { DefaultError } from '@tanstack/query-core'\nimport type {\n CreateMutateAsyncFunction,\n CreateMutateFunction,\n CreateMutationOptions,\n CreateMutationResult,\n} from './types'\n\n/**\n * Injects a mutation: an imperative function that can be invoked which typically performs server side effects.\n * Unlike queries, mutations are not run automatically.\n * @param optionsFn - A function that returns mutation options. Similar to `computed` from Angular, this\n * function runs in the reactive context, so signals read inside it drive the mutation.\n * @returns The mutation result, including `mutate` and `mutateAsync`.\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/mutations\n * @see {@link mutationOptions} to share these options between `injectMutation` and `injectMutationState`.\n * @example\n * ```angular-ts\n * @Component({\n * template: `\n * @if (mutation.isPending()) {\n * Saving...\n * } @else if (mutation.isError()) {\n * <span>Error: {{ mutation.error()?.message }}</span>\n * }\n * <button (click)=\"mutation.mutate({ title: 'New post' })\">Create</button>\n * `,\n * })\n * export class CreatePost {\n * readonly mutation = injectMutation(() => ({\n * mutationFn: createPost,\n * }))\n * }\n * ```\n */\nexport function injectMutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n>(\n optionsFn: () => CreateMutationOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n): CreateMutationResult<TData, TError, TVariables, TOnMutateResult> {\n if (typeof ngDevMode === 'undefined' || ngDevMode) {\n assertInInjectionContext(injectMutation)\n }\n const outsideZone = injectQueryZone()\n const queryClient = inject(QueryClient)\n const lifecycle = injectPendingTasksLifecycle()\n\n const optionsSignal = computed(optionsFn)\n\n const observerSignal = computed(\n () => new MutationObserver(queryClient, untracked(optionsSignal)),\n )\n\n // Configure the observer outside the result computation. Mutation-cache\n // listeners can synchronously read the public result while setOptions emits.\n effect(() => {\n const options = optionsSignal()\n outsideZone(() => untracked(() => observerSignal().setOptions(options)))\n })\n\n const mutationStateSignal = injectExternalStore(() => {\n const observer = observerSignal()\n return {\n getSnapshot: () => observer.getCurrentResult(),\n subscribe: (onStoreChange) => observer.subscribe(onStoreChange),\n }\n })\n\n const mutate: CreateMutateFunction<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > = (...args) => {\n mutateAsync(...args).catch(noop)\n }\n\n let pendingInvocations = 0\n\n const mutateAsync: CreateMutateAsyncFunction<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > = (...args) => {\n return outsideZone(() =>\n untracked(() => {\n const observer = observerSignal()\n observer.setOptions(optionsSignal())\n pendingInvocations++\n lifecycle.setPending(true)\n // Track invocations, not the observer's latest result: reset or a later\n // mutation completing must not release work which is still running.\n const settled = () => {\n pendingInvocations--\n lifecycle.setPending(pendingInvocations > 0)\n }\n try {\n return observer\n .mutate(args[0] as TVariables, args[1])\n .finally(settled)\n } catch (error) {\n settled()\n throw error\n }\n }),\n )\n }\n\n const reset = () => {\n outsideZone(() =>\n untracked(() => {\n const observer = observerSignal()\n observer.setOptions(optionsSignal())\n observer.reset()\n }),\n )\n }\n\n return Object.assign(signalProxy(mutationStateSignal, mutationResultFields), {\n mutate,\n mutateAsync,\n reset,\n }) as unknown as CreateMutationResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,eAMd,WAMkE;CAClE,IAAI,OAAO,cAAc,eAAe,WACtC,yBAAyB,cAAc;CAEzC,MAAM,cAAc,gBAAgB;CACpC,MAAM,cAAc,OAAO,WAAW;CACtC,MAAM,YAAY,4BAA4B;CAE9C,MAAM,gBAAgB,SAAS,SAAS;CAExC,MAAM,iBAAiB,eACf,IAAI,iBAAiB,aAAa,UAAU,aAAa,CAAC,CAClE;CAIA,aAAa;EACX,MAAM,UAAU,cAAc;EAC9B,kBAAkB,gBAAgB,eAAe,CAAC,CAAC,WAAW,OAAO,CAAC,CAAC;CACzE,CAAC;CAED,MAAM,sBAAsB,0BAA0B;EACpD,MAAM,WAAW,eAAe;EAChC,OAAO;GACL,mBAAmB,SAAS,iBAAiB;GAC7C,YAAY,kBAAkB,SAAS,UAAU,aAAa;EAChE;CACF,CAAC;CAED,MAAM,UAKD,GAAG,SAAS;EACf,YAAY,GAAG,IAAI,CAAC,CAAC,MAAM,IAAI;CACjC;CAEA,IAAI,qBAAqB;CAEzB,MAAM,eAKD,GAAG,SAAS;EACf,OAAO,kBACL,gBAAgB;GACd,MAAM,WAAW,eAAe;GAChC,SAAS,WAAW,cAAc,CAAC;GACnC;GACA,UAAU,WAAW,IAAI;GAGzB,MAAM,gBAAgB;IACpB;IACA,UAAU,WAAW,qBAAqB,CAAC;GAC7C;GACA,IAAI;IACF,OAAO,SACJ,OAAO,KAAK,IAAkB,KAAK,EAAE,CAAC,CACtC,QAAQ,OAAO;GACpB,SAAS,OAAO;IACd,QAAQ;IACR,MAAM;GACR;EACF,CAAC,CACH;CACF;CAEA,MAAM,cAAc;EAClB,kBACE,gBAAgB;GACd,MAAM,WAAW,eAAe;GAChC,SAAS,WAAW,cAAc,CAAC;GACnC,SAAS,MAAM;EACjB,CAAC,CACH;CACF;CAEA,OAAO,OAAO,OAAO,YAAY,qBAAqB,oBAAoB,GAAG;EAC3E;EACA;EACA;CACF,CAAC;AAMH"}
|
package/inject-queries.d.ts
CHANGED
|
@@ -2,12 +2,10 @@ import { InjectQueriesOptions, QueriesResults } from './inject-queries.types.js'
|
|
|
2
2
|
import { Signal } from '@angular/core';
|
|
3
3
|
/**
|
|
4
4
|
* Injects multiple queries that run in parallel and react to Angular signals.
|
|
5
|
-
*
|
|
6
|
-
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/parallel-queries
|
|
7
5
|
* @param optionsFn - A function that returns the queries' options. Similar to `computed` from Angular,
|
|
8
6
|
* this function runs in the reactive context, so signals read inside it drive the queries.
|
|
9
7
|
* @returns A signal containing the query results in the same order as the input queries.
|
|
10
|
-
*
|
|
8
|
+
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/parallel-queries
|
|
11
9
|
* @example
|
|
12
10
|
* ```angular-ts
|
|
13
11
|
* @Component({
|