@tanstack/query-core 5.105.1 → 5.105.3
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-gHEs4kLM.d.ts → hydration-9_kOYxJM.d.ts} +26 -15
- package/build/legacy/hydration-9_kOYxJM.d.ts.map +1 -0
- package/build/legacy/{hydration-DdkP3REv.d.cts → hydration-BIRJt69T.d.cts} +26 -15
- package/build/legacy/hydration-BIRJt69T.d.cts.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 +56 -37
- 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 +57 -38
- 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.cjs +1 -1
- package/build/legacy/retryer.cjs.map +1 -1
- package/build/legacy/retryer.d.cts +1 -1
- package/build/legacy/retryer.d.ts +1 -1
- package/build/legacy/retryer.js +1 -1
- package/build/legacy/retryer.js.map +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-gHEs4kLM.d.ts → hydration-9_kOYxJM.d.ts} +26 -15
- package/build/modern/hydration-9_kOYxJM.d.ts.map +1 -0
- package/build/modern/{hydration-DdkP3REv.d.cts → hydration-BIRJt69T.d.cts} +26 -15
- package/build/modern/hydration-BIRJt69T.d.cts.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 +53 -35
- 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 +54 -36
- 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.cjs +1 -1
- package/build/modern/retryer.cjs.map +1 -1
- package/build/modern/retryer.d.cts +1 -1
- package/build/modern/retryer.d.ts +1 -1
- package/build/modern/retryer.js +1 -1
- package/build/modern/retryer.js.map +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 +83 -65
- package/src/queryClient.ts +16 -10
- package/src/retryer.ts +3 -1
- 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 +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;CAYlB,YACE,QACA;EACA,MAAM;;;;;;EAEN,8BAAA,uBAAA,SAAA,MAAe,OAAO,MAAA;EACtB,KAAK,aAAa,OAAO;EACzB,8BAAA,uBAAA,gBAAA,MAAsB,OAAO,aAAA;EAC7B,8BAAA,uBAAA,YAAA,MAAkB,CAAC,CAAA;EACnB,KAAK,QAAQ,OAAO,SAAS,gBAAgB;EAE7C,KAAK,WAAW,OAAO,OAAO;EAC9B,KAAK,WAAW;CAClB;;;;;;CA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AAO;IACL,GAAG;IACH,SAAS,OAAO;IAChB,MAAM,KAAA;IACN,cAAc;IACd,eAAe;IACf,OAAO;IACP,UAAU,OAAO;IACjB,QAAQ;IACR,WAAW,OAAO;IAClB,aAAa,KAAK,IAAI;GACxB;GACF,KAAK,WACH,OAAO;IACL,GAAG;IACH,MAAM,OAAO;IACb,cAAc;IACd,eAAe;IACf,OAAO;IACP,QAAQ;IACR,UAAU;GACZ;GACF,KAAK,SACH,OAAO;IACL,GAAG;IACH,MAAM,KAAA;IACN,OAAO,OAAO;IACd,cAAc,MAAM,eAAe;IACnC,eAAe,OAAO;IACtB,UAAU;IACV,QAAQ;GACV;EACJ;CACF;CACA,KAAK,QAAQ,QAAQ,KAAK,KAAK;CAE/B,sBAAA,cAAc,YAAY;EACxB,8BAAA,uBAAA,YAAA,IAAA,CAAA,CAAgB,SAAS,aAAa;GACpC,SAAS,iBAAiB,MAAM;EAClC,CAAC;EACD,8BAAA,uBAAA,gBAAA,IAAA,CAAA,CAAoB,OAAO;GACzB,UAAU;GACV,MAAM;GACN;EACF,CAAC;CACH,CAAC;AACH;;;;;AAOF,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;CAYlB,YACE,QACA;EACA,MAAM;;;;;;EAEN,8BAAA,uBAAA,SAAA,MAAe,OAAO,MAAA;EACtB,KAAK,aAAa,OAAO;EACzB,8BAAA,uBAAA,gBAAA,MAAsB,OAAO,aAAA;EAC7B,8BAAA,uBAAA,YAAA,MAAkB,CAAC,CAAA;EACnB,KAAK,QAAQ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OAAO;IACrB,eAAe,OAAO;GACxB;GACF,KAAK,SACH,OAAO;IACL,GAAG;IACH,UAAU;GACZ;GACF,KAAK,YACH,OAAO;IACL,GAAG;IACH,UAAU;GACZ;GACF,KAAK,WACH,OAAO;IACL,GAAG;IACH,SAAS,OAAO;IAChB,MAAM,KAAA;IACN,cAAc;IACd,eAAe;IACf,OAAO;IACP,UAAU,OAAO;IACjB,QAAQ;IACR,WAAW,OAAO;IAClB,aAAa,KAAK,IAAI;GACxB;GACF,KAAK,WACH,OAAO;IACL,GAAG;IACH,MAAM,OAAO;IACb,cAAc;IACd,eAAe;IACf,OAAO;IACP,QAAQ;IACR,UAAU;GACZ;GACF,KAAK,SACH,OAAO;IACL,GAAG;IACH,MAAM,KAAA;IACN,OAAO,OAAO;IACd,cAAc,MAAM,eAAe;IACnC,eAAe,OAAO;IACtB,UAAU;IACV,QAAQ;GACV;EACJ;CACF;CACA,KAAK,QAAQ,QAAQ,KAAK,KAAK;CAE/B,sBAAA,cAAc,YAAY;EACxB,8BAAA,uBAAA,YAAA,IAAA,CAAA,CAAgB,SAAS,aAAa;GACpC,SAAS,iBAAiB,MAAM;EAClC,CAAC;EACD,8BAAA,uBAAA,gBAAA,IAAA,CAAA,CAAoB,OAAO;GACzB,UAAU;GACV,MAAM;GACN;EACF,CAAC;CACH,CAAC;AACH;;;;;AAOF,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-BIRJt69T.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-9_kOYxJM.js";
|
|
2
2
|
export { Action, Mutation, MutationState, getDefaultState };
|
package/build/legacy/mutation.js
CHANGED
|
@@ -200,6 +200,7 @@ var Mutation = class extends Removable {
|
|
|
200
200
|
isPaused
|
|
201
201
|
});
|
|
202
202
|
}
|
|
203
|
+
if (this.state.isPaused && retryer.canStart()) onContinue();
|
|
203
204
|
const data = await retryer.start();
|
|
204
205
|
await ((_classPrivateFieldGet3 = (_classPrivateFieldGet4 = _classPrivateFieldGet2(_mutationCache, this).config).onSuccess) === null || _classPrivateFieldGet3 === void 0 ? void 0 : _classPrivateFieldGet3.call(_classPrivateFieldGet4, data, variables, this.state.context, this, mutationFnContext));
|
|
205
206
|
await ((_this$options$onSucce = (_this$options2 = this.options).onSuccess) === null || _this$options$onSucce === void 0 ? void 0 : _this$options$onSucce.call(_this$options2, 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: undefined,\n submittedAt: 0,\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAyIA,IAAa,WAAb,cAKU,UAAU;CAYlB,YACE,QACA;EACA,MAAM;;;;;;EAEN,uBAAA,SAAA,MAAe,OAAO,MAAA;EACtB,KAAK,aAAa,OAAO;EACzB,uBAAA,gBAAA,MAAsB,OAAO,aAAA;EAC7B,uBAAA,YAAA,MAAkB,CAAC,CAAA;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,CAAA,uBAAA,YAAC,IAAA,CAAA,CAAgB,SAAS,QAAQ,GAAG;GACvC,uBAAA,YAAA,IAAA,CAAA,CAAgB,KAAK,QAAQ;GAG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,IAAA,CAAA,CAAgB,SAAS,aAAa;GACpC,SAAS,iBAAiB,MAAM;EAClC,CAAC;EACD,uBAAA,gBAAA,IAAA,CAAA,CAAoB,OAAO;GACzB,UAAU;GACV,MAAM;GACN;EACF,CAAC;CACH,CAAC;AACH;;;;;AAOF,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;CAYlB,YACE,QACA;EACA,MAAM;;;;;;EAEN,uBAAA,SAAA,MAAe,OAAO,MAAA;EACtB,KAAK,aAAa,OAAO;EACzB,uBAAA,gBAAA,MAAsB,OAAO,aAAA;EAC7B,uBAAA,YAAA,MAAkB,CAAC,CAAA;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,CAAA,uBAAA,YAAC,IAAA,CAAA,CAAgB,SAAS,QAAQ,GAAG;GACvC,uBAAA,YAAA,IAAA,CAAA,CAAgB,KAAK,QAAQ;GAG7B,KAAK,eAAe;GAEpB,uBAAA,gBAAA,IAAA,CAAA,CAAoB,OAAO;IACzB,MAAM;IACN,UAAU;IACV;GACF,CAAC;EACH;CACF;;;;;;CAOA,eAAe,UAAsD;EACnE,uBAAA,YAAA,MAAA,uBAAA,YAAkB,IAAA,CAAA,CAAg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AM;GACN;EACF,CAAC;CACH,CAAC;AACH;;;;;AAOF,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-BIRJt69T.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-9_kOYxJM.js";
|
|
2
2
|
export { MutationCache, MutationCacheConfig, MutationCacheNotifyEvent };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Yn as MutationObserver } from "./hydration-BIRJt69T.cjs";
|
|
2
2
|
export { MutationObserver };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Yn as MutationObserver } from "./hydration-9_kOYxJM.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-BIRJt69T.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-9_kOYxJM.js";
|
|
3
3
|
type QueriesObserverListener = (result: Array<QueryObserverResult>) => void;
|
|
4
4
|
type CombineFn<TCombinedResult> = (result: Array<QueryObserverResult>) => TCombinedResult;
|
|
5
5
|
/**
|
package/build/legacy/query.cjs
CHANGED
|
@@ -13,6 +13,7 @@ var _revertState = /* @__PURE__ */ new WeakMap();
|
|
|
13
13
|
var _cache = /* @__PURE__ */ new WeakMap();
|
|
14
14
|
var _client = /* @__PURE__ */ new WeakMap();
|
|
15
15
|
var _retryer = /* @__PURE__ */ new WeakMap();
|
|
16
|
+
var _fetchPromise = /* @__PURE__ */ new WeakMap();
|
|
16
17
|
var _defaultOptions = /* @__PURE__ */ new WeakMap();
|
|
17
18
|
var _abortSignalConsumed = /* @__PURE__ */ new WeakMap();
|
|
18
19
|
var _Query_brand = /* @__PURE__ */ new WeakSet();
|
|
@@ -44,6 +45,7 @@ var Query = class extends require_removable.Removable {
|
|
|
44
45
|
require_classPrivateFieldSet2._classPrivateFieldInitSpec(this, _cache, void 0);
|
|
45
46
|
require_classPrivateFieldSet2._classPrivateFieldInitSpec(this, _client, void 0);
|
|
46
47
|
require_classPrivateFieldSet2._classPrivateFieldInitSpec(this, _retryer, void 0);
|
|
48
|
+
require_classPrivateFieldSet2._classPrivateFieldInitSpec(this, _fetchPromise, void 0);
|
|
47
49
|
require_classPrivateFieldSet2._classPrivateFieldInitSpec(this, _defaultOptions, void 0);
|
|
48
50
|
require_classPrivateFieldSet2._classPrivateFieldInitSpec(this, _abortSignalConsumed, void 0);
|
|
49
51
|
require_classPrivateFieldSet2._classPrivateFieldSet2(_abortSignalConsumed, this, false);
|
|
@@ -211,11 +213,12 @@ var Query = class extends require_removable.Removable {
|
|
|
211
213
|
return this.state.dataUpdateCount + this.state.errorUpdateCount > 0;
|
|
212
214
|
}
|
|
213
215
|
/**
|
|
214
|
-
* Returns `true` if the query has at least one observer configured with
|
|
216
|
+
* Returns `true` if the query has data and at least one observer configured with
|
|
215
217
|
* `staleTime: 'static'`, meaning it is treated as never stale.
|
|
216
218
|
* @returns `true` if the query is static.
|
|
217
219
|
*/
|
|
218
220
|
isStatic() {
|
|
221
|
+
if (this.state.data === void 0) return false;
|
|
219
222
|
if (this.getObserversCount() > 0) return this.observers.some((observer) => require_utils.resolveQueryValue(observer.options.staleTime, this) === "static");
|
|
220
223
|
return false;
|
|
221
224
|
}
|
|
@@ -371,9 +374,9 @@ var Query = class extends require_removable.Removable {
|
|
|
371
374
|
var _classPrivateFieldGet7, _context$fetchOptions;
|
|
372
375
|
if (this.state.fetchStatus !== "idle" && ((_classPrivateFieldGet7 = require_classPrivateFieldSet2._classPrivateFieldGet2(_retryer, this)) === null || _classPrivateFieldGet7 === void 0 ? void 0 : _classPrivateFieldGet7.status()) !== "rejected") {
|
|
373
376
|
if (this.state.data !== void 0 && (fetchOptions === null || fetchOptions === void 0 ? void 0 : fetchOptions.cancelRefetch)) this.cancel({ silent: true });
|
|
374
|
-
else if (require_classPrivateFieldSet2._classPrivateFieldGet2(_retryer, this)) {
|
|
377
|
+
else if (require_classPrivateFieldSet2._classPrivateFieldGet2(_retryer, this) && require_classPrivateFieldSet2._classPrivateFieldGet2(_fetchPromise, this)) {
|
|
375
378
|
require_classPrivateFieldSet2._classPrivateFieldGet2(_retryer, this).continueRetry();
|
|
376
|
-
return require_classPrivateFieldSet2._classPrivateFieldGet2(
|
|
379
|
+
return require_classPrivateFieldSet2._classPrivateFieldGet2(_fetchPromise, this);
|
|
377
380
|
}
|
|
378
381
|
}
|
|
379
382
|
if (options) this.setOptions(options);
|
|
@@ -386,11 +389,17 @@ var Query = class extends require_removable.Removable {
|
|
|
386
389
|
}
|
|
387
390
|
const abortController = new AbortController();
|
|
388
391
|
const addSignalProperty = (object) => {
|
|
389
|
-
Object.
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
392
|
+
Object.defineProperties(object, {
|
|
393
|
+
[require_utils.fetchSignal]: {
|
|
394
|
+
value: abortController.signal,
|
|
395
|
+
enumerable: true
|
|
396
|
+
},
|
|
397
|
+
signal: {
|
|
398
|
+
enumerable: true,
|
|
399
|
+
get: () => {
|
|
400
|
+
require_classPrivateFieldSet2._classPrivateFieldSet2(_abortSignalConsumed, this, true);
|
|
401
|
+
return abortController.signal;
|
|
402
|
+
}
|
|
394
403
|
}
|
|
395
404
|
});
|
|
396
405
|
};
|
|
@@ -461,37 +470,47 @@ var Query = class extends require_removable.Removable {
|
|
|
461
470
|
networkMode: context.options.networkMode,
|
|
462
471
|
canRun: () => true
|
|
463
472
|
}));
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
if (
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
if (error
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
473
|
+
const fetchPromise = (async () => {
|
|
474
|
+
try {
|
|
475
|
+
var _classPrivateFieldGet8, _classPrivateFieldGet9, _classPrivateFieldGet10, _classPrivateFieldGet11;
|
|
476
|
+
const data = await retryer.promise;
|
|
477
|
+
if (data === void 0) {
|
|
478
|
+
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}`);
|
|
479
|
+
throw new Error(`${this.queryHash} data is undefined`);
|
|
480
|
+
}
|
|
481
|
+
this.setData(data);
|
|
482
|
+
(_classPrivateFieldGet8 = (_classPrivateFieldGet9 = require_classPrivateFieldSet2._classPrivateFieldGet2(_cache, this).config).onSuccess) === null || _classPrivateFieldGet8 === void 0 || _classPrivateFieldGet8.call(_classPrivateFieldGet9, data, this);
|
|
483
|
+
(_classPrivateFieldGet10 = (_classPrivateFieldGet11 = require_classPrivateFieldSet2._classPrivateFieldGet2(_cache, this).config).onSettled) === null || _classPrivateFieldGet10 === void 0 || _classPrivateFieldGet10.call(_classPrivateFieldGet11, data, this.state.error, this);
|
|
484
|
+
return data;
|
|
485
|
+
} catch (error) {
|
|
486
|
+
var _classPrivateFieldGet12, _classPrivateFieldGet13, _classPrivateFieldGet14, _classPrivateFieldGet15;
|
|
487
|
+
if (error instanceof require_retryer.CancelledError) {
|
|
488
|
+
if (error.silent) {
|
|
489
|
+
if (require_classPrivateFieldSet2._classPrivateFieldGet2(_retryer, this) !== retryer && require_classPrivateFieldSet2._classPrivateFieldGet2(_fetchPromise, this)) return require_classPrivateFieldSet2._classPrivateFieldGet2(_fetchPromise, this);
|
|
490
|
+
throw error;
|
|
491
|
+
} else if (error.revert) {
|
|
492
|
+
if (this.state.data === void 0) throw error;
|
|
493
|
+
return this.state.data;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
require_classPrivateFieldSet2._assertClassBrand(_Query_brand, this, _dispatch).call(this, {
|
|
497
|
+
type: "error",
|
|
498
|
+
error
|
|
499
|
+
});
|
|
500
|
+
(_classPrivateFieldGet12 = (_classPrivateFieldGet13 = require_classPrivateFieldSet2._classPrivateFieldGet2(_cache, this).config).onError) === null || _classPrivateFieldGet12 === void 0 || _classPrivateFieldGet12.call(_classPrivateFieldGet13, error, this);
|
|
501
|
+
(_classPrivateFieldGet14 = (_classPrivateFieldGet15 = require_classPrivateFieldSet2._classPrivateFieldGet2(_cache, this).config).onSettled) === null || _classPrivateFieldGet14 === void 0 || _classPrivateFieldGet14.call(_classPrivateFieldGet15, this.state.data, error, this);
|
|
502
|
+
throw error;
|
|
503
|
+
} finally {
|
|
504
|
+
if (require_classPrivateFieldSet2._classPrivateFieldGet2(_retryer, this) === retryer) {
|
|
505
|
+
require_classPrivateFieldSet2._classPrivateFieldSet2(_retryer, this, void 0);
|
|
506
|
+
require_classPrivateFieldSet2._classPrivateFieldSet2(_fetchPromise, this, void 0);
|
|
482
507
|
}
|
|
508
|
+
this.scheduleGc();
|
|
483
509
|
}
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
(_classPrivateFieldGet12 = (_classPrivateFieldGet13 = require_classPrivateFieldSet2._classPrivateFieldGet2(_cache, this).config).onError) === null || _classPrivateFieldGet12 === void 0 || _classPrivateFieldGet12.call(_classPrivateFieldGet13, error, this);
|
|
489
|
-
(_classPrivateFieldGet14 = (_classPrivateFieldGet15 = require_classPrivateFieldSet2._classPrivateFieldGet2(_cache, this).config).onSettled) === null || _classPrivateFieldGet14 === void 0 || _classPrivateFieldGet14.call(_classPrivateFieldGet15, this.state.data, error, this);
|
|
490
|
-
throw error;
|
|
491
|
-
} finally {
|
|
492
|
-
if (require_classPrivateFieldSet2._classPrivateFieldGet2(_retryer, this) === retryer) require_classPrivateFieldSet2._classPrivateFieldSet2(_retryer, this, void 0);
|
|
493
|
-
this.scheduleGc();
|
|
494
|
-
}
|
|
510
|
+
})();
|
|
511
|
+
require_classPrivateFieldSet2._classPrivateFieldSet2(_fetchPromise, this, fetchPromise);
|
|
512
|
+
retryer.start();
|
|
513
|
+
return fetchPromise;
|
|
495
514
|
}
|
|
496
515
|
};
|
|
497
516
|
function _dispatch(action) {
|