@tanstack/query-core 5.105.1 → 5.105.2
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/{hydration-DdkP3REv.d.cts → hydration-BXjqUj_D.d.cts} +25 -14
- package/build/legacy/hydration-BXjqUj_D.d.cts.map +1 -0
- package/build/legacy/{hydration-gHEs4kLM.d.ts → hydration-vgIjIhk6.d.ts} +25 -14
- package/build/legacy/hydration-vgIjIhk6.d.ts.map +1 -0
- package/build/legacy/hydration.d.cts +1 -1
- package/build/legacy/hydration.d.ts +1 -1
- package/build/legacy/index.d.cts +1 -1
- package/build/legacy/index.d.ts +1 -1
- package/build/legacy/infiniteQueryBehavior.cjs +2 -1
- package/build/legacy/infiniteQueryBehavior.cjs.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
- package/build/legacy/infiniteQueryBehavior.d.cts.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
- package/build/legacy/infiniteQueryBehavior.d.ts.map +1 -1
- package/build/legacy/infiniteQueryBehavior.js +3 -2
- package/build/legacy/infiniteQueryBehavior.js.map +1 -1
- package/build/legacy/infiniteQueryObserver.d.cts +1 -1
- package/build/legacy/infiniteQueryObserver.d.ts +1 -1
- package/build/legacy/mutation.cjs +1 -0
- package/build/legacy/mutation.cjs.map +1 -1
- package/build/legacy/mutation.d.cts +1 -1
- package/build/legacy/mutation.d.ts +1 -1
- package/build/legacy/mutation.js +1 -0
- package/build/legacy/mutation.js.map +1 -1
- package/build/legacy/mutationCache.d.cts +1 -1
- package/build/legacy/mutationCache.d.ts +1 -1
- package/build/legacy/mutationObserver.d.cts +1 -1
- package/build/legacy/mutationObserver.d.ts +1 -1
- package/build/legacy/queriesObserver.d.cts +1 -1
- package/build/legacy/queriesObserver.d.ts +1 -1
- package/build/legacy/query.cjs +54 -36
- package/build/legacy/query.cjs.map +1 -1
- package/build/legacy/query.d.cts +1 -1
- package/build/legacy/query.d.ts +1 -1
- package/build/legacy/query.js +55 -37
- package/build/legacy/query.js.map +1 -1
- package/build/legacy/queryCache.d.cts +1 -1
- package/build/legacy/queryCache.d.ts +1 -1
- package/build/legacy/queryClient.cjs +11 -6
- package/build/legacy/queryClient.cjs.map +1 -1
- package/build/legacy/queryClient.d.cts +1 -1
- package/build/legacy/queryClient.d.ts +1 -1
- package/build/legacy/queryClient.js +11 -6
- package/build/legacy/queryClient.js.map +1 -1
- package/build/legacy/queryObserver.d.cts +1 -1
- package/build/legacy/queryObserver.d.ts +1 -1
- package/build/legacy/retryer.d.cts +1 -1
- package/build/legacy/retryer.d.ts +1 -1
- package/build/legacy/streamedQuery.cjs +10 -4
- package/build/legacy/streamedQuery.cjs.map +1 -1
- package/build/legacy/streamedQuery.d.cts +3 -1
- package/build/legacy/streamedQuery.d.cts.map +1 -1
- package/build/legacy/streamedQuery.d.ts +3 -1
- package/build/legacy/streamedQuery.d.ts.map +1 -1
- package/build/legacy/streamedQuery.js +11 -5
- package/build/legacy/streamedQuery.js.map +1 -1
- package/build/legacy/types.d.cts +1 -1
- package/build/legacy/types.d.ts +1 -1
- package/build/legacy/utils.cjs +23 -19
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts +2 -2
- package/build/legacy/utils.d.ts +2 -2
- package/build/legacy/utils.js +23 -20
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/{hydration-DdkP3REv.d.cts → hydration-BXjqUj_D.d.cts} +25 -14
- package/build/modern/hydration-BXjqUj_D.d.cts.map +1 -0
- package/build/modern/{hydration-gHEs4kLM.d.ts → hydration-vgIjIhk6.d.ts} +25 -14
- package/build/modern/hydration-vgIjIhk6.d.ts.map +1 -0
- package/build/modern/hydration.d.cts +1 -1
- package/build/modern/hydration.d.ts +1 -1
- package/build/modern/index.d.cts +1 -1
- package/build/modern/index.d.ts +1 -1
- package/build/modern/infiniteQueryBehavior.cjs +2 -1
- package/build/modern/infiniteQueryBehavior.cjs.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.cts +1 -1
- package/build/modern/infiniteQueryBehavior.d.cts.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.ts +1 -1
- package/build/modern/infiniteQueryBehavior.d.ts.map +1 -1
- package/build/modern/infiniteQueryBehavior.js +3 -2
- package/build/modern/infiniteQueryBehavior.js.map +1 -1
- package/build/modern/infiniteQueryObserver.d.cts +1 -1
- package/build/modern/infiniteQueryObserver.d.ts +1 -1
- package/build/modern/mutation.cjs +1 -0
- package/build/modern/mutation.cjs.map +1 -1
- package/build/modern/mutation.d.cts +1 -1
- package/build/modern/mutation.d.ts +1 -1
- package/build/modern/mutation.js +1 -0
- package/build/modern/mutation.js.map +1 -1
- package/build/modern/mutationCache.d.cts +1 -1
- package/build/modern/mutationCache.d.ts +1 -1
- package/build/modern/mutationObserver.d.cts +1 -1
- package/build/modern/mutationObserver.d.ts +1 -1
- package/build/modern/queriesObserver.d.cts +1 -1
- package/build/modern/queriesObserver.d.ts +1 -1
- package/build/modern/query.cjs +51 -34
- package/build/modern/query.cjs.map +1 -1
- package/build/modern/query.d.cts +1 -1
- package/build/modern/query.d.ts +1 -1
- package/build/modern/query.js +52 -35
- package/build/modern/query.js.map +1 -1
- package/build/modern/queryCache.d.cts +1 -1
- package/build/modern/queryCache.d.ts +1 -1
- package/build/modern/queryClient.cjs +11 -6
- package/build/modern/queryClient.cjs.map +1 -1
- package/build/modern/queryClient.d.cts +1 -1
- package/build/modern/queryClient.d.ts +1 -1
- package/build/modern/queryClient.js +11 -6
- package/build/modern/queryClient.js.map +1 -1
- package/build/modern/queryObserver.d.cts +1 -1
- package/build/modern/queryObserver.d.ts +1 -1
- package/build/modern/retryer.d.cts +1 -1
- package/build/modern/retryer.d.ts +1 -1
- package/build/modern/streamedQuery.cjs +10 -4
- package/build/modern/streamedQuery.cjs.map +1 -1
- package/build/modern/streamedQuery.d.cts +3 -1
- package/build/modern/streamedQuery.d.cts.map +1 -1
- package/build/modern/streamedQuery.d.ts +3 -1
- package/build/modern/streamedQuery.d.ts.map +1 -1
- package/build/modern/streamedQuery.js +11 -5
- package/build/modern/streamedQuery.js.map +1 -1
- package/build/modern/types.d.cts +1 -1
- package/build/modern/types.d.ts +1 -1
- package/build/modern/utils.cjs +23 -19
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts +2 -2
- package/build/modern/utils.d.ts +2 -2
- package/build/modern/utils.js +23 -20
- package/build/modern/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/infiniteQueryBehavior.ts +5 -1
- package/src/mutation.ts +4 -0
- package/src/query.ts +78 -64
- package/src/queryClient.ts +16 -10
- package/src/streamedQuery.ts +14 -3
- package/src/utils.ts +39 -26
- package/build/legacy/hydration-DdkP3REv.d.cts.map +0 -1
- package/build/legacy/hydration-gHEs4kLM.d.ts.map +0 -1
- package/build/modern/hydration-DdkP3REv.d.cts.map +0 -1
- package/build/modern/hydration-gHEs4kLM.d.ts.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { A as InfiniteData, V as InfiniteQueryPageParamsOptions,
|
|
1
|
+
import { A as InfiniteData, V as InfiniteQueryPageParamsOptions, nr as QueryBehavior } from "./hydration-BXjqUj_D.cjs";
|
|
2
2
|
/**
|
|
3
3
|
* Creates the behavior that fetches the pages of an infinite query: the next or previous page for
|
|
4
4
|
* `fetchNextPage`/`fetchPreviousPage`, or all pages again from the start on a refetch.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infiniteQueryBehavior.d.cts","names":[],"sources":["../../src/infiniteQueryBehavior.ts"],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"infiniteQueryBehavior.d.cts","names":[],"sources":["../../src/infiniteQueryBehavior.ts"],"mappings":";;;;;;;;;wBAyBgB,sBAAsB,cAAc,QAAQ,OAAO,YACjE,iBACC,cAAc,cAAc,QAAQ,aAAa,OAAO;;;;;;;wBAkK3C,YACd,SAAS,0CACT,OAAO;;;;;;;;wBAaO,gBACd,SAAS,0CACT,OAAO"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { A as InfiniteData, V as InfiniteQueryPageParamsOptions,
|
|
1
|
+
import { A as InfiniteData, V as InfiniteQueryPageParamsOptions, nr as QueryBehavior } from "./hydration-vgIjIhk6.js";
|
|
2
2
|
/**
|
|
3
3
|
* Creates the behavior that fetches the pages of an infinite query: the next or previous page for
|
|
4
4
|
* `fetchNextPage`/`fetchPreviousPage`, or all pages again from the start on a refetch.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infiniteQueryBehavior.d.ts","names":[],"sources":["../../src/infiniteQueryBehavior.ts"],"mappings":";;;;;;;;;
|
|
1
|
+
{"version":3,"file":"infiniteQueryBehavior.d.ts","names":[],"sources":["../../src/infiniteQueryBehavior.ts"],"mappings":";;;;;;;;;wBAyBgB,sBAAsB,cAAc,QAAQ,OAAO,YACjE,iBACC,cAAc,cAAc,QAAQ,aAAa,OAAO;;;;;;;wBAkK3C,YACd,SAAS,0CACT,OAAO;;;;;;;;wBAaO,gBACd,SAAS,0CACT,OAAO"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { addConsumeAwareSignal, addToEnd, addToStart, ensureQueryFn } from "./utils.js";
|
|
1
|
+
import { addConsumeAwareSignal, addToEnd, addToStart, ensureQueryFn, fetchSignal } from "./utils.js";
|
|
2
2
|
// src/infiniteQueryBehavior.ts
|
|
3
3
|
/**
|
|
4
4
|
* Creates the behavior that fetches the pages of an infinite query: the next or previous page for
|
|
@@ -21,8 +21,9 @@ function infiniteQueryBehavior(pages) {
|
|
|
21
21
|
let currentPage = 0;
|
|
22
22
|
const fetchFn = async () => {
|
|
23
23
|
let cancelled = false;
|
|
24
|
+
const signalContext = context;
|
|
24
25
|
const addSignalProperty = (object) => {
|
|
25
|
-
addConsumeAwareSignal(object, () => context.signal, () => cancelled = true);
|
|
26
|
+
addConsumeAwareSignal(object, () => context.signal, () => cancelled = true, signalContext[fetchSignal]);
|
|
26
27
|
};
|
|
27
28
|
const queryFn = ensureQueryFn(context.options, context.fetchOptions);
|
|
28
29
|
const fetchPage = async (data, param, previous) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"infiniteQueryBehavior.js","names":[],"sources":["../../src/infiniteQueryBehavior.ts"],"sourcesContent":["import {\n addConsumeAwareSignal,\n addToEnd,\n addToStart,\n ensureQueryFn,\n} from './utils'\nimport type { QueryBehavior } from './query'\nimport type {\n InfiniteData,\n InfiniteQueryPageParamsOptions,\n OmitKeyof,\n QueryFunctionContext,\n QueryKey,\n} from './types'\n\n/**\n * Creates the behavior that fetches the pages of an infinite query: the next or previous page for\n * `fetchNextPage`/`fetchPreviousPage`, or all pages again from the start on a refetch.\n * @param pages - The number of pages to fetch from the start when all pages are fetched again,\n * e.g. from the `pages` option of `queryClient.infiniteQuery`. Defaults to the number of cached\n * pages.\n * @returns The query behavior to set on an infinite query.\n */\nexport function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>(\n pages?: number,\n): QueryBehavior<TQueryFnData, TError, InfiniteData<TData, TPageParam>> {\n return {\n onFetch: (context, query) => {\n const options = context.options as InfiniteQueryPageParamsOptions<TData>\n const direction = context.fetchOptions?.meta?.fetchMore?.direction\n const oldPages = context.state.data?.pages || []\n const oldPageParams = context.state.data?.pageParams || []\n let result: InfiniteData<unknown> = { pages: [], pageParams: [] }\n let currentPage = 0\n\n const fetchFn = async () => {\n let cancelled = false\n const addSignalProperty = (object:
|
|
1
|
+
{"version":3,"file":"infiniteQueryBehavior.js","names":[],"sources":["../../src/infiniteQueryBehavior.ts"],"sourcesContent":["import {\n addConsumeAwareSignal,\n addToEnd,\n addToStart,\n ensureQueryFn,\n fetchSignal,\n} from './utils'\nimport type { FetchSignalContext } from './utils'\nimport type { QueryBehavior } from './query'\nimport type {\n InfiniteData,\n InfiniteQueryPageParamsOptions,\n OmitKeyof,\n QueryFunctionContext,\n QueryKey,\n} from './types'\n\n/**\n * Creates the behavior that fetches the pages of an infinite query: the next or previous page for\n * `fetchNextPage`/`fetchPreviousPage`, or all pages again from the start on a refetch.\n * @param pages - The number of pages to fetch from the start when all pages are fetched again,\n * e.g. from the `pages` option of `queryClient.infiniteQuery`. Defaults to the number of cached\n * pages.\n * @returns The query behavior to set on an infinite query.\n */\nexport function infiniteQueryBehavior<TQueryFnData, TError, TData, TPageParam>(\n pages?: number,\n): QueryBehavior<TQueryFnData, TError, InfiniteData<TData, TPageParam>> {\n return {\n onFetch: (context, query) => {\n const options = context.options as InfiniteQueryPageParamsOptions<TData>\n const direction = context.fetchOptions?.meta?.fetchMore?.direction\n const oldPages = context.state.data?.pages || []\n const oldPageParams = context.state.data?.pageParams || []\n let result: InfiniteData<unknown> = { pages: [], pageParams: [] }\n let currentPage = 0\n\n const fetchFn = async () => {\n let cancelled = false\n const signalContext: FetchSignalContext = context\n const addSignalProperty = (object: object) => {\n addConsumeAwareSignal(\n object,\n () => context.signal,\n () => (cancelled = true),\n signalContext[fetchSignal],\n )\n }\n\n const queryFn = ensureQueryFn(context.options, context.fetchOptions)\n\n // Create function to fetch a page\n const fetchPage = async (\n data: InfiniteData<unknown>,\n param: unknown,\n previous?: boolean,\n ): Promise<InfiniteData<unknown>> => {\n if (cancelled) {\n return Promise.reject(context.signal.reason)\n }\n\n if (param == null && data.pages.length) {\n return Promise.resolve(data)\n }\n\n const createQueryFnContext = () => {\n const queryFnContext: OmitKeyof<\n QueryFunctionContext<QueryKey, unknown>,\n 'signal'\n > = {\n client: context.client,\n queryKey: context.queryKey,\n pageParam: param,\n direction: previous ? 'backward' : 'forward',\n meta: context.options.meta,\n }\n addSignalProperty(queryFnContext)\n return queryFnContext as QueryFunctionContext<QueryKey, unknown>\n }\n\n const queryFnContext = createQueryFnContext()\n\n const page = await queryFn(queryFnContext)\n\n const { maxPages } = context.options\n const addTo = previous ? addToStart : addToEnd\n\n return {\n pages: addTo(data.pages, page, maxPages),\n pageParams: addTo(data.pageParams, param, maxPages),\n }\n }\n\n // fetch next / previous page?\n if (direction && oldPages.length) {\n const previous = direction === 'backward'\n const pageParamFn = previous ? getPreviousPageParam : getNextPageParam\n const oldData = {\n pages: oldPages,\n pageParams: oldPageParams,\n }\n const param = pageParamFn(options, oldData)\n\n result = await fetchPage(oldData, param, previous)\n } else {\n const remainingPages = pages ?? oldPages.length\n\n // Fetch all pages\n do {\n const param =\n currentPage === 0\n ? oldPageParams[0] === undefined\n ? options.initialPageParam\n : oldPageParams[0]\n : getNextPageParam(options, result)\n if (currentPage > 0 && param == null) {\n break\n }\n result = await fetchPage(result, param)\n currentPage++\n } while (currentPage < remainingPages)\n }\n\n return result\n }\n if (context.options.persister) {\n context.fetchFn = () => {\n return context.options.persister?.(\n fetchFn as any,\n {\n client: context.client,\n queryKey: context.queryKey,\n meta: context.options.meta,\n signal: context.signal,\n },\n query,\n )\n }\n } else {\n context.fetchFn = fetchFn\n }\n },\n }\n}\n\n/**\n * Computes the page param of the page after the last one, with the `getNextPageParam` option.\n * @param options - The infinite query options.\n * @param data - The current pages and page params.\n * @returns The next page param, or `undefined` if there are no pages yet.\n */\nfunction getNextPageParam(\n options: InfiniteQueryPageParamsOptions<any>,\n { pages, pageParams }: InfiniteData<unknown>,\n): unknown | undefined {\n const lastIndex = pages.length - 1\n return pages.length > 0\n ? options.getNextPageParam(\n pages[lastIndex],\n pages,\n pageParams[lastIndex],\n pageParams,\n )\n : undefined\n}\n\n/**\n * Computes the page param of the page before the first one, with the `getPreviousPageParam`\n * option.\n * @param options - The infinite query options.\n * @param data - The current pages and page params.\n * @returns The previous page param, or `undefined` if there are no pages yet or no\n * `getPreviousPageParam` option.\n */\nfunction getPreviousPageParam(\n options: InfiniteQueryPageParamsOptions<any>,\n { pages, pageParams }: InfiniteData<unknown>,\n): unknown | undefined {\n return pages.length > 0\n ? options.getPreviousPageParam?.(pages[0], pages, pageParams[0], pageParams)\n : undefined\n}\n\n/**\n * Checks if there is a next page.\n * @param options - The infinite query options with `getNextPageParam`.\n * @param data - The current pages and page params.\n * @returns `true` if `getNextPageParam` returns a value other than `null` or `undefined`.\n */\nexport function hasNextPage(\n options: InfiniteQueryPageParamsOptions<any, any>,\n data?: InfiniteData<unknown>,\n): boolean {\n if (!data) return false\n return getNextPageParam(options, data) != null\n}\n\n/**\n * Checks if there is a previous page.\n * @param options - The infinite query options with `getPreviousPageParam`.\n * @param data - The current pages and page params.\n * @returns `true` if `getPreviousPageParam` is set and returns a value other than `null` or\n * `undefined`.\n */\nexport function hasPreviousPage(\n options: InfiniteQueryPageParamsOptions<any, any>,\n data?: InfiniteData<unknown>,\n): boolean {\n if (!data || !options.getPreviousPageParam) return false\n return getPreviousPageParam(options, data) != null\n}\n"],"mappings":";;;;;;;;;;AAyBA,SAAgB,sBACd,OACsE;CACtE,OAAO,EACL,UAAU,SAAS,UAAU;EAC3B,MAAM,UAAU,QAAQ;EACxB,MAAM,YAAY,QAAQ,cAAc,MAAM,WAAW;EACzD,MAAM,WAAW,QAAQ,MAAM,MAAM,SAAS,CAAC;EAC/C,MAAM,gBAAgB,QAAQ,MAAM,MAAM,cAAc,CAAC;EACzD,IAAI,SAAgC;GAAE,OAAO,CAAC;GAAG,YAAY,CAAC;EAAE;EAChE,IAAI,cAAc;EAElB,MAAM,UAAU,YAAY;GAC1B,IAAI,YAAY;GAChB,MAAM,gBAAoC;GAC1C,MAAM,qBAAqB,WAAmB;IAC5C,sBACE,cACM,QAAQ,cACP,YAAY,MACnB,cAAc,YAChB;GACF;GAEA,MAAM,UAAU,cAAc,QAAQ,SAAS,QAAQ,YAAY;GAGnE,MAAM,YAAY,OAChB,MACA,OACA,aACmC;IACnC,IAAI,WACF,OAAO,QAAQ,OAAO,QAAQ,OAAO,MAAM;IAG7C,IAAI,SAAS,QAAQ,KAAK,MAAM,QAC9B,OAAO,QAAQ,QAAQ,IAAI;IAG7B,MAAM,6BAA6B;KACjC,MAAM,iBAGF;MACF,QAAQ,QAAQ;MAChB,UAAU,QAAQ;MAClB,WAAW;MACX,WAAW,WAAW,aA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rB,OAAO,MAAM,SAAS,IAClB,QAAQ,uBAAuB,MAAM,IAAI,OAAO,WAAW,IAAI,UAAU,IACzE,KAAA;AACN;;;;;;;AAQA,SAAgB,YACd,SACA,MACS;CACT,IAAI,CAAC,MAAM,OAAO;CAClB,OAAO,iBAAiB,SAAS,IAAI,KAAK;AAC5C;;;;;;;;AASA,SAAgB,gBACd,SACA,MACS;CACT,IAAI,CAAC,QAAQ,CAAC,QAAQ,sBAAsB,OAAO;CACnD,OAAO,qBAAqB,SAAS,IAAI,KAAK;AAChD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as Subscribable } from "./subscribable-D2lAzdDi.cjs";
|
|
2
|
-
import {
|
|
2
|
+
import { A as InfiniteData, C as FetchPreviousPageOptions, Ct as QueryKey, F as InfiniteQueryObserverOptions, S as FetchNextPageOptions, ar as QueryObserver, f as DefaultError, m as DefaultedInfiniteQueryObserverOptions, on as QueryClient, tr as Query, z as InfiniteQueryObserverResult } from "./hydration-BXjqUj_D.cjs";
|
|
3
3
|
type InfiniteQueryObserverListener<TData, TError> = (result: InfiniteQueryObserverResult<TData, TError>) => void;
|
|
4
4
|
/**
|
|
5
5
|
* An `InfiniteQueryObserver` extends `QueryObserver` to observe and switch
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as Subscribable } from "./subscribable-D2lAzdDi.js";
|
|
2
|
-
import {
|
|
2
|
+
import { A as InfiniteData, C as FetchPreviousPageOptions, Ct as QueryKey, F as InfiniteQueryObserverOptions, S as FetchNextPageOptions, ar as QueryObserver, f as DefaultError, m as DefaultedInfiniteQueryObserverOptions, on as QueryClient, tr as Query, z as InfiniteQueryObserverResult } from "./hydration-vgIjIhk6.js";
|
|
3
3
|
type InfiniteQueryObserverListener<TData, TError> = (result: InfiniteQueryObserverResult<TData, TError>) => void;
|
|
4
4
|
/**
|
|
5
5
|
* An `InfiniteQueryObserver` extends `QueryObserver` to observe and switch
|
|
@@ -190,6 +190,7 @@ var Mutation = class extends require_removable.Removable {
|
|
|
190
190
|
isPaused
|
|
191
191
|
});
|
|
192
192
|
}
|
|
193
|
+
if (this.state.isPaused && retryer.canStart()) onContinue();
|
|
193
194
|
const data = await retryer.start();
|
|
194
195
|
await this.#mutationCache.config.onSuccess?.(data, variables, this.state.context, this, mutationFnContext);
|
|
195
196
|
await this.options.onSuccess?.(data, variables, this.state.context, mutationFnContext);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutation.cjs","names":["Removable","createRetryer"],"sources":["../../src/mutation.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { Removable } from './removable'\nimport { createRetryer } from './retryer'\nimport type {\n DefaultError,\n MutationFunctionContext,\n MutationMeta,\n MutationOptions,\n MutationStatus,\n} from './types'\nimport type { MutationCache } from './mutationCache'\nimport type { MutationObserver } from './mutationObserver'\nimport type { Retryer } from './retryer'\nimport type { QueryClient } from './queryClient'\n\n// TYPES\n\ninterface MutationConfig<TData, TError, TVariables, TOnMutateResult> {\n client: QueryClient\n mutationId: number\n mutationCache: MutationCache\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n state?: MutationState<TData, TError, TVariables, TOnMutateResult>\n}\n\n/**\n * The raw state stored on a `Mutation` instance. This is the underlying state\n * that observer results (e.g. `MutationObserverResult`) are derived from.\n */\nexport interface MutationState<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> {\n /**\n * The value returned by `onMutate`, if defined. Passed to `onSuccess`,\n * `onError` and `onSettled` as the mutation's context.\n */\n context: TOnMutateResult | undefined\n /**\n * The last successfully resolved data for the mutation.\n */\n data: TData | undefined\n /**\n * The error object for the mutation, if the last attempt resulted in an error.\n * - Defaults to `null`.\n */\n error: TError | null\n /**\n * The number of times the mutation function has failed for the current attempt.\n */\n failureCount: number\n /**\n * The reason the current attempt failed, as reported by the retryer.\n */\n failureReason: TError | null\n /**\n * Whether the mutation is currently paused (see network mode), or is\n * waiting for another mutation with the same `scope` to finish.\n */\n isPaused: boolean\n /**\n * The status of the mutation.\n */\n status: MutationStatus\n /**\n * The variables the mutation was last called with.\n */\n variables: TVariables | undefined\n /**\n * The timestamp for when the mutation was submitted.\n */\n submittedAt: number\n}\n\ninterface FailedAction<TError> {\n type: 'failed'\n failureCount: number\n error: TError | null\n}\n\ninterface PendingAction<TVariables, TOnMutateResult> {\n type: 'pending'\n isPaused: boolean\n variables?: TVariables\n context?: TOnMutateResult\n}\n\ninterface SuccessAction<TData> {\n type: 'success'\n data: TData\n}\n\ninterface ErrorAction<TError> {\n type: 'error'\n error: TError\n}\n\ninterface PauseAction {\n type: 'pause'\n}\n\ninterface ContinueAction {\n type: 'continue'\n}\n\n/**\n * The actions a mutation dispatches to update its state, e.g. when it starts, fails, pauses, or\n * succeeds.\n */\nexport type Action<TData, TError, TVariables, TOnMutateResult> =\n | ContinueAction\n | ErrorAction<TError>\n | FailedAction<TError>\n | PendingAction<TVariables, TOnMutateResult>\n | PauseAction\n | SuccessAction<TData>\n\n// CLASS\n\n/**\n * Represents a single mutation attempt. A `Mutation` holds the mutation's\n * options, state (data/error/status), and the `MutationObserver`s currently\n * subscribed to it.\n *\n * Instances are created and managed internally by `MutationCache`; application\n * code typically interacts with mutations indirectly through `QueryClient` or\n * a framework hook like `useMutation`. Direct access to a `Mutation` instance\n * is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * ```\n */\nexport class Mutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> extends Removable {\n state: MutationState<TData, TError, TVariables, TOnMutateResult>\n options!: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n readonly mutationId: number\n\n #client: QueryClient\n #observers: Array<\n MutationObserver<TData, TError, TVariables, TOnMutateResult>\n >\n #mutationCache: MutationCache\n #retryer?: Retryer<TData>\n\n constructor(\n config: MutationConfig<TData, TError, TVariables, TOnMutateResult>,\n ) {\n super()\n\n this.#client = config.client\n this.mutationId = config.mutationId\n this.#mutationCache = config.mutationCache\n this.#observers = []\n this.state = config.state || getDefaultState()\n\n this.setOptions(config.options)\n this.scheduleGc()\n }\n\n /**\n * Replaces the mutation's options and applies their `gcTime`.\n * @internal\n * @param options - The new mutation options.\n */\n setOptions(\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.options = options\n\n this.updateGcTime(this.options.gcTime)\n }\n\n /**\n * The `meta` object passed in the mutation's options, if any.\n * @returns The mutation's `meta`, or `undefined` if none was set.\n */\n get meta(): MutationMeta | undefined {\n return this.options.meta\n }\n\n /**\n * Subscribes an observer to the mutation and stops its garbage collection.\n * @internal\n * @param observer - The observer to add. Adding an observer that is already subscribed does\n * nothing.\n */\n addObserver(observer: MutationObserver<any, any, any, any>): void {\n if (!this.#observers.includes(observer)) {\n this.#observers.push(observer)\n\n // Stop the mutation from being garbage collected\n this.clearGcTimeout()\n\n this.#mutationCache.notify({\n type: 'observerAdded',\n mutation: this,\n observer,\n })\n }\n }\n\n /**\n * Unsubscribes an observer from the mutation.\n * @internal\n * @param observer - The observer to remove. Garbage collection is scheduled afterwards.\n */\n removeObserver(observer: MutationObserver<any, any, any, any>): void {\n this.#observers = this.#observers.filter((x) => x !== observer)\n\n this.scheduleGc()\n\n this.#mutationCache.notify({\n type: 'observerRemoved',\n mutation: this,\n observer,\n })\n }\n\n protected override optionalRemove() {\n if (!this.#observers.length) {\n if (this.state.status === 'pending') {\n this.scheduleGc()\n } else {\n this.#mutationCache.remove(this)\n }\n }\n }\n\n /**\n * Resumes a mutation that is currently paused or was restored from a\n * dehydrated, still-`pending` state.\n *\n * - If this mutation has an active retryer (it paused mid-attempt, e.g. due\n * to the network mode or scope-based queuing), its retryer is resumed.\n * - Otherwise, if the mutation's status is still `pending` (e.g. it was\n * dehydrated while an attempt was in flight and never got a retryer in\n * this instance), `execute` is called again with the last known variables.\n * - Otherwise the mutation has already settled and this resolves immediately\n * without running anything again.\n * @returns A promise that settles with the resumed mutation: it rejects if the mutation fails.\n * It resolves immediately if the mutation has already settled.\n * @see {@link Mutation#execute}\n * @example\n * ```ts\n * // typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * await mutation?.continue()\n * ```\n */\n continue(): Promise<unknown> {\n return (\n this.#retryer?.continue() ??\n // continuing a mutation assumes that variables are set, mutation must have been dehydrated before.\n // a settled mutation has no retryer to continue and must not run again\n (this.state.status === 'pending'\n ? this.execute(this.state.variables!)\n : Promise.resolve())\n )\n }\n\n /**\n * Runs the mutation function for the given variables through a retryer, and\n * drives the mutation's state and lifecycle callbacks through to settlement.\n *\n * If this mutation's state is already `pending` when `execute` is called\n * (i.e. it was restored, still in-flight, from a dehydrated state), the\n * `onMutate` step is skipped and a `continue` action is dispatched to\n * unpause it; otherwise a `pending` action is dispatched first, then the\n * mutation cache's `onMutate` and the mutation's own `onMutate` option are\n * awaited in that order, and the resulting context is stored.\n *\n * The mutation function is then run (subject to `retry`/`retryDelay`/\n * `networkMode`, and to the mutation cache's scope-based serialization).\n * On success, the cache's `onSuccess`/`onSettled` callbacks run before the\n * mutation's own `onSuccess`/`onSettled` options, a `success` action is\n * dispatched, and the resolved data is returned. On failure, the same\n * cache-then-option ordering is used for `onError`/`onSettled`, but each of\n * those four callbacks is individually caught so that a throwing callback\n * cannot mask the original error; an `error` action is then dispatched and\n * the original error is re-thrown.\n * @param variables - The variables passed to the `mutationFn`.\n * @returns A promise that resolves with the mutation's data, or rejects with its error.\n * @see {@link Mutation#continue}\n * @example\n * ```ts\n * // Called internally by `MutationObserver.mutate` and `Mutation.continue` —\n * // applications normally trigger mutations through those, not this method.\n * const data = await mutation.execute(variables)\n * ```\n */\n async execute(variables: TVariables): Promise<TData> {\n const onContinue = () => {\n this.#dispatch({ type: 'continue' })\n }\n\n const mutationFnContext = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n const retryer = (this.#retryer = createRetryer({\n fn: () => {\n if (!this.options.mutationFn) {\n return Promise.reject(new Error('No mutationFn found'))\n }\n\n return this.options.mutationFn(variables, mutationFnContext)\n },\n onFail: (failureCount, error) => {\n this.#dispatch({ type: 'failed', failureCount, error })\n },\n onPause: () => {\n this.#dispatch({ type: 'pause' })\n },\n onContinue,\n retry: this.options.retry ?? 0,\n retryDelay: this.options.retryDelay,\n networkMode: this.options.networkMode,\n canRun: () => this.#mutationCache.canRun(this),\n }))\n\n const restored = this.state.status === 'pending'\n const isPaused = !retryer.canStart()\n\n try {\n if (restored) {\n // Dispatch continue action to unpause restored mutation\n onContinue()\n } else {\n this.#dispatch({ type: 'pending', variables, isPaused })\n // Notify cache callback\n if (this.#mutationCache.config.onMutate) {\n await this.#mutationCache.config.onMutate(\n variables,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n }\n const context = await this.options.onMutate?.(\n variables,\n mutationFnContext,\n )\n if (context !== this.state.context) {\n this.#dispatch({\n type: 'pending',\n context,\n variables,\n isPaused,\n })\n }\n }\n const data = await retryer.start()\n\n // Notify cache callback\n await this.#mutationCache.config.onSuccess?.(\n data,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSuccess?.(\n data,\n variables,\n this.state.context!,\n mutationFnContext,\n )\n\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n data,\n null,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSettled?.(\n data,\n null,\n variables,\n this.state.context,\n mutationFnContext,\n )\n\n this.#dispatch({ type: 'success', data })\n return data\n } catch (error) {\n try {\n // Notify cache callback\n await this.#mutationCache.config.onError?.(\n error as any,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onError?.(\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n undefined,\n error as any,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onSettled?.(\n undefined,\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n this.#dispatch({ type: 'error', error: error as TError })\n throw error\n } finally {\n // The settled retryer's promise would otherwise pin this mutation's\n // result, variables and context for as long as the cache keeps it\n if (this.#retryer === retryer) {\n this.#retryer = undefined\n }\n this.#mutationCache.runNext(this)\n }\n }\n\n #dispatch(action: Action<TData, TError, TVariables, TOnMutateResult>): void {\n const reducer = (\n state: MutationState<TData, TError, TVariables, TOnMutateResult>,\n ): MutationState<TData, TError, TVariables, TOnMutateResult> => {\n switch (action.type) {\n case 'failed':\n return {\n ...state,\n failureCount: action.failureCount,\n failureReason: action.error,\n }\n case 'pause':\n return {\n ...state,\n isPaused: true,\n }\n case 'continue':\n return {\n ...state,\n isPaused: false,\n }\n case 'pending':\n return {\n ...state,\n context: action.context,\n data: undefined,\n failureCount: 0,\n failureReason: null,\n error: null,\n isPaused: action.isPaused,\n status: 'pending',\n variables: action.variables,\n submittedAt: Date.now(),\n }\n case 'success':\n return {\n ...state,\n data: action.data,\n failureCount: 0,\n failureReason: null,\n error: null,\n status: 'success',\n isPaused: false,\n }\n case 'error':\n return {\n ...state,\n data: undefined,\n error: action.error,\n failureCount: state.failureCount + 1,\n failureReason: action.error,\n isPaused: false,\n status: 'error',\n }\n }\n }\n this.state = reducer(this.state)\n\n notifyManager.batch(() => {\n this.#observers.forEach((observer) => {\n observer.onMutationUpdate(action)\n })\n this.#mutationCache.notify({\n mutation: this,\n type: 'updated',\n action,\n })\n })\n }\n}\n\n/**\n * Returns the initial state of a mutation: `'idle'`, with no data, error, or variables.\n * @returns The initial mutation state.\n */\nexport function getDefaultState<\n TData,\n TError,\n TVariables,\n TOnMutateResult,\n>(): MutationState<TData, TError, TVariables, TOnMutateResult> {\n return {\n context: undefined,\n data: undefined,\n error: null,\n failureCount: 0,\n failureReason: null,\n isPaused: false,\n status: 'idle',\n variables: undefined,\n submittedAt: 0,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAyIA,IAAa,WAAb,cAKUA,kBAAAA,UAAU;CAKlB;CACA;CAGA;CACA;CAEA,YACE,QACA;EACA,MAAM;EAEN,KAAK,UAAU,OAAO;EACtB,KAAK,aAAa,OAAO;EACzB,KAAK,iBAAiB,OAAO;EAC7B,KAAK,aAAa,CAAC;EACnB,KAAK,QAAQ,OAAO,SAAS,gBAAgB;EAE7C,KAAK,WAAW,OAAO,OAAO;EAC9B,KAAK,WAAW;CAClB;;;;;;CAOA,WACE,SACM;EACN,KAAK,UAAU;EAEf,KAAK,aAAa,KAAK,QAAQ,MAAM;CACvC;;;;;CAMA,IAAI,OAAiC;EACnC,OAAO,KAAK,QAAQ;CACtB;;;;;;;CAQA,YAAY,UAAsD;EAChE,IAAI,CAAC,KAAK,WAAW,SAAS,QAAQ,GAAG;GACvC,KAAK,WAAW,KAAK,QAAQ;GAG7B,KAAK,eAAe;GAEpB,KAAK,eAAe,OAAO;IACzB,MAAM;IACN,UAAU;IACV;GACF,CAAC;EACH;CACF;;;;;;CAOA,eAAe,UAAsD;EACnE,KAAK,aAAa,KAAK,WAAW,QAAQ,MAAM,MAAM,QAAQ;EAE9D,KAAK,WAAW;EAEhB,KAAK,eAAe,OAAO;GACzB,MAAM;GACN,UAAU;GACV;EACF,CAAC;CACH;CAEA,iBAAoC;EAClC,IAAI,CAAC,KAAK,WAAW,QAAQ;GAC3B,IAAI,KAAK,MAAM,WAAW,WACxB,KAAK,WAAW;QAEhB,KAAK,eAAe,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AAf;IACE,KAAK,UACH,OAAO;KACL,GAAG;KACH,cAAc,OAAO;KACrB,eAAe,OAAO;IACxB;IACF,KAAK,SACH,OAAO;KACL,GAAG;KACH,UAAU;IACZ;IACF,KAAK,YACH,OAAO;KACL,GAAG;KACH,UAAU;IACZ;IACF,KAAK,WACH,OAAO;KACL,GAAG;KACH,SAAS,OAAO;KAChB,MAAM,KAAA;KACN,cAAc;KACd,eAAe;KACf,OAAO;KACP,UAAU,OAAO;KACjB,QAAQ;KACR,WAAW,OAAO;KAClB,aAAa,KAAK,IAAI;IACxB;IACF,KAAK,WACH,OAAO;KACL,GAAG;KACH,MAAM,OAAO;KACb,cAAc;KACd,eAAe;KACf,OAAO;KACP,QAAQ;KACR,UAAU;IACZ;IACF,KAAK,SACH,OAAO;KACL,GAAG;KACH,MAAM,KAAA;KACN,OAAO,OAAO;KACd,cAAc,MAAM,eAAe;KACnC,eAAe,OAAO;KACtB,UAAU;KACV,QAAQ;IACV;GACJ;EACF;EACA,KAAK,QAAQ,QAAQ,KAAK,KAAK;EAE/B,sBAAA,cAAc,YAAY;GACxB,KAAK,WAAW,SAAS,aAAa;IACpC,SAAS,iBAAiB,MAAM;GAClC,CAAC;GACD,KAAK,eAAe,OAAO;IACzB,UAAU;IACV,MAAM;IACN;GACF,CAAC;EACH,CAAC;CACH;AACF;;;;;AAMA,SAAgB,kBAK+C;CAC7D,OAAO;EACL,SAAS,KAAA;EACT,MAAM,KAAA;EACN,OAAO;EACP,cAAc;EACd,eAAe;EACf,UAAU;EACV,QAAQ;EACR,WAAW,KAAA;EACX,aAAa;CACf;AACF"}
|
|
1
|
+
{"version":3,"file":"mutation.cjs","names":["Removable","createRetryer"],"sources":["../../src/mutation.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { Removable } from './removable'\nimport { createRetryer } from './retryer'\nimport type {\n DefaultError,\n MutationFunctionContext,\n MutationMeta,\n MutationOptions,\n MutationStatus,\n} from './types'\nimport type { MutationCache } from './mutationCache'\nimport type { MutationObserver } from './mutationObserver'\nimport type { Retryer } from './retryer'\nimport type { QueryClient } from './queryClient'\n\n// TYPES\n\ninterface MutationConfig<TData, TError, TVariables, TOnMutateResult> {\n client: QueryClient\n mutationId: number\n mutationCache: MutationCache\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n state?: MutationState<TData, TError, TVariables, TOnMutateResult>\n}\n\n/**\n * The raw state stored on a `Mutation` instance. This is the underlying state\n * that observer results (e.g. `MutationObserverResult`) are derived from.\n */\nexport interface MutationState<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> {\n /**\n * The value returned by `onMutate`, if defined. Passed to `onSuccess`,\n * `onError` and `onSettled` as the mutation's context.\n */\n context: TOnMutateResult | undefined\n /**\n * The last successfully resolved data for the mutation.\n */\n data: TData | undefined\n /**\n * The error object for the mutation, if the last attempt resulted in an error.\n * - Defaults to `null`.\n */\n error: TError | null\n /**\n * The number of times the mutation function has failed for the current attempt.\n */\n failureCount: number\n /**\n * The reason the current attempt failed, as reported by the retryer.\n */\n failureReason: TError | null\n /**\n * Whether the mutation is currently paused (see network mode), or is\n * waiting for another mutation with the same `scope` to finish.\n */\n isPaused: boolean\n /**\n * The status of the mutation.\n */\n status: MutationStatus\n /**\n * The variables the mutation was last called with.\n */\n variables: TVariables | undefined\n /**\n * The timestamp for when the mutation was submitted.\n */\n submittedAt: number\n}\n\ninterface FailedAction<TError> {\n type: 'failed'\n failureCount: number\n error: TError | null\n}\n\ninterface PendingAction<TVariables, TOnMutateResult> {\n type: 'pending'\n isPaused: boolean\n variables?: TVariables\n context?: TOnMutateResult\n}\n\ninterface SuccessAction<TData> {\n type: 'success'\n data: TData\n}\n\ninterface ErrorAction<TError> {\n type: 'error'\n error: TError\n}\n\ninterface PauseAction {\n type: 'pause'\n}\n\ninterface ContinueAction {\n type: 'continue'\n}\n\n/**\n * The actions a mutation dispatches to update its state, e.g. when it starts, fails, pauses, or\n * succeeds.\n */\nexport type Action<TData, TError, TVariables, TOnMutateResult> =\n | ContinueAction\n | ErrorAction<TError>\n | FailedAction<TError>\n | PendingAction<TVariables, TOnMutateResult>\n | PauseAction\n | SuccessAction<TData>\n\n// CLASS\n\n/**\n * Represents a single mutation attempt. A `Mutation` holds the mutation's\n * options, state (data/error/status), and the `MutationObserver`s currently\n * subscribed to it.\n *\n * Instances are created and managed internally by `MutationCache`; application\n * code typically interacts with mutations indirectly through `QueryClient` or\n * a framework hook like `useMutation`. Direct access to a `Mutation` instance\n * is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * ```\n */\nexport class Mutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> extends Removable {\n state: MutationState<TData, TError, TVariables, TOnMutateResult>\n options!: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n readonly mutationId: number\n\n #client: QueryClient\n #observers: Array<\n MutationObserver<TData, TError, TVariables, TOnMutateResult>\n >\n #mutationCache: MutationCache\n #retryer?: Retryer<TData>\n\n constructor(\n config: MutationConfig<TData, TError, TVariables, TOnMutateResult>,\n ) {\n super()\n\n this.#client = config.client\n this.mutationId = config.mutationId\n this.#mutationCache = config.mutationCache\n this.#observers = []\n this.state = config.state || getDefaultState()\n\n this.setOptions(config.options)\n this.scheduleGc()\n }\n\n /**\n * Replaces the mutation's options and applies their `gcTime`.\n * @internal\n * @param options - The new mutation options.\n */\n setOptions(\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.options = options\n\n this.updateGcTime(this.options.gcTime)\n }\n\n /**\n * The `meta` object passed in the mutation's options, if any.\n * @returns The mutation's `meta`, or `undefined` if none was set.\n */\n get meta(): MutationMeta | undefined {\n return this.options.meta\n }\n\n /**\n * Subscribes an observer to the mutation and stops its garbage collection.\n * @internal\n * @param observer - The observer to add. Adding an observer that is already subscribed does\n * nothing.\n */\n addObserver(observer: MutationObserver<any, any, any, any>): void {\n if (!this.#observers.includes(observer)) {\n this.#observers.push(observer)\n\n // Stop the mutation from being garbage collected\n this.clearGcTimeout()\n\n this.#mutationCache.notify({\n type: 'observerAdded',\n mutation: this,\n observer,\n })\n }\n }\n\n /**\n * Unsubscribes an observer from the mutation.\n * @internal\n * @param observer - The observer to remove. Garbage collection is scheduled afterwards.\n */\n removeObserver(observer: MutationObserver<any, any, any, any>): void {\n this.#observers = this.#observers.filter((x) => x !== observer)\n\n this.scheduleGc()\n\n this.#mutationCache.notify({\n type: 'observerRemoved',\n mutation: this,\n observer,\n })\n }\n\n protected override optionalRemove() {\n if (!this.#observers.length) {\n if (this.state.status === 'pending') {\n this.scheduleGc()\n } else {\n this.#mutationCache.remove(this)\n }\n }\n }\n\n /**\n * Resumes a mutation that is currently paused or was restored from a\n * dehydrated, still-`pending` state.\n *\n * - If this mutation has an active retryer (it paused mid-attempt, e.g. due\n * to the network mode or scope-based queuing), its retryer is resumed.\n * - Otherwise, if the mutation's status is still `pending` (e.g. it was\n * dehydrated while an attempt was in flight and never got a retryer in\n * this instance), `execute` is called again with the last known variables.\n * - Otherwise the mutation has already settled and this resolves immediately\n * without running anything again.\n * @returns A promise that settles with the resumed mutation: it rejects if the mutation fails.\n * It resolves immediately if the mutation has already settled.\n * @see {@link Mutation#execute}\n * @example\n * ```ts\n * // typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * await mutation?.continue()\n * ```\n */\n continue(): Promise<unknown> {\n return (\n this.#retryer?.continue() ??\n // continuing a mutation assumes that variables are set, mutation must have been dehydrated before.\n // a settled mutation has no retryer to continue and must not run again\n (this.state.status === 'pending'\n ? this.execute(this.state.variables!)\n : Promise.resolve())\n )\n }\n\n /**\n * Runs the mutation function for the given variables through a retryer, and\n * drives the mutation's state and lifecycle callbacks through to settlement.\n *\n * If this mutation's state is already `pending` when `execute` is called\n * (i.e. it was restored, still in-flight, from a dehydrated state), the\n * `onMutate` step is skipped and a `continue` action is dispatched to\n * unpause it; otherwise a `pending` action is dispatched first, then the\n * mutation cache's `onMutate` and the mutation's own `onMutate` option are\n * awaited in that order, and the resulting context is stored.\n *\n * The mutation function is then run (subject to `retry`/`retryDelay`/\n * `networkMode`, and to the mutation cache's scope-based serialization).\n * On success, the cache's `onSuccess`/`onSettled` callbacks run before the\n * mutation's own `onSuccess`/`onSettled` options, a `success` action is\n * dispatched, and the resolved data is returned. On failure, the same\n * cache-then-option ordering is used for `onError`/`onSettled`, but each of\n * those four callbacks is individually caught so that a throwing callback\n * cannot mask the original error; an `error` action is then dispatched and\n * the original error is re-thrown.\n * @param variables - The variables passed to the `mutationFn`.\n * @returns A promise that resolves with the mutation's data, or rejects with its error.\n * @see {@link Mutation#continue}\n * @example\n * ```ts\n * // Called internally by `MutationObserver.mutate` and `Mutation.continue` —\n * // applications normally trigger mutations through those, not this method.\n * const data = await mutation.execute(variables)\n * ```\n */\n async execute(variables: TVariables): Promise<TData> {\n const onContinue = () => {\n this.#dispatch({ type: 'continue' })\n }\n\n const mutationFnContext = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n const retryer = (this.#retryer = createRetryer({\n fn: () => {\n if (!this.options.mutationFn) {\n return Promise.reject(new Error('No mutationFn found'))\n }\n\n return this.options.mutationFn(variables, mutationFnContext)\n },\n onFail: (failureCount, error) => {\n this.#dispatch({ type: 'failed', failureCount, error })\n },\n onPause: () => {\n this.#dispatch({ type: 'pause' })\n },\n onContinue,\n retry: this.options.retry ?? 0,\n retryDelay: this.options.retryDelay,\n networkMode: this.options.networkMode,\n canRun: () => this.#mutationCache.canRun(this),\n }))\n\n const restored = this.state.status === 'pending'\n const isPaused = !retryer.canStart()\n\n try {\n if (restored) {\n // Dispatch continue action to unpause restored mutation\n onContinue()\n } else {\n this.#dispatch({ type: 'pending', variables, isPaused })\n // Notify cache callback\n if (this.#mutationCache.config.onMutate) {\n await this.#mutationCache.config.onMutate(\n variables,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n }\n const context = await this.options.onMutate?.(\n variables,\n mutationFnContext,\n )\n if (context !== this.state.context) {\n this.#dispatch({\n type: 'pending',\n context,\n variables,\n isPaused,\n })\n }\n }\n // Unpause a mutation that became able to start while onMutate was running\n if (this.state.isPaused && retryer.canStart()) {\n onContinue()\n }\n const data = await retryer.start()\n\n // Notify cache callback\n await this.#mutationCache.config.onSuccess?.(\n data,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSuccess?.(\n data,\n variables,\n this.state.context!,\n mutationFnContext,\n )\n\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n data,\n null,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSettled?.(\n data,\n null,\n variables,\n this.state.context,\n mutationFnContext,\n )\n\n this.#dispatch({ type: 'success', data })\n return data\n } catch (error) {\n try {\n // Notify cache callback\n await this.#mutationCache.config.onError?.(\n error as any,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onError?.(\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n undefined,\n error as any,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onSettled?.(\n undefined,\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n this.#dispatch({ type: 'error', error: error as TError })\n throw error\n } finally {\n // The settled retryer's promise would otherwise pin this mutation's\n // result, variables and context for as long as the cache keeps it\n if (this.#retryer === retryer) {\n this.#retryer = undefined\n }\n this.#mutationCache.runNext(this)\n }\n }\n\n #dispatch(action: Action<TData, TError, TVariables, TOnMutateResult>): void {\n const reducer = (\n state: MutationState<TData, TError, TVariables, TOnMutateResult>,\n ): MutationState<TData, TError, TVariables, TOnMutateResult> => {\n switch (action.type) {\n case 'failed':\n return {\n ...state,\n failureCount: action.failureCount,\n failureReason: action.error,\n }\n case 'pause':\n return {\n ...state,\n isPaused: true,\n }\n case 'continue':\n return {\n ...state,\n isPaused: false,\n }\n case 'pending':\n return {\n ...state,\n context: action.context,\n data: undefined,\n failureCount: 0,\n failureReason: null,\n error: null,\n isPaused: action.isPaused,\n status: 'pending',\n variables: action.variables,\n submittedAt: Date.now(),\n }\n case 'success':\n return {\n ...state,\n data: action.data,\n failureCount: 0,\n failureReason: null,\n error: null,\n status: 'success',\n isPaused: false,\n }\n case 'error':\n return {\n ...state,\n data: undefined,\n error: action.error,\n failureCount: state.failureCount + 1,\n failureReason: action.error,\n isPaused: false,\n status: 'error',\n }\n }\n }\n this.state = reducer(this.state)\n\n notifyManager.batch(() => {\n this.#observers.forEach((observer) => {\n observer.onMutationUpdate(action)\n })\n this.#mutationCache.notify({\n mutation: this,\n type: 'updated',\n action,\n })\n })\n }\n}\n\n/**\n * Returns the initial state of a mutation: `'idle'`, with no data, error, or variables.\n * @returns The initial mutation state.\n */\nexport function getDefaultState<\n TData,\n TError,\n TVariables,\n TOnMutateResult,\n>(): MutationState<TData, TError, TVariables, TOnMutateResult> {\n return {\n context: undefined,\n data: undefined,\n error: null,\n failureCount: 0,\n failureReason: null,\n isPaused: false,\n status: 'idle',\n variables: undefined,\n submittedAt: 0,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAyIA,IAAa,WAAb,cAKUA,kBAAAA,UAAU;CAKlB;CACA;CAGA;CACA;CAEA,YACE,QACA;EACA,MAAM;EAEN,KAAK,UAAU,OAAO;EACtB,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AAG;KACH,MAAM,OAAO;KACb,cAAc;KACd,eAAe;KACf,OAAO;KACP,QAAQ;KACR,UAAU;IACZ;IACF,KAAK,SACH,OAAO;KACL,GAAG;KACH,MAAM,KAAA;KACN,OAAO,OAAO;KACd,cAAc,MAAM,eAAe;KACnC,eAAe,OAAO;KACtB,UAAU;KACV,QAAQ;IACV;GACJ;EACF;EACA,KAAK,QAAQ,QAAQ,KAAK,KAAK;EAE/B,sBAAA,cAAc,YAAY;GACxB,KAAK,WAAW,SAAS,aAAa;IACpC,SAAS,iBAAiB,MAAM;GAClC,CAAC;GACD,KAAK,eAAe,OAAO;IACzB,UAAU;IACV,MAAM;IACN;GACF,CAAC;EACH,CAAC;CACH;AACF;;;;;AAMA,SAAgB,kBAK+C;CAC7D,OAAO;EACL,SAAS,KAAA;EACT,MAAM,KAAA;EACN,OAAO;EACP,cAAc;EACd,eAAe;EACf,UAAU;EACV,QAAQ;EACR,WAAW,KAAA;EACX,aAAa;CACf;AACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Gn as getDefaultState, Hn as Action, Un as Mutation, Wn as MutationState } from "./hydration-BXjqUj_D.cjs";
|
|
2
2
|
export { Action, Mutation, MutationState, getDefaultState };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Gn as getDefaultState, Hn as Action, Un as Mutation, Wn as MutationState } from "./hydration-vgIjIhk6.js";
|
|
2
2
|
export { Action, Mutation, MutationState, getDefaultState };
|
package/build/modern/mutation.js
CHANGED
|
@@ -189,6 +189,7 @@ var Mutation = class extends Removable {
|
|
|
189
189
|
isPaused
|
|
190
190
|
});
|
|
191
191
|
}
|
|
192
|
+
if (this.state.isPaused && retryer.canStart()) onContinue();
|
|
192
193
|
const data = await retryer.start();
|
|
193
194
|
await this.#mutationCache.config.onSuccess?.(data, variables, this.state.context, this, mutationFnContext);
|
|
194
195
|
await this.options.onSuccess?.(data, variables, this.state.context, mutationFnContext);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutation.js","names":[],"sources":["../../src/mutation.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { Removable } from './removable'\nimport { createRetryer } from './retryer'\nimport type {\n DefaultError,\n MutationFunctionContext,\n MutationMeta,\n MutationOptions,\n MutationStatus,\n} from './types'\nimport type { MutationCache } from './mutationCache'\nimport type { MutationObserver } from './mutationObserver'\nimport type { Retryer } from './retryer'\nimport type { QueryClient } from './queryClient'\n\n// TYPES\n\ninterface MutationConfig<TData, TError, TVariables, TOnMutateResult> {\n client: QueryClient\n mutationId: number\n mutationCache: MutationCache\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n state?: MutationState<TData, TError, TVariables, TOnMutateResult>\n}\n\n/**\n * The raw state stored on a `Mutation` instance. This is the underlying state\n * that observer results (e.g. `MutationObserverResult`) are derived from.\n */\nexport interface MutationState<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> {\n /**\n * The value returned by `onMutate`, if defined. Passed to `onSuccess`,\n * `onError` and `onSettled` as the mutation's context.\n */\n context: TOnMutateResult | undefined\n /**\n * The last successfully resolved data for the mutation.\n */\n data: TData | undefined\n /**\n * The error object for the mutation, if the last attempt resulted in an error.\n * - Defaults to `null`.\n */\n error: TError | null\n /**\n * The number of times the mutation function has failed for the current attempt.\n */\n failureCount: number\n /**\n * The reason the current attempt failed, as reported by the retryer.\n */\n failureReason: TError | null\n /**\n * Whether the mutation is currently paused (see network mode), or is\n * waiting for another mutation with the same `scope` to finish.\n */\n isPaused: boolean\n /**\n * The status of the mutation.\n */\n status: MutationStatus\n /**\n * The variables the mutation was last called with.\n */\n variables: TVariables | undefined\n /**\n * The timestamp for when the mutation was submitted.\n */\n submittedAt: number\n}\n\ninterface FailedAction<TError> {\n type: 'failed'\n failureCount: number\n error: TError | null\n}\n\ninterface PendingAction<TVariables, TOnMutateResult> {\n type: 'pending'\n isPaused: boolean\n variables?: TVariables\n context?: TOnMutateResult\n}\n\ninterface SuccessAction<TData> {\n type: 'success'\n data: TData\n}\n\ninterface ErrorAction<TError> {\n type: 'error'\n error: TError\n}\n\ninterface PauseAction {\n type: 'pause'\n}\n\ninterface ContinueAction {\n type: 'continue'\n}\n\n/**\n * The actions a mutation dispatches to update its state, e.g. when it starts, fails, pauses, or\n * succeeds.\n */\nexport type Action<TData, TError, TVariables, TOnMutateResult> =\n | ContinueAction\n | ErrorAction<TError>\n | FailedAction<TError>\n | PendingAction<TVariables, TOnMutateResult>\n | PauseAction\n | SuccessAction<TData>\n\n// CLASS\n\n/**\n * Represents a single mutation attempt. A `Mutation` holds the mutation's\n * options, state (data/error/status), and the `MutationObserver`s currently\n * subscribed to it.\n *\n * Instances are created and managed internally by `MutationCache`; application\n * code typically interacts with mutations indirectly through `QueryClient` or\n * a framework hook like `useMutation`. Direct access to a `Mutation` instance\n * is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * ```\n */\nexport class Mutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> extends Removable {\n state: MutationState<TData, TError, TVariables, TOnMutateResult>\n options!: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n readonly mutationId: number\n\n #client: QueryClient\n #observers: Array<\n MutationObserver<TData, TError, TVariables, TOnMutateResult>\n >\n #mutationCache: MutationCache\n #retryer?: Retryer<TData>\n\n constructor(\n config: MutationConfig<TData, TError, TVariables, TOnMutateResult>,\n ) {\n super()\n\n this.#client = config.client\n this.mutationId = config.mutationId\n this.#mutationCache = config.mutationCache\n this.#observers = []\n this.state = config.state || getDefaultState()\n\n this.setOptions(config.options)\n this.scheduleGc()\n }\n\n /**\n * Replaces the mutation's options and applies their `gcTime`.\n * @internal\n * @param options - The new mutation options.\n */\n setOptions(\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.options = options\n\n this.updateGcTime(this.options.gcTime)\n }\n\n /**\n * The `meta` object passed in the mutation's options, if any.\n * @returns The mutation's `meta`, or `undefined` if none was set.\n */\n get meta(): MutationMeta | undefined {\n return this.options.meta\n }\n\n /**\n * Subscribes an observer to the mutation and stops its garbage collection.\n * @internal\n * @param observer - The observer to add. Adding an observer that is already subscribed does\n * nothing.\n */\n addObserver(observer: MutationObserver<any, any, any, any>): void {\n if (!this.#observers.includes(observer)) {\n this.#observers.push(observer)\n\n // Stop the mutation from being garbage collected\n this.clearGcTimeout()\n\n this.#mutationCache.notify({\n type: 'observerAdded',\n mutation: this,\n observer,\n })\n }\n }\n\n /**\n * Unsubscribes an observer from the mutation.\n * @internal\n * @param observer - The observer to remove. Garbage collection is scheduled afterwards.\n */\n removeObserver(observer: MutationObserver<any, any, any, any>): void {\n this.#observers = this.#observers.filter((x) => x !== observer)\n\n this.scheduleGc()\n\n this.#mutationCache.notify({\n type: 'observerRemoved',\n mutation: this,\n observer,\n })\n }\n\n protected override optionalRemove() {\n if (!this.#observers.length) {\n if (this.state.status === 'pending') {\n this.scheduleGc()\n } else {\n this.#mutationCache.remove(this)\n }\n }\n }\n\n /**\n * Resumes a mutation that is currently paused or was restored from a\n * dehydrated, still-`pending` state.\n *\n * - If this mutation has an active retryer (it paused mid-attempt, e.g. due\n * to the network mode or scope-based queuing), its retryer is resumed.\n * - Otherwise, if the mutation's status is still `pending` (e.g. it was\n * dehydrated while an attempt was in flight and never got a retryer in\n * this instance), `execute` is called again with the last known variables.\n * - Otherwise the mutation has already settled and this resolves immediately\n * without running anything again.\n * @returns A promise that settles with the resumed mutation: it rejects if the mutation fails.\n * It resolves immediately if the mutation has already settled.\n * @see {@link Mutation#execute}\n * @example\n * ```ts\n * // typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * await mutation?.continue()\n * ```\n */\n continue(): Promise<unknown> {\n return (\n this.#retryer?.continue() ??\n // continuing a mutation assumes that variables are set, mutation must have been dehydrated before.\n // a settled mutation has no retryer to continue and must not run again\n (this.state.status === 'pending'\n ? this.execute(this.state.variables!)\n : Promise.resolve())\n )\n }\n\n /**\n * Runs the mutation function for the given variables through a retryer, and\n * drives the mutation's state and lifecycle callbacks through to settlement.\n *\n * If this mutation's state is already `pending` when `execute` is called\n * (i.e. it was restored, still in-flight, from a dehydrated state), the\n * `onMutate` step is skipped and a `continue` action is dispatched to\n * unpause it; otherwise a `pending` action is dispatched first, then the\n * mutation cache's `onMutate` and the mutation's own `onMutate` option are\n * awaited in that order, and the resulting context is stored.\n *\n * The mutation function is then run (subject to `retry`/`retryDelay`/\n * `networkMode`, and to the mutation cache's scope-based serialization).\n * On success, the cache's `onSuccess`/`onSettled` callbacks run before the\n * mutation's own `onSuccess`/`onSettled` options, a `success` action is\n * dispatched, and the resolved data is returned. On failure, the same\n * cache-then-option ordering is used for `onError`/`onSettled`, but each of\n * those four callbacks is individually caught so that a throwing callback\n * cannot mask the original error; an `error` action is then dispatched and\n * the original error is re-thrown.\n * @param variables - The variables passed to the `mutationFn`.\n * @returns A promise that resolves with the mutation's data, or rejects with its error.\n * @see {@link Mutation#continue}\n * @example\n * ```ts\n * // Called internally by `MutationObserver.mutate` and `Mutation.continue` —\n * // applications normally trigger mutations through those, not this method.\n * const data = await mutation.execute(variables)\n * ```\n */\n async execute(variables: TVariables): Promise<TData> {\n const onContinue = () => {\n this.#dispatch({ type: 'continue' })\n }\n\n const mutationFnContext = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n const retryer = (this.#retryer = createRetryer({\n fn: () => {\n if (!this.options.mutationFn) {\n return Promise.reject(new Error('No mutationFn found'))\n }\n\n return this.options.mutationFn(variables, mutationFnContext)\n },\n onFail: (failureCount, error) => {\n this.#dispatch({ type: 'failed', failureCount, error })\n },\n onPause: () => {\n this.#dispatch({ type: 'pause' })\n },\n onContinue,\n retry: this.options.retry ?? 0,\n retryDelay: this.options.retryDelay,\n networkMode: this.options.networkMode,\n canRun: () => this.#mutationCache.canRun(this),\n }))\n\n const restored = this.state.status === 'pending'\n const isPaused = !retryer.canStart()\n\n try {\n if (restored) {\n // Dispatch continue action to unpause restored mutation\n onContinue()\n } else {\n this.#dispatch({ type: 'pending', variables, isPaused })\n // Notify cache callback\n if (this.#mutationCache.config.onMutate) {\n await this.#mutationCache.config.onMutate(\n variables,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n }\n const context = await this.options.onMutate?.(\n variables,\n mutationFnContext,\n )\n if (context !== this.state.context) {\n this.#dispatch({\n type: 'pending',\n context,\n variables,\n isPaused,\n })\n }\n }\n const data = await retryer.start()\n\n // Notify cache callback\n await this.#mutationCache.config.onSuccess?.(\n data,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSuccess?.(\n data,\n variables,\n this.state.context!,\n mutationFnContext,\n )\n\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n data,\n null,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSettled?.(\n data,\n null,\n variables,\n this.state.context,\n mutationFnContext,\n )\n\n this.#dispatch({ type: 'success', data })\n return data\n } catch (error) {\n try {\n // Notify cache callback\n await this.#mutationCache.config.onError?.(\n error as any,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onError?.(\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n undefined,\n error as any,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onSettled?.(\n undefined,\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n this.#dispatch({ type: 'error', error: error as TError })\n throw error\n } finally {\n // The settled retryer's promise would otherwise pin this mutation's\n // result, variables and context for as long as the cache keeps it\n if (this.#retryer === retryer) {\n this.#retryer = undefined\n }\n this.#mutationCache.runNext(this)\n }\n }\n\n #dispatch(action: Action<TData, TError, TVariables, TOnMutateResult>): void {\n const reducer = (\n state: MutationState<TData, TError, TVariables, TOnMutateResult>,\n ): MutationState<TData, TError, TVariables, TOnMutateResult> => {\n switch (action.type) {\n case 'failed':\n return {\n ...state,\n failureCount: action.failureCount,\n failureReason: action.error,\n }\n case 'pause':\n return {\n ...state,\n isPaused: true,\n }\n case 'continue':\n return {\n ...state,\n isPaused: false,\n }\n case 'pending':\n return {\n ...state,\n context: action.context,\n data: undefined,\n failureCount: 0,\n failureReason: null,\n error: null,\n isPaused: action.isPaused,\n status: 'pending',\n variables: action.variables,\n submittedAt: Date.now(),\n }\n case 'success':\n return {\n ...state,\n data: action.data,\n failureCount: 0,\n failureReason: null,\n error: null,\n status: 'success',\n isPaused: false,\n }\n case 'error':\n return {\n ...state,\n data: undefined,\n error: action.error,\n failureCount: state.failureCount + 1,\n failureReason: action.error,\n isPaused: false,\n status: 'error',\n }\n }\n }\n this.state = reducer(this.state)\n\n notifyManager.batch(() => {\n this.#observers.forEach((observer) => {\n observer.onMutationUpdate(action)\n })\n this.#mutationCache.notify({\n mutation: this,\n type: 'updated',\n action,\n })\n })\n }\n}\n\n/**\n * Returns the initial state of a mutation: `'idle'`, with no data, error, or variables.\n * @returns The initial mutation state.\n */\nexport function getDefaultState<\n TData,\n TError,\n TVariables,\n TOnMutateResult,\n>(): MutationState<TData, TError, TVariables, TOnMutateResult> {\n return {\n context: undefined,\n data: undefined,\n error: null,\n failureCount: 0,\n failureReason: null,\n isPaused: false,\n status: 'idle',\n variables: undef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,UAAU,OAAO;KACjB,QAAQ;KACR,WAAW,OAAO;KAClB,aAAa,KAAK,IAAI;IACxB;IACF,KAAK,WACH,OAAO;KACL,GAAG;KACH,MAAM,OAAO;KACb,cAAc;KACd,eAAe;KACf,OAAO;KACP,QAAQ;KACR,UAAU;IACZ;IACF,KAAK,SACH,OAAO;KACL,GAAG;KACH,MAAM,KAAA;KACN,OAAO,OAAO;KACd,cAAc,MAAM,eAAe;KACnC,eAAe,OAAO;KACtB,UAAU;KACV,QAAQ;IACV;GACJ;EACF;EACA,KAAK,QAAQ,QAAQ,KAAK,KAAK;EAE/B,cAAc,YAAY;GACxB,KAAK,WAAW,SAAS,aAAa;IACpC,SAAS,iBAAiB,MAAM;GAClC,CAAC;GACD,KAAK,eAAe,OAAO;IACzB,UAAU;IACV,MAAM;IACN;GACF,CAAC;EACH,CAAC;CACH;AACF;;;;;AAMA,SAAgB,kBAK+C;CAC7D,OAAO;EACL,SAAS,KAAA;EACT,MAAM,KAAA;EACN,OAAO;EACP,cAAc;EACd,eAAe;EACf,UAAU;EACV,QAAQ;EACR,WAAW,KAAA;EACX,aAAa;CACf;AACF"}
|
|
1
|
+
{"version":3,"file":"mutation.js","names":[],"sources":["../../src/mutation.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { Removable } from './removable'\nimport { createRetryer } from './retryer'\nimport type {\n DefaultError,\n MutationFunctionContext,\n MutationMeta,\n MutationOptions,\n MutationStatus,\n} from './types'\nimport type { MutationCache } from './mutationCache'\nimport type { MutationObserver } from './mutationObserver'\nimport type { Retryer } from './retryer'\nimport type { QueryClient } from './queryClient'\n\n// TYPES\n\ninterface MutationConfig<TData, TError, TVariables, TOnMutateResult> {\n client: QueryClient\n mutationId: number\n mutationCache: MutationCache\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n state?: MutationState<TData, TError, TVariables, TOnMutateResult>\n}\n\n/**\n * The raw state stored on a `Mutation` instance. This is the underlying state\n * that observer results (e.g. `MutationObserverResult`) are derived from.\n */\nexport interface MutationState<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> {\n /**\n * The value returned by `onMutate`, if defined. Passed to `onSuccess`,\n * `onError` and `onSettled` as the mutation's context.\n */\n context: TOnMutateResult | undefined\n /**\n * The last successfully resolved data for the mutation.\n */\n data: TData | undefined\n /**\n * The error object for the mutation, if the last attempt resulted in an error.\n * - Defaults to `null`.\n */\n error: TError | null\n /**\n * The number of times the mutation function has failed for the current attempt.\n */\n failureCount: number\n /**\n * The reason the current attempt failed, as reported by the retryer.\n */\n failureReason: TError | null\n /**\n * Whether the mutation is currently paused (see network mode), or is\n * waiting for another mutation with the same `scope` to finish.\n */\n isPaused: boolean\n /**\n * The status of the mutation.\n */\n status: MutationStatus\n /**\n * The variables the mutation was last called with.\n */\n variables: TVariables | undefined\n /**\n * The timestamp for when the mutation was submitted.\n */\n submittedAt: number\n}\n\ninterface FailedAction<TError> {\n type: 'failed'\n failureCount: number\n error: TError | null\n}\n\ninterface PendingAction<TVariables, TOnMutateResult> {\n type: 'pending'\n isPaused: boolean\n variables?: TVariables\n context?: TOnMutateResult\n}\n\ninterface SuccessAction<TData> {\n type: 'success'\n data: TData\n}\n\ninterface ErrorAction<TError> {\n type: 'error'\n error: TError\n}\n\ninterface PauseAction {\n type: 'pause'\n}\n\ninterface ContinueAction {\n type: 'continue'\n}\n\n/**\n * The actions a mutation dispatches to update its state, e.g. when it starts, fails, pauses, or\n * succeeds.\n */\nexport type Action<TData, TError, TVariables, TOnMutateResult> =\n | ContinueAction\n | ErrorAction<TError>\n | FailedAction<TError>\n | PendingAction<TVariables, TOnMutateResult>\n | PauseAction\n | SuccessAction<TData>\n\n// CLASS\n\n/**\n * Represents a single mutation attempt. A `Mutation` holds the mutation's\n * options, state (data/error/status), and the `MutationObserver`s currently\n * subscribed to it.\n *\n * Instances are created and managed internally by `MutationCache`; application\n * code typically interacts with mutations indirectly through `QueryClient` or\n * a framework hook like `useMutation`. Direct access to a `Mutation` instance\n * is possible via `mutationCache.find()`/`getAll()` for inspecting cache state.\n * @example\n * ```ts\n * const mutationCache = queryClient.getMutationCache()\n *\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * ```\n */\nexport class Mutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = unknown,\n TOnMutateResult = unknown,\n> extends Removable {\n state: MutationState<TData, TError, TVariables, TOnMutateResult>\n options!: MutationOptions<TData, TError, TVariables, TOnMutateResult>\n readonly mutationId: number\n\n #client: QueryClient\n #observers: Array<\n MutationObserver<TData, TError, TVariables, TOnMutateResult>\n >\n #mutationCache: MutationCache\n #retryer?: Retryer<TData>\n\n constructor(\n config: MutationConfig<TData, TError, TVariables, TOnMutateResult>,\n ) {\n super()\n\n this.#client = config.client\n this.mutationId = config.mutationId\n this.#mutationCache = config.mutationCache\n this.#observers = []\n this.state = config.state || getDefaultState()\n\n this.setOptions(config.options)\n this.scheduleGc()\n }\n\n /**\n * Replaces the mutation's options and applies their `gcTime`.\n * @internal\n * @param options - The new mutation options.\n */\n setOptions(\n options: MutationOptions<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.options = options\n\n this.updateGcTime(this.options.gcTime)\n }\n\n /**\n * The `meta` object passed in the mutation's options, if any.\n * @returns The mutation's `meta`, or `undefined` if none was set.\n */\n get meta(): MutationMeta | undefined {\n return this.options.meta\n }\n\n /**\n * Subscribes an observer to the mutation and stops its garbage collection.\n * @internal\n * @param observer - The observer to add. Adding an observer that is already subscribed does\n * nothing.\n */\n addObserver(observer: MutationObserver<any, any, any, any>): void {\n if (!this.#observers.includes(observer)) {\n this.#observers.push(observer)\n\n // Stop the mutation from being garbage collected\n this.clearGcTimeout()\n\n this.#mutationCache.notify({\n type: 'observerAdded',\n mutation: this,\n observer,\n })\n }\n }\n\n /**\n * Unsubscribes an observer from the mutation.\n * @internal\n * @param observer - The observer to remove. Garbage collection is scheduled afterwards.\n */\n removeObserver(observer: MutationObserver<any, any, any, any>): void {\n this.#observers = this.#observers.filter((x) => x !== observer)\n\n this.scheduleGc()\n\n this.#mutationCache.notify({\n type: 'observerRemoved',\n mutation: this,\n observer,\n })\n }\n\n protected override optionalRemove() {\n if (!this.#observers.length) {\n if (this.state.status === 'pending') {\n this.scheduleGc()\n } else {\n this.#mutationCache.remove(this)\n }\n }\n }\n\n /**\n * Resumes a mutation that is currently paused or was restored from a\n * dehydrated, still-`pending` state.\n *\n * - If this mutation has an active retryer (it paused mid-attempt, e.g. due\n * to the network mode or scope-based queuing), its retryer is resumed.\n * - Otherwise, if the mutation's status is still `pending` (e.g. it was\n * dehydrated while an attempt was in flight and never got a retryer in\n * this instance), `execute` is called again with the last known variables.\n * - Otherwise the mutation has already settled and this resolves immediately\n * without running anything again.\n * @returns A promise that settles with the resumed mutation: it rejects if the mutation fails.\n * It resolves immediately if the mutation has already settled.\n * @see {@link Mutation#execute}\n * @example\n * ```ts\n * // typically driven by reconnect handling, e.g. queryClient.resumePausedMutations()\n * const mutation = mutationCache.find({ mutationKey: ['addPost'] })\n * await mutation?.continue()\n * ```\n */\n continue(): Promise<unknown> {\n return (\n this.#retryer?.continue() ??\n // continuing a mutation assumes that variables are set, mutation must have been dehydrated before.\n // a settled mutation has no retryer to continue and must not run again\n (this.state.status === 'pending'\n ? this.execute(this.state.variables!)\n : Promise.resolve())\n )\n }\n\n /**\n * Runs the mutation function for the given variables through a retryer, and\n * drives the mutation's state and lifecycle callbacks through to settlement.\n *\n * If this mutation's state is already `pending` when `execute` is called\n * (i.e. it was restored, still in-flight, from a dehydrated state), the\n * `onMutate` step is skipped and a `continue` action is dispatched to\n * unpause it; otherwise a `pending` action is dispatched first, then the\n * mutation cache's `onMutate` and the mutation's own `onMutate` option are\n * awaited in that order, and the resulting context is stored.\n *\n * The mutation function is then run (subject to `retry`/`retryDelay`/\n * `networkMode`, and to the mutation cache's scope-based serialization).\n * On success, the cache's `onSuccess`/`onSettled` callbacks run before the\n * mutation's own `onSuccess`/`onSettled` options, a `success` action is\n * dispatched, and the resolved data is returned. On failure, the same\n * cache-then-option ordering is used for `onError`/`onSettled`, but each of\n * those four callbacks is individually caught so that a throwing callback\n * cannot mask the original error; an `error` action is then dispatched and\n * the original error is re-thrown.\n * @param variables - The variables passed to the `mutationFn`.\n * @returns A promise that resolves with the mutation's data, or rejects with its error.\n * @see {@link Mutation#continue}\n * @example\n * ```ts\n * // Called internally by `MutationObserver.mutate` and `Mutation.continue` —\n * // applications normally trigger mutations through those, not this method.\n * const data = await mutation.execute(variables)\n * ```\n */\n async execute(variables: TVariables): Promise<TData> {\n const onContinue = () => {\n this.#dispatch({ type: 'continue' })\n }\n\n const mutationFnContext = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n const retryer = (this.#retryer = createRetryer({\n fn: () => {\n if (!this.options.mutationFn) {\n return Promise.reject(new Error('No mutationFn found'))\n }\n\n return this.options.mutationFn(variables, mutationFnContext)\n },\n onFail: (failureCount, error) => {\n this.#dispatch({ type: 'failed', failureCount, error })\n },\n onPause: () => {\n this.#dispatch({ type: 'pause' })\n },\n onContinue,\n retry: this.options.retry ?? 0,\n retryDelay: this.options.retryDelay,\n networkMode: this.options.networkMode,\n canRun: () => this.#mutationCache.canRun(this),\n }))\n\n const restored = this.state.status === 'pending'\n const isPaused = !retryer.canStart()\n\n try {\n if (restored) {\n // Dispatch continue action to unpause restored mutation\n onContinue()\n } else {\n this.#dispatch({ type: 'pending', variables, isPaused })\n // Notify cache callback\n if (this.#mutationCache.config.onMutate) {\n await this.#mutationCache.config.onMutate(\n variables,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n }\n const context = await this.options.onMutate?.(\n variables,\n mutationFnContext,\n )\n if (context !== this.state.context) {\n this.#dispatch({\n type: 'pending',\n context,\n variables,\n isPaused,\n })\n }\n }\n // Unpause a mutation that became able to start while onMutate was running\n if (this.state.isPaused && retryer.canStart()) {\n onContinue()\n }\n const data = await retryer.start()\n\n // Notify cache callback\n await this.#mutationCache.config.onSuccess?.(\n data,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSuccess?.(\n data,\n variables,\n this.state.context!,\n mutationFnContext,\n )\n\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n data,\n null,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n\n await this.options.onSettled?.(\n data,\n null,\n variables,\n this.state.context,\n mutationFnContext,\n )\n\n this.#dispatch({ type: 'success', data })\n return data\n } catch (error) {\n try {\n // Notify cache callback\n await this.#mutationCache.config.onError?.(\n error as any,\n variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onError?.(\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n // Notify cache callback\n await this.#mutationCache.config.onSettled?.(\n undefined,\n error as any,\n this.state.variables,\n this.state.context,\n this as Mutation<unknown, unknown, unknown, unknown>,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n try {\n await this.options.onSettled?.(\n undefined,\n error as TError,\n variables,\n this.state.context,\n mutationFnContext,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n\n this.#dispatch({ type: 'error', error: error as TError })\n throw error\n } finally {\n // The settled retryer's promise would otherwise pin this mutation's\n // result, variables and context for as long as the cache keeps it\n if (this.#retryer === retryer) {\n this.#retryer = undefined\n }\n this.#mutationCache.runNext(this)\n }\n }\n\n #dispatch(action: Action<TData, TError, TVariables, TOnMutateResult>): void {\n const reducer = (\n state: MutationState<TData, TError, TVariables, TOnMutateResult>,\n ): MutationState<TData, TError, TVariables, TOnMutateResult> => {\n switch (action.type) {\n case 'failed':\n return {\n ...state,\n failureCount: action.failureCount,\n failureReason: action.error,\n }\n case 'pause':\n return {\n ...state,\n isPaused: true,\n }\n case 'continue':\n return {\n ...state,\n isPaused: false,\n }\n case 'pending':\n return {\n ...state,\n context: action.context,\n data: undefined,\n failureCount: 0,\n failureReason: null,\n error: null,\n isPaused: action.isPaused,\n status: 'pending',\n variables: action.variables,\n submittedAt: Date.now(),\n }\n case 'success':\n return {\n ...state,\n data: action.data,\n failureCount: 0,\n failureReason: null,\n error: null,\n status: 'success',\n isPaused: false,\n }\n case 'error':\n return {\n ...state,\n data: undefined,\n error: action.error,\n failureCount: state.failureCount + 1,\n failureReason: action.error,\n isPaused: false,\n status: 'error',\n }\n }\n }\n this.state = reducer(this.state)\n\n notifyManager.batch(() => {\n this.#observers.forEach((observer) => {\n observer.onMutationUpdate(action)\n })\n this.#mutationCache.notify({\n mutation: this,\n type: 'updated',\n action,\n })\n })\n }\n}\n\n/**\n * Returns the initial state of a mutation: `'idle'`, with no data, error, or variables.\n * @returns The initial mutation state.\n */\nexport function getDefaultState<\n TData,\n TError,\n TVariables,\n TOnMutateResult,\n>(): MutationState<TData, TError, TVariables, TOnMutateResult> {\n return {\n context: undefined,\n data: undefined,\n error: null,\n failureCount: 0,\n failureReason: null,\n isPaused: false,\n status: 'idle',\n variables: undefined,\n submittedAt: 0,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAyIA,IAAa,WAAb,cAKU,UAAU;CAKlB;CACA;CAGA;CACA;CAEA,YACE,QACA;EACA,MAAM;EAEN,KAAK,UAAU,OAAO;EACtB,KAAK,aAAa,OAAO;EACzB,KAAK,iBAAiB,OAAO;EAC7B,KAAK,aAAa,CAAC;EACnB,KAAK,QAAQ,OAAO,SAAS,gBAAgB;EAE7C,KAAK,WAAW,OAAO,OAAO;EAC9B,KAAK,WAAW;CAClB;;;;;;CAOA,WACE,SACM;EACN,KAAK,UAAU;EAEf,KAAK,aAAa,KAAK,QAAQ,MAAM;CACvC;;;;;CAMA,IAAI,OAAiC;EACnC,OAAO,KAAK,QAAQ;CACtB;;;;;;;CA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SAAS,iBAAiB,MAAM;GAClC,CAAC;GACD,KAAK,eAAe,OAAO;IACzB,UAAU;IACV,MAAM;IACN;GACF,CAAC;EACH,CAAC;CACH;AACF;;;;;AAMA,SAAgB,kBAK+C;CAC7D,OAAO;EACL,SAAS,KAAA;EACT,MAAM,KAAA;EACN,OAAO;EACP,cAAc;EACd,eAAe;EACf,UAAU;EACV,QAAQ;EACR,WAAW,KAAA;EACX,aAAa;CACf;AACF"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Jn as MutationCacheNotifyEvent, Kn as MutationCache, qn as MutationCacheConfig } from "./hydration-BXjqUj_D.cjs";
|
|
2
2
|
export { MutationCache, MutationCacheConfig, MutationCacheNotifyEvent };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Jn as MutationCacheNotifyEvent, Kn as MutationCache, qn as MutationCacheConfig } from "./hydration-vgIjIhk6.js";
|
|
2
2
|
export { MutationCache, MutationCacheConfig, MutationCacheNotifyEvent };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Yn as MutationObserver } from "./hydration-BXjqUj_D.cjs";
|
|
2
2
|
export { MutationObserver };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Yn as MutationObserver } from "./hydration-vgIjIhk6.js";
|
|
2
2
|
export { MutationObserver };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as Subscribable } from "./subscribable-D2lAzdDi.cjs";
|
|
2
|
-
import {
|
|
2
|
+
import { At as QueryObserverOptions, Pt as QueryObserverResult, ar as QueryObserver, on as QueryClient, tr as Query } from "./hydration-BXjqUj_D.cjs";
|
|
3
3
|
type QueriesObserverListener = (result: Array<QueryObserverResult>) => void;
|
|
4
4
|
type CombineFn<TCombinedResult> = (result: Array<QueryObserverResult>) => TCombinedResult;
|
|
5
5
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as Subscribable } from "./subscribable-D2lAzdDi.js";
|
|
2
|
-
import {
|
|
2
|
+
import { At as QueryObserverOptions, Pt as QueryObserverResult, ar as QueryObserver, on as QueryClient, tr as Query } from "./hydration-vgIjIhk6.js";
|
|
3
3
|
type QueriesObserverListener = (result: Array<QueryObserverResult>) => void;
|
|
4
4
|
type CombineFn<TCombinedResult> = (result: Array<QueryObserverResult>) => TCombinedResult;
|
|
5
5
|
/**
|
package/build/modern/query.cjs
CHANGED
|
@@ -30,6 +30,7 @@ var Query = class extends require_removable.Removable {
|
|
|
30
30
|
#cache;
|
|
31
31
|
#client;
|
|
32
32
|
#retryer;
|
|
33
|
+
#fetchPromise;
|
|
33
34
|
#defaultOptions;
|
|
34
35
|
#abortSignalConsumed;
|
|
35
36
|
constructor(config) {
|
|
@@ -352,9 +353,9 @@ var Query = class extends require_removable.Removable {
|
|
|
352
353
|
async fetch(options, fetchOptions) {
|
|
353
354
|
if (this.state.fetchStatus !== "idle" && this.#retryer?.status() !== "rejected") {
|
|
354
355
|
if (this.state.data !== void 0 && fetchOptions?.cancelRefetch) this.cancel({ silent: true });
|
|
355
|
-
else if (this.#retryer) {
|
|
356
|
+
else if (this.#retryer && this.#fetchPromise) {
|
|
356
357
|
this.#retryer.continueRetry();
|
|
357
|
-
return this.#
|
|
358
|
+
return this.#fetchPromise;
|
|
358
359
|
}
|
|
359
360
|
}
|
|
360
361
|
if (options) this.setOptions(options);
|
|
@@ -367,11 +368,17 @@ var Query = class extends require_removable.Removable {
|
|
|
367
368
|
}
|
|
368
369
|
const abortController = new AbortController();
|
|
369
370
|
const addSignalProperty = (object) => {
|
|
370
|
-
Object.
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
371
|
+
Object.defineProperties(object, {
|
|
372
|
+
[require_utils.fetchSignal]: {
|
|
373
|
+
value: abortController.signal,
|
|
374
|
+
enumerable: true
|
|
375
|
+
},
|
|
376
|
+
signal: {
|
|
377
|
+
enumerable: true,
|
|
378
|
+
get: () => {
|
|
379
|
+
this.#abortSignalConsumed = true;
|
|
380
|
+
return abortController.signal;
|
|
381
|
+
}
|
|
375
382
|
}
|
|
376
383
|
});
|
|
377
384
|
};
|
|
@@ -438,35 +445,45 @@ var Query = class extends require_removable.Removable {
|
|
|
438
445
|
networkMode: context.options.networkMode,
|
|
439
446
|
canRun: () => true
|
|
440
447
|
});
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
if (
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
if (error.
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
448
|
+
const fetchPromise = (async () => {
|
|
449
|
+
try {
|
|
450
|
+
const data = await retryer.promise;
|
|
451
|
+
if (data === void 0) {
|
|
452
|
+
if (process.env.NODE_ENV !== "production") console.error(`Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: ${this.queryHash}`);
|
|
453
|
+
throw new Error(`${this.queryHash} data is undefined`);
|
|
454
|
+
}
|
|
455
|
+
this.setData(data);
|
|
456
|
+
this.#cache.config.onSuccess?.(data, this);
|
|
457
|
+
this.#cache.config.onSettled?.(data, this.state.error, this);
|
|
458
|
+
return data;
|
|
459
|
+
} catch (error) {
|
|
460
|
+
if (error instanceof require_retryer.CancelledError) {
|
|
461
|
+
if (error.silent) {
|
|
462
|
+
if (this.#retryer !== retryer && this.#fetchPromise) return this.#fetchPromise;
|
|
463
|
+
throw error;
|
|
464
|
+
} else if (error.revert) {
|
|
465
|
+
if (this.state.data === void 0) throw error;
|
|
466
|
+
return this.state.data;
|
|
467
|
+
}
|
|
457
468
|
}
|
|
469
|
+
this.#dispatch({
|
|
470
|
+
type: "error",
|
|
471
|
+
error
|
|
472
|
+
});
|
|
473
|
+
this.#cache.config.onError?.(error, this);
|
|
474
|
+
this.#cache.config.onSettled?.(this.state.data, error, this);
|
|
475
|
+
throw error;
|
|
476
|
+
} finally {
|
|
477
|
+
if (this.#retryer === retryer) {
|
|
478
|
+
this.#retryer = void 0;
|
|
479
|
+
this.#fetchPromise = void 0;
|
|
480
|
+
}
|
|
481
|
+
this.scheduleGc();
|
|
458
482
|
}
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
this.#cache.config.onError?.(error, this);
|
|
464
|
-
this.#cache.config.onSettled?.(this.state.data, error, this);
|
|
465
|
-
throw error;
|
|
466
|
-
} finally {
|
|
467
|
-
if (this.#retryer === retryer) this.#retryer = void 0;
|
|
468
|
-
this.scheduleGc();
|
|
469
|
-
}
|
|
483
|
+
})();
|
|
484
|
+
this.#fetchPromise = fetchPromise;
|
|
485
|
+
retryer.start();
|
|
486
|
+
return fetchPromise;
|
|
470
487
|
}
|
|
471
488
|
#dispatch(action) {
|
|
472
489
|
const reducer = (state) => {
|