@tanstack/react-query 5.104.4 → 5.104.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/build/legacy/useMutationState.cjs +23 -20
- package/build/legacy/useMutationState.cjs.map +1 -1
- package/build/legacy/useMutationState.d.cts.map +1 -1
- package/build/legacy/useMutationState.d.ts.map +1 -1
- package/build/legacy/useMutationState.js +23 -20
- package/build/legacy/useMutationState.js.map +1 -1
- package/build/modern/useMutationState.cjs +23 -20
- package/build/modern/useMutationState.cjs.map +1 -1
- package/build/modern/useMutationState.d.cts.map +1 -1
- package/build/modern/useMutationState.d.ts.map +1 -1
- package/build/modern/useMutationState.js +23 -20
- package/build/modern/useMutationState.js.map +1 -1
- package/package.json +3 -3
- package/src/useMutationState.ts +51 -40
|
@@ -33,14 +33,27 @@ function useIsMutating(filters, queryClient) {
|
|
|
33
33
|
} }, client).length;
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @
|
|
40
|
-
* @returns The selected value of every matching mutation.
|
|
36
|
+
* Creates snapshot getters that share equal selected results across renders.
|
|
37
|
+
* A new getter is created on each render. Each getter caches its snapshot until the matching mutations
|
|
38
|
+
* or their states change.
|
|
39
|
+
* @returns A factory for snapshot getters with a shared result cache.
|
|
41
40
|
*/
|
|
42
|
-
function
|
|
43
|
-
|
|
41
|
+
function createSnapshotFactory() {
|
|
42
|
+
let previousResult;
|
|
43
|
+
return (mutationCache, options) => {
|
|
44
|
+
let inputs = [];
|
|
45
|
+
let snapshot;
|
|
46
|
+
return () => {
|
|
47
|
+
const mutations = mutationCache.findAll(options.filters);
|
|
48
|
+
const nextInputs = mutations.flatMap((mutation) => [mutation, mutation.state]);
|
|
49
|
+
if (!snapshot || nextInputs.length !== inputs.length || nextInputs.some((input, index) => !Object.is(input, inputs[index]))) {
|
|
50
|
+
inputs = nextInputs;
|
|
51
|
+
snapshot = (0, _tanstack_query_core.replaceEqualDeep)(previousResult, mutations.map((mutation) => options.select ? options.select(mutation) : mutation.state));
|
|
52
|
+
previousResult = snapshot;
|
|
53
|
+
}
|
|
54
|
+
return snapshot;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
44
57
|
}
|
|
45
58
|
/**
|
|
46
59
|
* `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass
|
|
@@ -113,19 +126,9 @@ function getResult(mutationCache, options) {
|
|
|
113
126
|
*/
|
|
114
127
|
function useMutationState(options = {}, queryClient) {
|
|
115
128
|
const mutationCache = require_QueryClientProvider.useQueryClient(queryClient).getMutationCache();
|
|
116
|
-
const
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
react.useEffect(() => {
|
|
120
|
-
optionsRef.current = options;
|
|
121
|
-
});
|
|
122
|
-
return react.useSyncExternalStore(react.useCallback((onStoreChange) => mutationCache.subscribe(() => {
|
|
123
|
-
const nextResult = (0, _tanstack_query_core.replaceEqualDeep)(resultRef.current, getResult(mutationCache, optionsRef.current));
|
|
124
|
-
if (resultRef.current !== nextResult) {
|
|
125
|
-
resultRef.current = nextResult;
|
|
126
|
-
_tanstack_query_core.notifyManager.schedule(onStoreChange);
|
|
127
|
-
}
|
|
128
|
-
}), [mutationCache]), () => resultRef.current, () => resultRef.current);
|
|
129
|
+
const [createSnapshot] = react.useState(() => createSnapshotFactory());
|
|
130
|
+
const getSnapshot = createSnapshot(mutationCache, options);
|
|
131
|
+
return react.useSyncExternalStore(react.useCallback((onStoreChange) => mutationCache.subscribe(_tanstack_query_core.notifyManager.batchCalls(onStoreChange)), [mutationCache]), getSnapshot, getSnapshot);
|
|
129
132
|
}
|
|
130
133
|
//#endregion
|
|
131
134
|
exports.useIsMutating = useIsMutating;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.cjs","names":["useQueryClient","React","
|
|
1
|
+
{"version":3,"file":"useMutationState.cjs","names":["useQueryClient","replaceEqualDeep","React","notifyManager"],"sources":["../../src/useMutationState.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\n\nimport { notifyManager, replaceEqualDeep } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n QueryClient,\n} from '@tanstack/query-core'\n\n/**\n * The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`\n * (useful for app-wide loading indicators).\n * @param filters - The {@link MutationFilters} to narrow down the matched mutations.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be the `number` of the mutations that your application currently has `pending`.\n * @example\n * ```tsx\n * import { useIsMutating } from '@tanstack/react-query'\n *\n * function PostsMutatingIndicator() {\n * // How many mutations matching the posts prefix are in progress?\n * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })\n *\n * return isMutatingPosts ? <span>Saving posts...</span> : null\n * }\n * ```\n */\nexport function useIsMutating(\n filters?: MutationFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n return useMutationState(\n { filters: { ...filters, status: 'pending' } },\n client,\n ).length\n}\n\ntype MutationTypeFromResult<TResult> = [TResult] extends [\n MutationState<\n infer TData,\n infer TError,\n infer TVariables,\n infer TOnMutateResult\n >,\n]\n ? Mutation<TData, TError, TVariables, TOnMutateResult>\n : Mutation\n\ntype MutationStateOptions<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n> = {\n filters?: MutationFilters\n select?: (mutation: TMutation) => TResult\n}\n\n/**\n * Creates snapshot getters that share equal selected results across renders.\n * A new getter is created on each render. Each getter caches its snapshot until the matching mutations\n * or their states change.\n * @returns A factory for snapshot getters with a shared result cache.\n */\nfunction createSnapshotFactory<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>() {\n let previousResult: Array<TResult> | undefined\n\n return (\n mutationCache: MutationCache,\n options: MutationStateOptions<TResult, TMutation>,\n ) => {\n let inputs: Array<unknown> = []\n let snapshot: Array<TResult> | undefined\n\n return (): Array<TResult> => {\n const mutations = mutationCache.findAll(options.filters)\n const nextInputs = mutations.flatMap((mutation) => [\n mutation,\n mutation.state,\n ])\n\n if (\n !snapshot ||\n nextInputs.length !== inputs.length ||\n nextInputs.some((input, index) => !Object.is(input, inputs[index]))\n ) {\n inputs = nextInputs\n snapshot = replaceEqualDeep(\n previousResult,\n mutations.map(\n (mutation): TResult =>\n (options.select\n ? options.select(mutation as TMutation)\n : mutation.state) as TResult,\n ),\n )\n previousResult = snapshot\n }\n\n return snapshot\n }\n }\n}\n\n/**\n * `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass\n * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation\n * state.\n * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the\n * mutation state.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be an Array of whatever `select` returns for each matching mutation.\n * @example\n * Get all variables of all running mutations:\n * ```tsx\n * import { useMutationState } from '@tanstack/react-query'\n *\n * function PendingPosts() {\n * const pendingVariables = useMutationState({\n * filters: { status: 'pending' },\n * select: (mutation) => mutation.state.variables,\n * })\n *\n * return <>{pendingVariables.length} posts saving...</>\n * }\n * ```\n * @example\n * Get all data for specific mutations via the `mutationKey`:\n * ```tsx\n * import { useMutation, useMutationState } from '@tanstack/react-query'\n *\n * const mutationKey = ['posts']\n *\n * function Posts() {\n * // Some mutation that we want to get the state for\n * const mutation = useMutation({\n * mutationKey,\n * mutationFn: createPosts,\n * })\n *\n * const savedPosts = useMutationState({\n * // this mutation key needs to match the mutation key of the given mutation (see above)\n * filters: { mutationKey, status: 'success' },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * return (\n * <button onClick={() => mutation.mutate(['New Post'])}>\n * Create post ({savedPosts.length} saved so far)\n * </button>\n * )\n * }\n * ```\n * @example\n * Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new\n * entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the\n * last item that `useMutationState` returns to get the latest successful invocation:\n * ```tsx\n * import { useMutationState } from '@tanstack/react-query'\n *\n * function LatestPost() {\n * const savedPosts = useMutationState({\n * filters: { mutationKey: ['posts'], status: 'success' },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * const latestSavedPost = savedPosts[savedPosts.length - 1]\n *\n * return <>{latestSavedPost ? 'Saved' : 'Nothing saved yet'}</>\n * }\n * ```\n */\nexport function useMutationState<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n options: MutationStateOptions<TResult, TMutation> = {},\n queryClient?: QueryClient,\n): Array<TResult> {\n const mutationCache = useQueryClient(queryClient).getMutationCache()\n const [createSnapshot] = React.useState(() =>\n createSnapshotFactory<TResult, TMutation>(),\n )\n const getSnapshot = createSnapshot(mutationCache, options)\n\n return React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n mutationCache.subscribe(notifyManager.batchCalls(onStoreChange)),\n [mutationCache],\n ),\n getSnapshot,\n getSnapshot,\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,cACd,SACA,aACQ;CACR,MAAM,SAASA,4BAAAA,eAAe,WAAW;CACzC,OAAO,iBACL,EAAE,SAAS;EAAE,GAAG;EAAS,QAAQ;CAAU,EAAE,GAC7C,MACF,CAAC,CAAC;AACJ;;;;;;;AA4BA,SAAS,wBAIL;CACF,IAAI;CAEJ,QACE,eACA,YACG;EACH,IAAI,SAAyB,CAAC;EAC9B,IAAI;EAEJ,aAA6B;GAC3B,MAAM,YAAY,cAAc,QAAQ,QAAQ,OAAO;GACvD,MAAM,aAAa,UAAU,SAAS,aAAa,CACjD,UACA,SAAS,KACX,CAAC;GAED,IACE,CAAC,YACD,WAAW,WAAW,OAAO,UAC7B,WAAW,MAAM,OAAO,UAAU,CAAC,OAAO,GAAG,OAAO,OAAO,MAAM,CAAC,GAClE;IACA,SAAS;IACT,YAAA,GAAWC,qBAAAA,iBAAAA,CACT,gBACA,UAAU,KACP,aACE,QAAQ,SACL,QAAQ,OAAO,QAAqB,IACpC,SAAS,KACjB,CACF;IACA,iBAAiB;GACnB;GAEA,OAAO;EACT;CACF;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,SAAgB,iBAKd,UAAoD,CAAC,GACrD,aACgB;CAChB,MAAM,gBAAgBD,4BAAAA,eAAe,WAAW,CAAC,CAAC,iBAAiB;CACnE,MAAM,CAAC,kBAAkBE,MAAM,eAC7B,sBAA0C,CAC5C;CACA,MAAM,cAAc,eAAe,eAAe,OAAO;CAEzD,OAAOA,MAAM,qBACXA,MAAM,aACH,kBACC,cAAc,UAAUC,qBAAAA,cAAc,WAAW,aAAa,CAAC,GACjE,CAAC,aAAa,CAChB,GACA,aACA,WACF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.d.cts","names":[],"sources":["../../src/useMutationState.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAgCgB,cACd,UAAU,iBACV,cAAc;KASX,uBAAuB,YAAY,kBACtC,oBACQ,aACA,cACA,kBACA,oBAGN,SAAS,OAAO,QAAQ,YAAY,mBACpC;KAEC,qBACH,UAAU,eACV,kBAAkB,+BAChB,uBAAuB;EAEzB,UAAU;EACV,UAAU,UAAU,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"useMutationState.d.cts","names":[],"sources":["../../src/useMutationState.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAgCgB,cACd,UAAU,iBACV,cAAc;KASX,uBAAuB,YAAY,kBACtC,oBACQ,aACA,cACA,kBACA,oBAGN,SAAS,OAAO,QAAQ,YAAY,mBACpC;KAEC,qBACH,UAAU,eACV,kBAAkB,+BAChB,uBAAuB;EAEzB,UAAU;EACV,UAAU,UAAU,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA0HpB,iBACd,UAAU,eACV,kBAAkB,+BAChB,uBAAuB,UAEzB,UAAS,qBAAqB,SAAS,YACvC,cAAc,cACb,MAAM"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.d.ts","names":[],"sources":["../../src/useMutationState.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAgCgB,cACd,UAAU,iBACV,cAAc;KASX,uBAAuB,YAAY,kBACtC,oBACQ,aACA,cACA,kBACA,oBAGN,SAAS,OAAO,QAAQ,YAAY,mBACpC;KAEC,qBACH,UAAU,eACV,kBAAkB,+BAChB,uBAAuB;EAEzB,UAAU;EACV,UAAU,UAAU,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"useMutationState.d.ts","names":[],"sources":["../../src/useMutationState.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAgCgB,cACd,UAAU,iBACV,cAAc;KASX,uBAAuB,YAAY,kBACtC,oBACQ,aACA,cACA,kBACA,oBAGN,SAAS,OAAO,QAAQ,YAAY,mBACpC;KAEC,qBACH,UAAU,eACV,kBAAkB,+BAChB,uBAAuB;EAEzB,UAAU;EACV,UAAU,UAAU,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA0HpB,iBACd,UAAU,eACV,kBAAkB,+BAChB,uBAAuB,UAEzB,UAAS,qBAAqB,SAAS,YACvC,cAAc,cACb,MAAM"}
|
|
@@ -30,14 +30,27 @@ function useIsMutating(filters, queryClient) {
|
|
|
30
30
|
} }, client).length;
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* @
|
|
37
|
-
* @returns The selected value of every matching mutation.
|
|
33
|
+
* Creates snapshot getters that share equal selected results across renders.
|
|
34
|
+
* A new getter is created on each render. Each getter caches its snapshot until the matching mutations
|
|
35
|
+
* or their states change.
|
|
36
|
+
* @returns A factory for snapshot getters with a shared result cache.
|
|
38
37
|
*/
|
|
39
|
-
function
|
|
40
|
-
|
|
38
|
+
function createSnapshotFactory() {
|
|
39
|
+
let previousResult;
|
|
40
|
+
return (mutationCache, options) => {
|
|
41
|
+
let inputs = [];
|
|
42
|
+
let snapshot;
|
|
43
|
+
return () => {
|
|
44
|
+
const mutations = mutationCache.findAll(options.filters);
|
|
45
|
+
const nextInputs = mutations.flatMap((mutation) => [mutation, mutation.state]);
|
|
46
|
+
if (!snapshot || nextInputs.length !== inputs.length || nextInputs.some((input, index) => !Object.is(input, inputs[index]))) {
|
|
47
|
+
inputs = nextInputs;
|
|
48
|
+
snapshot = replaceEqualDeep(previousResult, mutations.map((mutation) => options.select ? options.select(mutation) : mutation.state));
|
|
49
|
+
previousResult = snapshot;
|
|
50
|
+
}
|
|
51
|
+
return snapshot;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
41
54
|
}
|
|
42
55
|
/**
|
|
43
56
|
* `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass
|
|
@@ -110,19 +123,9 @@ function getResult(mutationCache, options) {
|
|
|
110
123
|
*/
|
|
111
124
|
function useMutationState(options = {}, queryClient) {
|
|
112
125
|
const mutationCache = useQueryClient(queryClient).getMutationCache();
|
|
113
|
-
const
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
React.useEffect(() => {
|
|
117
|
-
optionsRef.current = options;
|
|
118
|
-
});
|
|
119
|
-
return React.useSyncExternalStore(React.useCallback((onStoreChange) => mutationCache.subscribe(() => {
|
|
120
|
-
const nextResult = replaceEqualDeep(resultRef.current, getResult(mutationCache, optionsRef.current));
|
|
121
|
-
if (resultRef.current !== nextResult) {
|
|
122
|
-
resultRef.current = nextResult;
|
|
123
|
-
notifyManager.schedule(onStoreChange);
|
|
124
|
-
}
|
|
125
|
-
}), [mutationCache]), () => resultRef.current, () => resultRef.current);
|
|
126
|
+
const [createSnapshot] = React.useState(() => createSnapshotFactory());
|
|
127
|
+
const getSnapshot = createSnapshot(mutationCache, options);
|
|
128
|
+
return React.useSyncExternalStore(React.useCallback((onStoreChange) => mutationCache.subscribe(notifyManager.batchCalls(onStoreChange)), [mutationCache]), getSnapshot, getSnapshot);
|
|
126
129
|
}
|
|
127
130
|
//#endregion
|
|
128
131
|
export { useIsMutating, useMutationState };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.js","names":[],"sources":["../../src/useMutationState.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\n\nimport { notifyManager, replaceEqualDeep } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n QueryClient,\n} from '@tanstack/query-core'\n\n/**\n * The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`\n * (useful for app-wide loading indicators).\n * @param filters - The {@link MutationFilters} to narrow down the matched mutations.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be the `number` of the mutations that your application currently has `pending`.\n * @example\n * ```tsx\n * import { useIsMutating } from '@tanstack/react-query'\n *\n * function PostsMutatingIndicator() {\n * // How many mutations matching the posts prefix are in progress?\n * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })\n *\n * return isMutatingPosts ? <span>Saving posts...</span> : null\n * }\n * ```\n */\nexport function useIsMutating(\n filters?: MutationFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n return useMutationState(\n { filters: { ...filters, status: 'pending' } },\n client,\n ).length\n}\n\ntype MutationTypeFromResult<TResult> = [TResult] extends [\n MutationState<\n infer TData,\n infer TError,\n infer TVariables,\n infer TOnMutateResult\n >,\n]\n ? Mutation<TData, TError, TVariables, TOnMutateResult>\n : Mutation\n\ntype MutationStateOptions<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n> = {\n filters?: MutationFilters\n select?: (mutation: TMutation) => TResult\n}\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"useMutationState.js","names":[],"sources":["../../src/useMutationState.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\n\nimport { notifyManager, replaceEqualDeep } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n QueryClient,\n} from '@tanstack/query-core'\n\n/**\n * The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`\n * (useful for app-wide loading indicators).\n * @param filters - The {@link MutationFilters} to narrow down the matched mutations.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be the `number` of the mutations that your application currently has `pending`.\n * @example\n * ```tsx\n * import { useIsMutating } from '@tanstack/react-query'\n *\n * function PostsMutatingIndicator() {\n * // How many mutations matching the posts prefix are in progress?\n * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })\n *\n * return isMutatingPosts ? <span>Saving posts...</span> : null\n * }\n * ```\n */\nexport function useIsMutating(\n filters?: MutationFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n return useMutationState(\n { filters: { ...filters, status: 'pending' } },\n client,\n ).length\n}\n\ntype MutationTypeFromResult<TResult> = [TResult] extends [\n MutationState<\n infer TData,\n infer TError,\n infer TVariables,\n infer TOnMutateResult\n >,\n]\n ? Mutation<TData, TError, TVariables, TOnMutateResult>\n : Mutation\n\ntype MutationStateOptions<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n> = {\n filters?: MutationFilters\n select?: (mutation: TMutation) => TResult\n}\n\n/**\n * Creates snapshot getters that share equal selected results across renders.\n * A new getter is created on each render. Each getter caches its snapshot until the matching mutations\n * or their states change.\n * @returns A factory for snapshot getters with a shared result cache.\n */\nfunction createSnapshotFactory<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>() {\n let previousResult: Array<TResult> | undefined\n\n return (\n mutationCache: MutationCache,\n options: MutationStateOptions<TResult, TMutation>,\n ) => {\n let inputs: Array<unknown> = []\n let snapshot: Array<TResult> | undefined\n\n return (): Array<TResult> => {\n const mutations = mutationCache.findAll(options.filters)\n const nextInputs = mutations.flatMap((mutation) => [\n mutation,\n mutation.state,\n ])\n\n if (\n !snapshot ||\n nextInputs.length !== inputs.length ||\n nextInputs.some((input, index) => !Object.is(input, inputs[index]))\n ) {\n inputs = nextInputs\n snapshot = replaceEqualDeep(\n previousResult,\n mutations.map(\n (mutation): TResult =>\n (options.select\n ? options.select(mutation as TMutation)\n : mutation.state) as TResult,\n ),\n )\n previousResult = snapshot\n }\n\n return snapshot\n }\n }\n}\n\n/**\n * `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass\n * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation\n * state.\n * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the\n * mutation state.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be an Array of whatever `select` returns for each matching mutation.\n * @example\n * Get all variables of all running mutations:\n * ```tsx\n * import { useMutationState } from '@tanstack/react-query'\n *\n * function PendingPosts() {\n * const pendingVariables = useMutationState({\n * filters: { status: 'pending' },\n * select: (mutation) => mutation.state.variables,\n * })\n *\n * return <>{pendingVariables.length} posts saving...</>\n * }\n * ```\n * @example\n * Get all data for specific mutations via the `mutationKey`:\n * ```tsx\n * import { useMutation, useMutationState } from '@tanstack/react-query'\n *\n * const mutationKey = ['posts']\n *\n * function Posts() {\n * // Some mutation that we want to get the state for\n * const mutation = useMutation({\n * mutationKey,\n * mutationFn: createPosts,\n * })\n *\n * const savedPosts = useMutationState({\n * // this mutation key needs to match the mutation key of the given mutation (see above)\n * filters: { mutationKey, status: 'success' },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * return (\n * <button onClick={() => mutation.mutate(['New Post'])}>\n * Create post ({savedPosts.length} saved so far)\n * </button>\n * )\n * }\n * ```\n * @example\n * Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new\n * entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the\n * last item that `useMutationState` returns to get the latest successful invocation:\n * ```tsx\n * import { useMutationState } from '@tanstack/react-query'\n *\n * function LatestPost() {\n * const savedPosts = useMutationState({\n * filters: { mutationKey: ['posts'], status: 'success' },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * const latestSavedPost = savedPosts[savedPosts.length - 1]\n *\n * return <>{latestSavedPost ? 'Saved' : 'Nothing saved yet'}</>\n * }\n * ```\n */\nexport function useMutationState<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n options: MutationStateOptions<TResult, TMutation> = {},\n queryClient?: QueryClient,\n): Array<TResult> {\n const mutationCache = useQueryClient(queryClient).getMutationCache()\n const [createSnapshot] = React.useState(() =>\n createSnapshotFactory<TResult, TMutation>(),\n )\n const getSnapshot = createSnapshot(mutationCache, options)\n\n return React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n mutationCache.subscribe(notifyManager.batchCalls(onStoreChange)),\n [mutationCache],\n ),\n getSnapshot,\n getSnapshot,\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,cACd,SACA,aACQ;CACR,MAAM,SAAS,eAAe,WAAW;CACzC,OAAO,iBACL,EAAE,SAAS;EAAE,GAAG;EAAS,QAAQ;CAAU,EAAE,GAC7C,MACF,CAAC,CAAC;AACJ;;;;;;;AA4BA,SAAS,wBAIL;CACF,IAAI;CAEJ,QACE,eACA,YACG;EACH,IAAI,SAAyB,CAAC;EAC9B,IAAI;EAEJ,aAA6B;GAC3B,MAAM,YAAY,cAAc,QAAQ,QAAQ,OAAO;GACvD,MAAM,aAAa,UAAU,SAAS,aAAa,CACjD,UACA,SAAS,KACX,CAAC;GAED,IACE,CAAC,YACD,WAAW,WAAW,OAAO,UAC7B,WAAW,MAAM,OAAO,UAAU,CAAC,OAAO,GAAG,OAAO,OAAO,MAAM,CAAC,GAClE;IACA,SAAS;IACT,WAAW,iBACT,gBACA,UAAU,KACP,aACE,QAAQ,SACL,QAAQ,OAAO,QAAqB,IACpC,SAAS,KACjB,CACF;IACA,iBAAiB;GACnB;GAEA,OAAO;EACT;CACF;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,SAAgB,iBAKd,UAAoD,CAAC,GACrD,aACgB;CAChB,MAAM,gBAAgB,eAAe,WAAW,CAAC,CAAC,iBAAiB;CACnE,MAAM,CAAC,kBAAkB,MAAM,eAC7B,sBAA0C,CAC5C;CACA,MAAM,cAAc,eAAe,eAAe,OAAO;CAEzD,OAAO,MAAM,qBACX,MAAM,aACH,kBACC,cAAc,UAAU,cAAc,WAAW,aAAa,CAAC,GACjE,CAAC,aAAa,CAChB,GACA,aACA,WACF;AACF"}
|
|
@@ -33,14 +33,27 @@ function useIsMutating(filters, queryClient) {
|
|
|
33
33
|
} }, client).length;
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* @
|
|
40
|
-
* @returns The selected value of every matching mutation.
|
|
36
|
+
* Creates snapshot getters that share equal selected results across renders.
|
|
37
|
+
* A new getter is created on each render. Each getter caches its snapshot until the matching mutations
|
|
38
|
+
* or their states change.
|
|
39
|
+
* @returns A factory for snapshot getters with a shared result cache.
|
|
41
40
|
*/
|
|
42
|
-
function
|
|
43
|
-
|
|
41
|
+
function createSnapshotFactory() {
|
|
42
|
+
let previousResult;
|
|
43
|
+
return (mutationCache, options) => {
|
|
44
|
+
let inputs = [];
|
|
45
|
+
let snapshot;
|
|
46
|
+
return () => {
|
|
47
|
+
const mutations = mutationCache.findAll(options.filters);
|
|
48
|
+
const nextInputs = mutations.flatMap((mutation) => [mutation, mutation.state]);
|
|
49
|
+
if (!snapshot || nextInputs.length !== inputs.length || nextInputs.some((input, index) => !Object.is(input, inputs[index]))) {
|
|
50
|
+
inputs = nextInputs;
|
|
51
|
+
snapshot = (0, _tanstack_query_core.replaceEqualDeep)(previousResult, mutations.map((mutation) => options.select ? options.select(mutation) : mutation.state));
|
|
52
|
+
previousResult = snapshot;
|
|
53
|
+
}
|
|
54
|
+
return snapshot;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
44
57
|
}
|
|
45
58
|
/**
|
|
46
59
|
* `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass
|
|
@@ -113,19 +126,9 @@ function getResult(mutationCache, options) {
|
|
|
113
126
|
*/
|
|
114
127
|
function useMutationState(options = {}, queryClient) {
|
|
115
128
|
const mutationCache = require_QueryClientProvider.useQueryClient(queryClient).getMutationCache();
|
|
116
|
-
const
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
react.useEffect(() => {
|
|
120
|
-
optionsRef.current = options;
|
|
121
|
-
});
|
|
122
|
-
return react.useSyncExternalStore(react.useCallback((onStoreChange) => mutationCache.subscribe(() => {
|
|
123
|
-
const nextResult = (0, _tanstack_query_core.replaceEqualDeep)(resultRef.current, getResult(mutationCache, optionsRef.current));
|
|
124
|
-
if (resultRef.current !== nextResult) {
|
|
125
|
-
resultRef.current = nextResult;
|
|
126
|
-
_tanstack_query_core.notifyManager.schedule(onStoreChange);
|
|
127
|
-
}
|
|
128
|
-
}), [mutationCache]), () => resultRef.current, () => resultRef.current);
|
|
129
|
+
const [createSnapshot] = react.useState(() => createSnapshotFactory());
|
|
130
|
+
const getSnapshot = createSnapshot(mutationCache, options);
|
|
131
|
+
return react.useSyncExternalStore(react.useCallback((onStoreChange) => mutationCache.subscribe(_tanstack_query_core.notifyManager.batchCalls(onStoreChange)), [mutationCache]), getSnapshot, getSnapshot);
|
|
129
132
|
}
|
|
130
133
|
//#endregion
|
|
131
134
|
exports.useIsMutating = useIsMutating;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.cjs","names":["useQueryClient","React","
|
|
1
|
+
{"version":3,"file":"useMutationState.cjs","names":["useQueryClient","replaceEqualDeep","React","notifyManager"],"sources":["../../src/useMutationState.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\n\nimport { notifyManager, replaceEqualDeep } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n QueryClient,\n} from '@tanstack/query-core'\n\n/**\n * The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`\n * (useful for app-wide loading indicators).\n * @param filters - The {@link MutationFilters} to narrow down the matched mutations.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be the `number` of the mutations that your application currently has `pending`.\n * @example\n * ```tsx\n * import { useIsMutating } from '@tanstack/react-query'\n *\n * function PostsMutatingIndicator() {\n * // How many mutations matching the posts prefix are in progress?\n * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })\n *\n * return isMutatingPosts ? <span>Saving posts...</span> : null\n * }\n * ```\n */\nexport function useIsMutating(\n filters?: MutationFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n return useMutationState(\n { filters: { ...filters, status: 'pending' } },\n client,\n ).length\n}\n\ntype MutationTypeFromResult<TResult> = [TResult] extends [\n MutationState<\n infer TData,\n infer TError,\n infer TVariables,\n infer TOnMutateResult\n >,\n]\n ? Mutation<TData, TError, TVariables, TOnMutateResult>\n : Mutation\n\ntype MutationStateOptions<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n> = {\n filters?: MutationFilters\n select?: (mutation: TMutation) => TResult\n}\n\n/**\n * Creates snapshot getters that share equal selected results across renders.\n * A new getter is created on each render. Each getter caches its snapshot until the matching mutations\n * or their states change.\n * @returns A factory for snapshot getters with a shared result cache.\n */\nfunction createSnapshotFactory<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>() {\n let previousResult: Array<TResult> | undefined\n\n return (\n mutationCache: MutationCache,\n options: MutationStateOptions<TResult, TMutation>,\n ) => {\n let inputs: Array<unknown> = []\n let snapshot: Array<TResult> | undefined\n\n return (): Array<TResult> => {\n const mutations = mutationCache.findAll(options.filters)\n const nextInputs = mutations.flatMap((mutation) => [\n mutation,\n mutation.state,\n ])\n\n if (\n !snapshot ||\n nextInputs.length !== inputs.length ||\n nextInputs.some((input, index) => !Object.is(input, inputs[index]))\n ) {\n inputs = nextInputs\n snapshot = replaceEqualDeep(\n previousResult,\n mutations.map(\n (mutation): TResult =>\n (options.select\n ? options.select(mutation as TMutation)\n : mutation.state) as TResult,\n ),\n )\n previousResult = snapshot\n }\n\n return snapshot\n }\n }\n}\n\n/**\n * `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass\n * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation\n * state.\n * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the\n * mutation state.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be an Array of whatever `select` returns for each matching mutation.\n * @example\n * Get all variables of all running mutations:\n * ```tsx\n * import { useMutationState } from '@tanstack/react-query'\n *\n * function PendingPosts() {\n * const pendingVariables = useMutationState({\n * filters: { status: 'pending' },\n * select: (mutation) => mutation.state.variables,\n * })\n *\n * return <>{pendingVariables.length} posts saving...</>\n * }\n * ```\n * @example\n * Get all data for specific mutations via the `mutationKey`:\n * ```tsx\n * import { useMutation, useMutationState } from '@tanstack/react-query'\n *\n * const mutationKey = ['posts']\n *\n * function Posts() {\n * // Some mutation that we want to get the state for\n * const mutation = useMutation({\n * mutationKey,\n * mutationFn: createPosts,\n * })\n *\n * const savedPosts = useMutationState({\n * // this mutation key needs to match the mutation key of the given mutation (see above)\n * filters: { mutationKey, status: 'success' },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * return (\n * <button onClick={() => mutation.mutate(['New Post'])}>\n * Create post ({savedPosts.length} saved so far)\n * </button>\n * )\n * }\n * ```\n * @example\n * Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new\n * entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the\n * last item that `useMutationState` returns to get the latest successful invocation:\n * ```tsx\n * import { useMutationState } from '@tanstack/react-query'\n *\n * function LatestPost() {\n * const savedPosts = useMutationState({\n * filters: { mutationKey: ['posts'], status: 'success' },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * const latestSavedPost = savedPosts[savedPosts.length - 1]\n *\n * return <>{latestSavedPost ? 'Saved' : 'Nothing saved yet'}</>\n * }\n * ```\n */\nexport function useMutationState<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n options: MutationStateOptions<TResult, TMutation> = {},\n queryClient?: QueryClient,\n): Array<TResult> {\n const mutationCache = useQueryClient(queryClient).getMutationCache()\n const [createSnapshot] = React.useState(() =>\n createSnapshotFactory<TResult, TMutation>(),\n )\n const getSnapshot = createSnapshot(mutationCache, options)\n\n return React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n mutationCache.subscribe(notifyManager.batchCalls(onStoreChange)),\n [mutationCache],\n ),\n getSnapshot,\n getSnapshot,\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,cACd,SACA,aACQ;CACR,MAAM,SAASA,4BAAAA,eAAe,WAAW;CACzC,OAAO,iBACL,EAAE,SAAS;EAAE,GAAG;EAAS,QAAQ;CAAU,EAAE,GAC7C,MACF,CAAC,CAAC;AACJ;;;;;;;AA4BA,SAAS,wBAIL;CACF,IAAI;CAEJ,QACE,eACA,YACG;EACH,IAAI,SAAyB,CAAC;EAC9B,IAAI;EAEJ,aAA6B;GAC3B,MAAM,YAAY,cAAc,QAAQ,QAAQ,OAAO;GACvD,MAAM,aAAa,UAAU,SAAS,aAAa,CACjD,UACA,SAAS,KACX,CAAC;GAED,IACE,CAAC,YACD,WAAW,WAAW,OAAO,UAC7B,WAAW,MAAM,OAAO,UAAU,CAAC,OAAO,GAAG,OAAO,OAAO,MAAM,CAAC,GAClE;IACA,SAAS;IACT,YAAA,GAAWC,qBAAAA,iBAAAA,CACT,gBACA,UAAU,KACP,aACE,QAAQ,SACL,QAAQ,OAAO,QAAqB,IACpC,SAAS,KACjB,CACF;IACA,iBAAiB;GACnB;GAEA,OAAO;EACT;CACF;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,SAAgB,iBAKd,UAAoD,CAAC,GACrD,aACgB;CAChB,MAAM,gBAAgBD,4BAAAA,eAAe,WAAW,CAAC,CAAC,iBAAiB;CACnE,MAAM,CAAC,kBAAkBE,MAAM,eAC7B,sBAA0C,CAC5C;CACA,MAAM,cAAc,eAAe,eAAe,OAAO;CAEzD,OAAOA,MAAM,qBACXA,MAAM,aACH,kBACC,cAAc,UAAUC,qBAAAA,cAAc,WAAW,aAAa,CAAC,GACjE,CAAC,aAAa,CAChB,GACA,aACA,WACF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.d.cts","names":[],"sources":["../../src/useMutationState.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAgCgB,cACd,UAAU,iBACV,cAAc;KASX,uBAAuB,YAAY,kBACtC,oBACQ,aACA,cACA,kBACA,oBAGN,SAAS,OAAO,QAAQ,YAAY,mBACpC;KAEC,qBACH,UAAU,eACV,kBAAkB,+BAChB,uBAAuB;EAEzB,UAAU;EACV,UAAU,UAAU,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"useMutationState.d.cts","names":[],"sources":["../../src/useMutationState.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAgCgB,cACd,UAAU,iBACV,cAAc;KASX,uBAAuB,YAAY,kBACtC,oBACQ,aACA,cACA,kBACA,oBAGN,SAAS,OAAO,QAAQ,YAAY,mBACpC;KAEC,qBACH,UAAU,eACV,kBAAkB,+BAChB,uBAAuB;EAEzB,UAAU;EACV,UAAU,UAAU,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA0HpB,iBACd,UAAU,eACV,kBAAkB,+BAChB,uBAAuB,UAEzB,UAAS,qBAAqB,SAAS,YACvC,cAAc,cACb,MAAM"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.d.ts","names":[],"sources":["../../src/useMutationState.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAgCgB,cACd,UAAU,iBACV,cAAc;KASX,uBAAuB,YAAY,kBACtC,oBACQ,aACA,cACA,kBACA,oBAGN,SAAS,OAAO,QAAQ,YAAY,mBACpC;KAEC,qBACH,UAAU,eACV,kBAAkB,+BAChB,uBAAuB;EAEzB,UAAU;EACV,UAAU,UAAU,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"useMutationState.d.ts","names":[],"sources":["../../src/useMutationState.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAgCgB,cACd,UAAU,iBACV,cAAc;KASX,uBAAuB,YAAY,kBACtC,oBACQ,aACA,cACA,kBACA,oBAGN,SAAS,OAAO,QAAQ,YAAY,mBACpC;KAEC,qBACH,UAAU,eACV,kBAAkB,+BAChB,uBAAuB;EAEzB,UAAU;EACV,UAAU,UAAU,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA0HpB,iBACd,UAAU,eACV,kBAAkB,+BAChB,uBAAuB,UAEzB,UAAS,qBAAqB,SAAS,YACvC,cAAc,cACb,MAAM"}
|
|
@@ -30,14 +30,27 @@ function useIsMutating(filters, queryClient) {
|
|
|
30
30
|
} }, client).length;
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* @
|
|
37
|
-
* @returns The selected value of every matching mutation.
|
|
33
|
+
* Creates snapshot getters that share equal selected results across renders.
|
|
34
|
+
* A new getter is created on each render. Each getter caches its snapshot until the matching mutations
|
|
35
|
+
* or their states change.
|
|
36
|
+
* @returns A factory for snapshot getters with a shared result cache.
|
|
38
37
|
*/
|
|
39
|
-
function
|
|
40
|
-
|
|
38
|
+
function createSnapshotFactory() {
|
|
39
|
+
let previousResult;
|
|
40
|
+
return (mutationCache, options) => {
|
|
41
|
+
let inputs = [];
|
|
42
|
+
let snapshot;
|
|
43
|
+
return () => {
|
|
44
|
+
const mutations = mutationCache.findAll(options.filters);
|
|
45
|
+
const nextInputs = mutations.flatMap((mutation) => [mutation, mutation.state]);
|
|
46
|
+
if (!snapshot || nextInputs.length !== inputs.length || nextInputs.some((input, index) => !Object.is(input, inputs[index]))) {
|
|
47
|
+
inputs = nextInputs;
|
|
48
|
+
snapshot = replaceEqualDeep(previousResult, mutations.map((mutation) => options.select ? options.select(mutation) : mutation.state));
|
|
49
|
+
previousResult = snapshot;
|
|
50
|
+
}
|
|
51
|
+
return snapshot;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
41
54
|
}
|
|
42
55
|
/**
|
|
43
56
|
* `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass
|
|
@@ -110,19 +123,9 @@ function getResult(mutationCache, options) {
|
|
|
110
123
|
*/
|
|
111
124
|
function useMutationState(options = {}, queryClient) {
|
|
112
125
|
const mutationCache = useQueryClient(queryClient).getMutationCache();
|
|
113
|
-
const
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
React.useEffect(() => {
|
|
117
|
-
optionsRef.current = options;
|
|
118
|
-
});
|
|
119
|
-
return React.useSyncExternalStore(React.useCallback((onStoreChange) => mutationCache.subscribe(() => {
|
|
120
|
-
const nextResult = replaceEqualDeep(resultRef.current, getResult(mutationCache, optionsRef.current));
|
|
121
|
-
if (resultRef.current !== nextResult) {
|
|
122
|
-
resultRef.current = nextResult;
|
|
123
|
-
notifyManager.schedule(onStoreChange);
|
|
124
|
-
}
|
|
125
|
-
}), [mutationCache]), () => resultRef.current, () => resultRef.current);
|
|
126
|
+
const [createSnapshot] = React.useState(() => createSnapshotFactory());
|
|
127
|
+
const getSnapshot = createSnapshot(mutationCache, options);
|
|
128
|
+
return React.useSyncExternalStore(React.useCallback((onStoreChange) => mutationCache.subscribe(notifyManager.batchCalls(onStoreChange)), [mutationCache]), getSnapshot, getSnapshot);
|
|
126
129
|
}
|
|
127
130
|
//#endregion
|
|
128
131
|
export { useIsMutating, useMutationState };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMutationState.js","names":[],"sources":["../../src/useMutationState.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\n\nimport { notifyManager, replaceEqualDeep } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n QueryClient,\n} from '@tanstack/query-core'\n\n/**\n * The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`\n * (useful for app-wide loading indicators).\n * @param filters - The {@link MutationFilters} to narrow down the matched mutations.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be the `number` of the mutations that your application currently has `pending`.\n * @example\n * ```tsx\n * import { useIsMutating } from '@tanstack/react-query'\n *\n * function PostsMutatingIndicator() {\n * // How many mutations matching the posts prefix are in progress?\n * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })\n *\n * return isMutatingPosts ? <span>Saving posts...</span> : null\n * }\n * ```\n */\nexport function useIsMutating(\n filters?: MutationFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n return useMutationState(\n { filters: { ...filters, status: 'pending' } },\n client,\n ).length\n}\n\ntype MutationTypeFromResult<TResult> = [TResult] extends [\n MutationState<\n infer TData,\n infer TError,\n infer TVariables,\n infer TOnMutateResult\n >,\n]\n ? Mutation<TData, TError, TVariables, TOnMutateResult>\n : Mutation\n\ntype MutationStateOptions<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n> = {\n filters?: MutationFilters\n select?: (mutation: TMutation) => TResult\n}\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"useMutationState.js","names":[],"sources":["../../src/useMutationState.ts"],"sourcesContent":["'use client'\nimport * as React from 'react'\n\nimport { notifyManager, replaceEqualDeep } from '@tanstack/query-core'\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n Mutation,\n MutationCache,\n MutationFilters,\n MutationState,\n QueryClient,\n} from '@tanstack/query-core'\n\n/**\n * The `useIsMutating` hook returns the `number` of mutations that your application currently has `pending`\n * (useful for app-wide loading indicators).\n * @param filters - The {@link MutationFilters} to narrow down the matched mutations.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be the `number` of the mutations that your application currently has `pending`.\n * @example\n * ```tsx\n * import { useIsMutating } from '@tanstack/react-query'\n *\n * function PostsMutatingIndicator() {\n * // How many mutations matching the posts prefix are in progress?\n * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })\n *\n * return isMutatingPosts ? <span>Saving posts...</span> : null\n * }\n * ```\n */\nexport function useIsMutating(\n filters?: MutationFilters,\n queryClient?: QueryClient,\n): number {\n const client = useQueryClient(queryClient)\n return useMutationState(\n { filters: { ...filters, status: 'pending' } },\n client,\n ).length\n}\n\ntype MutationTypeFromResult<TResult> = [TResult] extends [\n MutationState<\n infer TData,\n infer TError,\n infer TVariables,\n infer TOnMutateResult\n >,\n]\n ? Mutation<TData, TError, TVariables, TOnMutateResult>\n : Mutation\n\ntype MutationStateOptions<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n> = {\n filters?: MutationFilters\n select?: (mutation: TMutation) => TResult\n}\n\n/**\n * Creates snapshot getters that share equal selected results across renders.\n * A new getter is created on each render. Each getter caches its snapshot until the matching mutations\n * or their states change.\n * @returns A factory for snapshot getters with a shared result cache.\n */\nfunction createSnapshotFactory<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>() {\n let previousResult: Array<TResult> | undefined\n\n return (\n mutationCache: MutationCache,\n options: MutationStateOptions<TResult, TMutation>,\n ) => {\n let inputs: Array<unknown> = []\n let snapshot: Array<TResult> | undefined\n\n return (): Array<TResult> => {\n const mutations = mutationCache.findAll(options.filters)\n const nextInputs = mutations.flatMap((mutation) => [\n mutation,\n mutation.state,\n ])\n\n if (\n !snapshot ||\n nextInputs.length !== inputs.length ||\n nextInputs.some((input, index) => !Object.is(input, inputs[index]))\n ) {\n inputs = nextInputs\n snapshot = replaceEqualDeep(\n previousResult,\n mutations.map(\n (mutation): TResult =>\n (options.select\n ? options.select(mutation as TMutation)\n : mutation.state) as TResult,\n ),\n )\n previousResult = snapshot\n }\n\n return snapshot\n }\n }\n}\n\n/**\n * `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass\n * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation\n * state.\n * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the\n * mutation state.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns Will be an Array of whatever `select` returns for each matching mutation.\n * @example\n * Get all variables of all running mutations:\n * ```tsx\n * import { useMutationState } from '@tanstack/react-query'\n *\n * function PendingPosts() {\n * const pendingVariables = useMutationState({\n * filters: { status: 'pending' },\n * select: (mutation) => mutation.state.variables,\n * })\n *\n * return <>{pendingVariables.length} posts saving...</>\n * }\n * ```\n * @example\n * Get all data for specific mutations via the `mutationKey`:\n * ```tsx\n * import { useMutation, useMutationState } from '@tanstack/react-query'\n *\n * const mutationKey = ['posts']\n *\n * function Posts() {\n * // Some mutation that we want to get the state for\n * const mutation = useMutation({\n * mutationKey,\n * mutationFn: createPosts,\n * })\n *\n * const savedPosts = useMutationState({\n * // this mutation key needs to match the mutation key of the given mutation (see above)\n * filters: { mutationKey, status: 'success' },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * return (\n * <button onClick={() => mutation.mutate(['New Post'])}>\n * Create post ({savedPosts.length} saved so far)\n * </button>\n * )\n * }\n * ```\n * @example\n * Access the latest successful mutation data via the `mutationKey`. Each invocation of `mutate` adds a new\n * entry to the mutation cache for `gcTime` milliseconds — with the `status: 'success'` filter below, check the\n * last item that `useMutationState` returns to get the latest successful invocation:\n * ```tsx\n * import { useMutationState } from '@tanstack/react-query'\n *\n * function LatestPost() {\n * const savedPosts = useMutationState({\n * filters: { mutationKey: ['posts'], status: 'success' },\n * select: (mutation) => mutation.state.data,\n * })\n *\n * const latestSavedPost = savedPosts[savedPosts.length - 1]\n *\n * return <>{latestSavedPost ? 'Saved' : 'Nothing saved yet'}</>\n * }\n * ```\n */\nexport function useMutationState<\n TResult = MutationState,\n TMutation extends Mutation<any, any, any, any> =\n MutationTypeFromResult<TResult>,\n>(\n options: MutationStateOptions<TResult, TMutation> = {},\n queryClient?: QueryClient,\n): Array<TResult> {\n const mutationCache = useQueryClient(queryClient).getMutationCache()\n const [createSnapshot] = React.useState(() =>\n createSnapshotFactory<TResult, TMutation>(),\n )\n const getSnapshot = createSnapshot(mutationCache, options)\n\n return React.useSyncExternalStore(\n React.useCallback(\n (onStoreChange) =>\n mutationCache.subscribe(notifyManager.batchCalls(onStoreChange)),\n [mutationCache],\n ),\n getSnapshot,\n getSnapshot,\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAgB,cACd,SACA,aACQ;CACR,MAAM,SAAS,eAAe,WAAW;CACzC,OAAO,iBACL,EAAE,SAAS;EAAE,GAAG;EAAS,QAAQ;CAAU,EAAE,GAC7C,MACF,CAAC,CAAC;AACJ;;;;;;;AA4BA,SAAS,wBAIL;CACF,IAAI;CAEJ,QACE,eACA,YACG;EACH,IAAI,SAAyB,CAAC;EAC9B,IAAI;EAEJ,aAA6B;GAC3B,MAAM,YAAY,cAAc,QAAQ,QAAQ,OAAO;GACvD,MAAM,aAAa,UAAU,SAAS,aAAa,CACjD,UACA,SAAS,KACX,CAAC;GAED,IACE,CAAC,YACD,WAAW,WAAW,OAAO,UAC7B,WAAW,MAAM,OAAO,UAAU,CAAC,OAAO,GAAG,OAAO,OAAO,MAAM,CAAC,GAClE;IACA,SAAS;IACT,WAAW,iBACT,gBACA,UAAU,KACP,aACE,QAAQ,SACL,QAAQ,OAAO,QAAqB,IACpC,SAAS,KACjB,CACF;IACA,iBAAiB;GACnB;GAEA,OAAO;EACT;CACF;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,SAAgB,iBAKd,UAAoD,CAAC,GACrD,aACgB;CAChB,MAAM,gBAAgB,eAAe,WAAW,CAAC,CAAC,iBAAiB;CACnE,MAAM,CAAC,kBAAkB,MAAM,eAC7B,sBAA0C,CAC5C;CACA,MAAM,cAAc,eAAe,eAAe,OAAO;CAEzD,OAAO,MAAM,qBACX,MAAM,aACH,kBACC,cAAc,UAAU,cAAc,WAAW,aAAa,CAAC,GACjE,CAAC,aAAa,CAChB,GACA,aACA,WACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-query",
|
|
3
|
-
"version": "5.104.
|
|
3
|
+
"version": "5.104.5",
|
|
4
4
|
"description": "Hooks for managing, caching and syncing asynchronous and remote data in React",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -44,11 +44,11 @@
|
|
|
44
44
|
"!build/codemods/**/__tests__"
|
|
45
45
|
],
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@tanstack/query-core": "5.104.
|
|
47
|
+
"@tanstack/query-core": "5.104.5"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@eslint-react/eslint-plugin": "^5.23.3",
|
|
51
|
-
"@tanstack/query-persist-client-core": "5.104.
|
|
51
|
+
"@tanstack/query-persist-client-core": "5.104.5",
|
|
52
52
|
"@tanstack/query-test-utils": "0.0.0",
|
|
53
53
|
"@testing-library/react": "^16.1.0",
|
|
54
54
|
"@testing-library/react-render-stream": "^2.0.2",
|
package/src/useMutationState.ts
CHANGED
|
@@ -62,28 +62,53 @@ type MutationStateOptions<
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* @
|
|
69
|
-
* @returns The selected value of every matching mutation.
|
|
65
|
+
* Creates snapshot getters that share equal selected results across renders.
|
|
66
|
+
* A new getter is created on each render. Each getter caches its snapshot until the matching mutations
|
|
67
|
+
* or their states change.
|
|
68
|
+
* @returns A factory for snapshot getters with a shared result cache.
|
|
70
69
|
*/
|
|
71
|
-
function
|
|
70
|
+
function createSnapshotFactory<
|
|
72
71
|
TResult = MutationState,
|
|
73
72
|
TMutation extends Mutation<any, any, any, any> =
|
|
74
73
|
MutationTypeFromResult<TResult>,
|
|
75
|
-
>(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
74
|
+
>() {
|
|
75
|
+
let previousResult: Array<TResult> | undefined
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
mutationCache: MutationCache,
|
|
79
|
+
options: MutationStateOptions<TResult, TMutation>,
|
|
80
|
+
) => {
|
|
81
|
+
let inputs: Array<unknown> = []
|
|
82
|
+
let snapshot: Array<TResult> | undefined
|
|
83
|
+
|
|
84
|
+
return (): Array<TResult> => {
|
|
85
|
+
const mutations = mutationCache.findAll(options.filters)
|
|
86
|
+
const nextInputs = mutations.flatMap((mutation) => [
|
|
87
|
+
mutation,
|
|
88
|
+
mutation.state,
|
|
89
|
+
])
|
|
90
|
+
|
|
91
|
+
if (
|
|
92
|
+
!snapshot ||
|
|
93
|
+
nextInputs.length !== inputs.length ||
|
|
94
|
+
nextInputs.some((input, index) => !Object.is(input, inputs[index]))
|
|
95
|
+
) {
|
|
96
|
+
inputs = nextInputs
|
|
97
|
+
snapshot = replaceEqualDeep(
|
|
98
|
+
previousResult,
|
|
99
|
+
mutations.map(
|
|
100
|
+
(mutation): TResult =>
|
|
101
|
+
(options.select
|
|
102
|
+
? options.select(mutation as TMutation)
|
|
103
|
+
: mutation.state) as TResult,
|
|
104
|
+
),
|
|
105
|
+
)
|
|
106
|
+
previousResult = snapshot
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return snapshot
|
|
110
|
+
}
|
|
111
|
+
}
|
|
87
112
|
}
|
|
88
113
|
|
|
89
114
|
/**
|
|
@@ -164,32 +189,18 @@ export function useMutationState<
|
|
|
164
189
|
queryClient?: QueryClient,
|
|
165
190
|
): Array<TResult> {
|
|
166
191
|
const mutationCache = useQueryClient(queryClient).getMutationCache()
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
React.useEffect(() => {
|
|
174
|
-
optionsRef.current = options
|
|
175
|
-
})
|
|
192
|
+
const [createSnapshot] = React.useState(() =>
|
|
193
|
+
createSnapshotFactory<TResult, TMutation>(),
|
|
194
|
+
)
|
|
195
|
+
const getSnapshot = createSnapshot(mutationCache, options)
|
|
176
196
|
|
|
177
197
|
return React.useSyncExternalStore(
|
|
178
198
|
React.useCallback(
|
|
179
199
|
(onStoreChange) =>
|
|
180
|
-
mutationCache.subscribe(()
|
|
181
|
-
const nextResult = replaceEqualDeep(
|
|
182
|
-
resultRef.current,
|
|
183
|
-
getResult(mutationCache, optionsRef.current),
|
|
184
|
-
)
|
|
185
|
-
if (resultRef.current !== nextResult) {
|
|
186
|
-
resultRef.current = nextResult
|
|
187
|
-
notifyManager.schedule(onStoreChange)
|
|
188
|
-
}
|
|
189
|
-
}),
|
|
200
|
+
mutationCache.subscribe(notifyManager.batchCalls(onStoreChange)),
|
|
190
201
|
[mutationCache],
|
|
191
202
|
),
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
)
|
|
203
|
+
getSnapshot,
|
|
204
|
+
getSnapshot,
|
|
205
|
+
)
|
|
195
206
|
}
|