@tanstack/query-core 5.107.1 → 5.108.0
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-9_kOYxJM.d.ts → hydration-Bwo9mbMU.d.ts} +3177 -3177
- package/build/legacy/hydration-Bwo9mbMU.d.ts.map +1 -0
- package/build/legacy/{hydration-BIRJt69T.d.cts → hydration-DRPiSXsi.d.cts} +3177 -3177
- package/build/legacy/hydration-DRPiSXsi.d.cts.map +1 -0
- package/build/legacy/hydration.cjs.map +1 -1
- package/build/legacy/hydration.d.cts +1 -1
- package/build/legacy/hydration.d.ts +1 -1
- package/build/legacy/hydration.js.map +1 -1
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +2 -2
- package/build/legacy/index.d.ts +2 -2
- package/build/legacy/index.js.map +1 -1
- package/build/legacy/infiniteQueryBehavior.cjs.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
- package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
- package/build/legacy/infiniteQueryBehavior.js.map +1 -1
- package/build/legacy/infiniteQueryObserver.cjs.map +1 -1
- package/build/legacy/infiniteQueryObserver.d.cts +1 -1
- package/build/legacy/infiniteQueryObserver.d.ts +1 -1
- package/build/legacy/infiniteQueryObserver.js.map +1 -1
- package/build/legacy/mutation.cjs +1 -1
- 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 -1
- package/build/legacy/mutation.js.map +1 -1
- package/build/legacy/mutationCache.cjs.map +1 -1
- package/build/legacy/mutationCache.d.cts +1 -1
- package/build/legacy/mutationCache.d.ts +1 -1
- package/build/legacy/mutationCache.js.map +1 -1
- package/build/legacy/mutationObserver.cjs.map +1 -1
- package/build/legacy/mutationObserver.d.cts +1 -1
- package/build/legacy/mutationObserver.d.ts +1 -1
- package/build/legacy/mutationObserver.js.map +1 -1
- package/build/legacy/queriesObserver.cjs.map +1 -1
- package/build/legacy/queriesObserver.d.cts +1 -1
- package/build/legacy/queriesObserver.d.ts +1 -1
- package/build/legacy/queriesObserver.js.map +1 -1
- package/build/legacy/query.cjs +6 -4
- 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 +6 -4
- package/build/legacy/query.js.map +1 -1
- package/build/legacy/queryCache.cjs.map +1 -1
- package/build/legacy/queryCache.d.cts +1 -1
- package/build/legacy/queryCache.d.ts +1 -1
- package/build/legacy/queryCache.js.map +1 -1
- 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.map +1 -1
- package/build/legacy/queryObserver.cjs.map +1 -1
- package/build/legacy/queryObserver.d.cts +1 -1
- package/build/legacy/queryObserver.d.ts +1 -1
- package/build/legacy/queryObserver.js.map +1 -1
- package/build/legacy/removable.cjs.map +1 -1
- package/build/legacy/removable.js.map +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.map +1 -1
- package/build/legacy/streamedQuery.cjs.map +1 -1
- package/build/legacy/streamedQuery.d.cts +1 -1
- package/build/legacy/streamedQuery.d.ts +1 -1
- package/build/legacy/streamedQuery.js.map +1 -1
- package/build/legacy/subscribable-D2lAzdDi.d.cts.map +1 -1
- package/build/legacy/subscribable-D2lAzdDi.d.ts.map +1 -1
- package/build/legacy/subscribable.cjs.map +1 -1
- package/build/legacy/subscribable.js.map +1 -1
- package/build/legacy/types.cjs.map +1 -1
- package/build/legacy/types.d.cts +1 -1
- package/build/legacy/types.d.ts +1 -1
- package/build/legacy/types.js.map +1 -1
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts +1 -1
- package/build/legacy/utils.d.ts +1 -1
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/{hydration-9_kOYxJM.d.ts → hydration-Bwo9mbMU.d.ts} +3177 -3177
- package/build/modern/hydration-Bwo9mbMU.d.ts.map +1 -0
- package/build/modern/{hydration-BIRJt69T.d.cts → hydration-DRPiSXsi.d.cts} +3177 -3177
- package/build/modern/hydration-DRPiSXsi.d.cts.map +1 -0
- package/build/modern/hydration.cjs.map +1 -1
- package/build/modern/hydration.d.cts +1 -1
- package/build/modern/hydration.d.ts +1 -1
- package/build/modern/hydration.js.map +1 -1
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +2 -2
- package/build/modern/index.d.ts +2 -2
- package/build/modern/index.js.map +1 -1
- package/build/modern/infiniteQueryBehavior.cjs.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.cts +1 -1
- package/build/modern/infiniteQueryBehavior.d.ts +1 -1
- package/build/modern/infiniteQueryBehavior.js.map +1 -1
- package/build/modern/infiniteQueryObserver.cjs.map +1 -1
- package/build/modern/infiniteQueryObserver.d.cts +1 -1
- package/build/modern/infiniteQueryObserver.d.ts +1 -1
- package/build/modern/infiniteQueryObserver.js.map +1 -1
- package/build/modern/mutation.cjs +1 -1
- 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 -1
- package/build/modern/mutation.js.map +1 -1
- package/build/modern/mutationCache.cjs.map +1 -1
- package/build/modern/mutationCache.d.cts +1 -1
- package/build/modern/mutationCache.d.ts +1 -1
- package/build/modern/mutationCache.js.map +1 -1
- package/build/modern/mutationObserver.cjs.map +1 -1
- package/build/modern/mutationObserver.d.cts +1 -1
- package/build/modern/mutationObserver.d.ts +1 -1
- package/build/modern/mutationObserver.js.map +1 -1
- package/build/modern/queriesObserver.cjs.map +1 -1
- package/build/modern/queriesObserver.d.cts +1 -1
- package/build/modern/queriesObserver.d.ts +1 -1
- package/build/modern/queriesObserver.js.map +1 -1
- package/build/modern/query.cjs +6 -4
- 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 +6 -4
- package/build/modern/query.js.map +1 -1
- package/build/modern/queryCache.cjs.map +1 -1
- package/build/modern/queryCache.d.cts +1 -1
- package/build/modern/queryCache.d.ts +1 -1
- package/build/modern/queryCache.js.map +1 -1
- 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.map +1 -1
- package/build/modern/queryObserver.cjs.map +1 -1
- package/build/modern/queryObserver.d.cts +1 -1
- package/build/modern/queryObserver.d.ts +1 -1
- package/build/modern/queryObserver.js.map +1 -1
- package/build/modern/removable.cjs.map +1 -1
- package/build/modern/removable.js.map +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.map +1 -1
- package/build/modern/streamedQuery.cjs.map +1 -1
- package/build/modern/streamedQuery.d.cts +1 -1
- package/build/modern/streamedQuery.d.ts +1 -1
- package/build/modern/streamedQuery.js.map +1 -1
- package/build/modern/subscribable-D2lAzdDi.d.cts.map +1 -1
- package/build/modern/subscribable-D2lAzdDi.d.ts.map +1 -1
- package/build/modern/subscribable.cjs.map +1 -1
- package/build/modern/subscribable.js.map +1 -1
- package/build/modern/types.cjs.map +1 -1
- package/build/modern/types.d.cts +1 -1
- package/build/modern/types.d.ts +1 -1
- package/build/modern/types.js.map +1 -1
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts +1 -1
- package/build/modern/utils.d.ts +1 -1
- package/build/modern/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/hydration.ts +3 -3
- package/src/index.ts +98 -1
- package/src/infiniteQueryBehavior.ts +1 -1
- package/src/infiniteQueryObserver.ts +3 -3
- package/src/mutation.ts +4 -4
- package/src/mutationCache.ts +4 -4
- package/src/mutationObserver.ts +1 -1
- package/src/queriesObserver.ts +1 -1
- package/src/query.ts +10 -8
- package/src/queryCache.ts +5 -5
- package/src/queryClient.ts +6 -6
- package/src/queryObserver.ts +3 -3
- package/src/removable.ts +1 -1
- package/src/retryer.ts +1 -1
- package/src/streamedQuery.ts +1 -1
- package/src/subscribable.ts +4 -1
- package/src/types.ts +58 -57
- package/src/utils.ts +2 -2
- package/build/legacy/hydration-9_kOYxJM.d.ts.map +0 -1
- package/build/legacy/hydration-BIRJt69T.d.cts.map +0 -1
- package/build/modern/hydration-9_kOYxJM.d.ts.map +0 -1
- package/build/modern/hydration-BIRJt69T.d.cts.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutationObserver.cjs","names":["Subscribable","shallowEqualObjects","hashKey","getDefaultState"],"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { hashKey, shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationFunctionContext,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TOnMutateResult> = (\n result: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,\n) => void\n\n// CLASS\n\n/**\n * Observes a single mutation and derives a `MutationObserverResult` from it.\n * A framework hook like `useMutation` creates one `MutationObserver` per hook\n * call, keeps it stable across re-renders, calls `setOptions` when the options\n * passed to the hook change, subscribes to it to re-render on updates, and\n * reads `getCurrentResult()` for the value to return. Calling `mutate()`\n * builds a new underlying `Mutation` in the `MutationCache` and executes it.\n * @example\n * ```ts\n * const observer = new MutationObserver(queryClient, {\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * })\n * ```\n */\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TOnMutateResult>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > = undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TOnMutateResult>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TOnMutateResult>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n /**\n * Binds the methods of the result (`mutate` and `reset`) to the observer, so they keep working when\n * destructured from it.\n */\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n /**\n * Updates the observer's options.\n *\n * If the new `mutationKey` differs from the previous one (and both were\n * defined), the observer is reset, detaching it from the mutation it was\n * observing. Otherwise, if the currently observed mutation is still\n * `pending`, its options are updated in place as well.\n * @param options - The new mutation observer options. They are defaulted with {@link\n * QueryClient#defaultMutationOptions} before being applied.\n * @example\n * ```ts\n * observer.setOptions({\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * onSuccess: (data) => console.log(data),\n * })\n * ```\n */\n setOptions(\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n const prevOptions = this.options as\n | MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n | undefined\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(this.options, prevOptions)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n\n if (\n prevOptions?.mutationKey &&\n this.options.mutationKey &&\n hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)\n ) {\n this.reset()\n } else if (this.#currentMutation?.state.status === 'pending') {\n this.#currentMutation.setOptions(this.options)\n }\n }\n\n protected override onSubscribe(): void {\n if (this.listeners.size === 1 && this.#currentMutation) {\n this.#currentMutation.addObserver(this)\n\n this.#updateResult()\n }\n }\n\n protected override onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n /**\n * Updates the observer's result when the observed mutation changes, and notifies its listeners.\n * @internal\n * @param action - The action that updated the observed mutation, passed on to the observer's\n * callbacks.\n */\n onMutationUpdate(\n action: Action<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n /**\n * Returns the observer's current result, derived from the observed\n * mutation's state (or the default, `idle` state if no mutation has been\n * built yet, e.g. before the first `mutate()` call or after `reset()`).\n * @returns The observer's latest result.\n */\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > {\n return this.#currentResult\n }\n\n /**\n * Detaches the observer from the mutation it is currently observing (if\n * any) and resets the observed result back to its default, `idle` state.\n *\n * This does not cancel an in-flight mutation; the mutation itself keeps\n * running to completion and its own callbacks still fire, but this\n * observer stops reflecting its state and a subsequent `mutate()` call\n * will build a brand new mutation.\n * @see {@link MutationObserver#mutate}\n * @example\n * ```ts\n * observer.reset()\n * ```\n */\n reset(): void {\n // reset needs to remove the observer from the mutation because there is no way to \"get it back\"\n // another mutate call will yield a new mutation!\n this.#currentMutation?.removeObserver(this)\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n /**\n * Builds a new `Mutation` in the `MutationCache` using the observer's\n * current options, detaches this observer from any previously observed\n * mutation, attaches it to the new one, and executes it with the given\n * variables.\n *\n * The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are\n * invoked once the mutation settles, in addition to any callbacks defined\n * on the observer's own options.\n * @param variables - The variables passed to the `mutationFn`.\n * @param options - Per-call `onSuccess`, `onError`, and `onSettled` callbacks.\n * @returns A promise that resolves with the mutation's data, or rejects with its error.\n * @example\n * ```ts\n * await observer.mutate(\n * { title: 'New post' },\n * { onSuccess: (data) => console.log(data) },\n * )\n * ```\n */\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, this.options)\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TOnMutateResult>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TOnMutateResult>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TOnMutateResult>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n // Snapshot mutateOptions before dispatching — a callback (e.g. onSuccess)\n // may call mutate() again, which overwrites this.#mutateOptions in place.\n // Without the snapshot, onSettled would fire with the *next* mutation's\n // options but the *current* mutation's data/variables.\n const mutateOptions = this.#mutateOptions\n if (mutateOptions && this.hasListeners()) {\n const variables = this.#currentResult.variables!\n const onMutateResult = this.#currentResult.context\n\n const context = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n if (action?.type === 'success') {\n try {\n mutateOptions.onSuccess?.(\n action.data,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n mutateOptions.onSettled?.(\n action.data,\n null,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n } else if (action?.type === 'error') {\n try {\n mutateOptions.onError?.(\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n mutateOptions.onSettled?.(\n undefined,\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,IAAa,mBAAb,cAKUA,qBAAAA,aAER;CAaA,YACE,QACA,SAMA;EACA,MAAM;;;EAbJ,8BAAA,2BAAA,MAAA,gBAAA,KAAA,CAAA;;;EAeF,8BAAA,uBAAA,SAAA,MAAe,MAAA;EACf,KAAK,WAAW,OAAO;EACvB,KAAK,YAAY;EACjB,8BAAA,kBAAA,yBAAA,MAAA,aAAA,CAAA,CAAA,KAAA,IAAmB;CACrB;;;;;CAMA,cAA8B;EAC5B,KAAK,SAAS,KAAK,OAAO,KAAK,IAAI;EACnC,KAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;CACnC;;;;;;;;;;;;;;;;;;CAmBA,WACE,SAMA;;EACA,MAAM,cAAc,KAAK;EAGzB,KAAK,UAAA,8BAAA,uBAAA,SAAU,IAAA,CAAA,CAAa,uBAAuB,OAAO;EAC1D,IAAI,CAACC,cAAAA,oBAAoB,KAAK,SAAS,WAAW,GAChD,8BAAA,uBAAA,SAAA,IAAA,CAAA,CAAa,iBAAiB,CAAC,CAAC,OAAO;GACrC,MAAM;GACN,UAAA,8BAAA,uBAAA,kBAAU,IAAA;GACV,UAAU;EACZ,CAAC;EAGH,KAAA,gBAAA,QAAA,gBAAA,KAAA,IAAA,KAAA,IACE,YAAa,gBACb,KAAK,QAAQ,eACbC,cAAAA,QAAQ,YAAY,WAAW,MAAMA,cAAAA,QAAQ,KAAK,QAAQ,WAAW,GAErE,KAAK,MAAM;OACN,MAAA,2BAAA,8BAAA,uBAAA,kBAAI,IAAA,OAAA,QAAA,6BAAA,KAAA,IAAA,KAAA,IAAA,yBAAuB,MAAM,YAAW,WACjD,8BAAA,uBAAA,kBAAA,IAAA,CAAA,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,sBAAA,KAAA,eACZ,OAAO,MACP,MACA,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;GACF,OAAO,KAAA,WAAA,QAAA,WAAA,KAAA,IAAA,KAAA,IAAI,OAAQ,UAAS,SAAS;IACnC,IAAI;;KACF,CAAA,wBAAA,cAAc,aAAA,QAAA,0BAAA,KAAA,KAAA,sBAAA,KAAA,eACZ,OAAO,OACP,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;IACA,IAAI;;KACF,CAAA,yBAAA,cAAc,eAAA,QAAA,2BAAA,KAAA,KAAA,uBAAA,KAAA,eACZ,KAAA,GACA,OAAO,OACP,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;GACF;EACF;EAGA,KAAK,UAAU,SAAS,aAAa;GACnC,SAAA,8BAAA,uBAAA,gBAAS,IAAA,CAAmB;EAC9B,CAAC;CACH,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"mutationObserver.cjs","names":["Subscribable","shallowEqualObjects","hashKey","getDefaultState"],"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { hashKey, shallowEqualObjects } from './utils'\nimport type { Action, Mutation } from './mutation'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationFunctionContext,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TOnMutateResult> = (\n result: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,\n) => void\n\n// CLASS\n\n/**\n * Observes a single mutation and derives a `MutationObserverResult` from it.\n * A framework hook like `useMutation` creates one `MutationObserver` per hook\n * call, keeps it stable across re-renders, calls `setOptions` when the options\n * passed to the hook change, subscribes to it to re-render on updates, and\n * reads `getCurrentResult()` for the value to return. Calling `mutate()`\n * builds a new underlying `Mutation` in the `MutationCache` and executes it.\n * @example\n * ```ts\n * const observer = new MutationObserver(queryClient, {\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * })\n * ```\n */\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TOnMutateResult>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > = undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TOnMutateResult>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TOnMutateResult>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n /**\n * Binds the methods of the result (`mutate` and `reset`) to the observer, so they keep working when\n * destructured from it.\n */\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n /**\n * Updates the observer's options.\n *\n * If the new `mutationKey` differs from the previous one (and both were\n * defined), the observer is reset, detaching it from the mutation it was\n * observing. Otherwise, if the currently observed mutation is still\n * `pending`, its options are updated in place as well.\n * @param options - The new mutation observer options. They are defaulted with {@link\n * QueryClient#defaultMutationOptions} before being applied.\n * @example\n * ```ts\n * observer.setOptions({\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * onSuccess: (data) => console.log(data),\n * })\n * ```\n */\n setOptions(\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n const prevOptions = this.options as\n | MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n | undefined\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(this.options, prevOptions)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n\n if (\n prevOptions?.mutationKey &&\n this.options.mutationKey &&\n hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)\n ) {\n this.reset()\n } else if (this.#currentMutation?.state.status === 'pending') {\n this.#currentMutation.setOptions(this.options)\n }\n }\n\n protected override onSubscribe(): void {\n if (this.listeners.size === 1 && this.#currentMutation) {\n this.#currentMutation.addObserver(this)\n\n this.#updateResult()\n }\n }\n\n protected override onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n /**\n * Updates the observer's result when the observed mutation changes, and notifies its listeners.\n * @internal\n * @param action - The action that updated the observed mutation, passed on to the observer's\n * callbacks.\n */\n onMutationUpdate(\n action: Action<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n /**\n * Returns the observer's current result, derived from the observed\n * mutation's state (or the default, `idle` state if no mutation has been\n * built yet, e.g. before the first `mutate()` call or after `reset()`).\n * @returns The observer's latest result.\n */\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > {\n return this.#currentResult\n }\n\n /**\n * Detaches the observer from the mutation it is currently observing (if\n * any) and resets the observed result back to its default, `idle` state.\n *\n * This does not cancel an in-flight mutation; the mutation itself keeps\n * running to completion and its own callbacks still fire, but this\n * observer stops reflecting its state and a subsequent `mutate()` call\n * will build a brand new mutation.\n * @see {@link MutationObserver#mutate}\n * @example\n * ```ts\n * observer.reset()\n * ```\n */\n reset(): void {\n // reset needs to remove the observer from the mutation because there is no way to \"get it back\"\n // another mutate call will yield a new mutation!\n this.#currentMutation?.removeObserver(this)\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n /**\n * Builds a new `Mutation` in the `MutationCache` using the observer's\n * current options, detaches this observer from any previously observed\n * mutation, attaches it to the new one, and executes it with the given\n * variables.\n *\n * The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are\n * invoked once the mutation settles, in addition to any callbacks defined\n * on the observer's own options.\n * @param variables - The variables passed to the `mutationFn`.\n * @param options - Per-call `onSuccess`, `onError`, and `onSettled` callbacks.\n * @returns A promise that resolves with the mutation's data, or rejects with its error.\n * @example\n * ```ts\n * await observer.mutate(\n * { title: 'New post' },\n * { onSuccess: (data) => console.log(data) },\n * )\n * ```\n */\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, this.options)\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TOnMutateResult>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TOnMutateResult>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TOnMutateResult>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n // Snapshot mutateOptions before dispatching — a callback (e.g. onSuccess)\n // may call mutate() again, which overwrites this.#mutateOptions in place.\n // Without the snapshot, onSettled would fire with the *next* mutation's\n // options but the *current* mutation's data/variables.\n const mutateOptions = this.#mutateOptions\n if (mutateOptions && this.hasListeners()) {\n const variables = this.#currentResult.variables!\n const onMutateResult = this.#currentResult.context\n\n const context = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n if (action?.type === 'success') {\n try {\n mutateOptions.onSuccess?.(\n action.data,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n mutateOptions.onSettled?.(\n action.data,\n null,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n } else if (action?.type === 'error') {\n try {\n mutateOptions.onError?.(\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n mutateOptions.onSettled?.(\n undefined,\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,IAAa,mBAAb,cAKUA,qBAAAA,aAER;CAaA,YACE,QACA,SAMA;EACA,MAAM;;;EAbJ,8BAAA,2BAAA,MAAA,gBAAA,KAAA,CAAA;;;EAeF,8BAAA,uBAAA,SAAA,MAAe,MAAA;EACf,KAAK,WAAW,OAAO;EACvB,KAAK,YAAY;EACjB,8BAAA,kBAAA,yBAAA,MAAA,aAAA,CAAA,CAAA,KAAA,IAAmB;CACrB;;;;;CAMA,cAA8B;EAC5B,KAAK,SAAS,KAAK,OAAO,KAAK,IAAI;EACnC,KAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;CACnC;;;;;;;;;;;;;;;;;;CAmBA,WACE,SAMA;;EACA,MAAM,cAAc,KAAK;EAGzB,KAAK,UAAA,8BAAA,uBAAA,SAAU,IAAA,CAAA,CAAa,uBAAuB,OAAO;EAC1D,IAAI,CAACC,cAAAA,oBAAoB,KAAK,SAAS,WAAW,GAChD,8BAAA,uBAAA,SAAA,IAAA,CAAA,CAAa,iBAAiB,CAAC,CAAC,OAAO;GACrC,MAAM;GACN,UAAA,8BAAA,uBAAA,kBAAU,IAAA;GACV,UAAU;EACZ,CAAC;EAGH,KAAA,gBAAA,QAAA,gBAAA,KAAA,IAAA,KAAA,IACE,YAAa,gBACb,KAAK,QAAQ,eACbC,cAAAA,QAAQ,YAAY,WAAW,MAAMA,cAAAA,QAAQ,KAAK,QAAQ,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AA,KAAA,KAAA,sBAAA,KAAA,eACZ,OAAO,MACP,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;IACA,IAAI;;KACF,CAAA,wBAAA,cAAc,eAAA,QAAA,0BAAA,KAAA,KAAA,sBAAA,KAAA,eACZ,OAAO,MACP,MACA,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;GACF,OAAO,KAAA,WAAA,QAAA,WAAA,KAAA,IAAA,KAAA,IAAI,OAAQ,UAAS,SAAS;IACnC,IAAI;;KACF,CAAA,wBAAA,cAAc,aAAA,QAAA,0BAAA,KAAA,KAAA,sBAAA,KAAA,eACZ,OAAO,OACP,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;IACA,IAAI;;KACF,CAAA,yBAAA,cAAc,eAAA,QAAA,2BAAA,KAAA,KAAA,uBAAA,KAAA,eACZ,KAAA,GACA,OAAO,OACP,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;GACF;EACF;EAGA,KAAK,UAAU,SAAS,aAAa;GACnC,SAAA,8BAAA,uBAAA,gBAAS,IAAA,CAAmB;EAC9B,CAAC;CACH,CAAC;AACH"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { g as MutationObserver } from "./hydration-DRPiSXsi.cjs";
|
|
2
2
|
export { MutationObserver };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { g as MutationObserver } from "./hydration-Bwo9mbMU.js";
|
|
2
2
|
export { MutationObserver };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutationObserver.js","names":[],"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { hashKey, shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationFunctionContext,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TOnMutateResult> = (\n result: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,\n) => void\n\n// CLASS\n\n/**\n * Observes a single mutation and derives a `MutationObserverResult` from it.\n * A framework hook like `useMutation` creates one `MutationObserver` per hook\n * call, keeps it stable across re-renders, calls `setOptions` when the options\n * passed to the hook change, subscribes to it to re-render on updates, and\n * reads `getCurrentResult()` for the value to return. Calling `mutate()`\n * builds a new underlying `Mutation` in the `MutationCache` and executes it.\n * @example\n * ```ts\n * const observer = new MutationObserver(queryClient, {\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * })\n * ```\n */\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TOnMutateResult>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > = undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TOnMutateResult>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TOnMutateResult>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n /**\n * Binds the methods of the result (`mutate` and `reset`) to the observer, so they keep working when\n * destructured from it.\n */\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n /**\n * Updates the observer's options.\n *\n * If the new `mutationKey` differs from the previous one (and both were\n * defined), the observer is reset, detaching it from the mutation it was\n * observing. Otherwise, if the currently observed mutation is still\n * `pending`, its options are updated in place as well.\n * @param options - The new mutation observer options. They are defaulted with {@link\n * QueryClient#defaultMutationOptions} before being applied.\n * @example\n * ```ts\n * observer.setOptions({\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * onSuccess: (data) => console.log(data),\n * })\n * ```\n */\n setOptions(\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n const prevOptions = this.options as\n | MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n | undefined\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(this.options, prevOptions)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n\n if (\n prevOptions?.mutationKey &&\n this.options.mutationKey &&\n hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)\n ) {\n this.reset()\n } else if (this.#currentMutation?.state.status === 'pending') {\n this.#currentMutation.setOptions(this.options)\n }\n }\n\n protected override onSubscribe(): void {\n if (this.listeners.size === 1 && this.#currentMutation) {\n this.#currentMutation.addObserver(this)\n\n this.#updateResult()\n }\n }\n\n protected override onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n /**\n * Updates the observer's result when the observed mutation changes, and notifies its listeners.\n * @internal\n * @param action - The action that updated the observed mutation, passed on to the observer's\n * callbacks.\n */\n onMutationUpdate(\n action: Action<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n /**\n * Returns the observer's current result, derived from the observed\n * mutation's state (or the default, `idle` state if no mutation has been\n * built yet, e.g. before the first `mutate()` call or after `reset()`).\n * @returns The observer's latest result.\n */\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > {\n return this.#currentResult\n }\n\n /**\n * Detaches the observer from the mutation it is currently observing (if\n * any) and resets the observed result back to its default, `idle` state.\n *\n * This does not cancel an in-flight mutation; the mutation itself keeps\n * running to completion and its own callbacks still fire, but this\n * observer stops reflecting its state and a subsequent `mutate()` call\n * will build a brand new mutation.\n * @see {@link MutationObserver#mutate}\n * @example\n * ```ts\n * observer.reset()\n * ```\n */\n reset(): void {\n // reset needs to remove the observer from the mutation because there is no way to \"get it back\"\n // another mutate call will yield a new mutation!\n this.#currentMutation?.removeObserver(this)\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n /**\n * Builds a new `Mutation` in the `MutationCache` using the observer's\n * current options, detaches this observer from any previously observed\n * mutation, attaches it to the new one, and executes it with the given\n * variables.\n *\n * The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are\n * invoked once the mutation settles, in addition to any callbacks defined\n * on the observer's own options.\n * @param variables - The variables passed to the `mutationFn`.\n * @param options - Per-call `onSuccess`, `onError`, and `onSettled` callbacks.\n * @returns A promise that resolves with the mutation's data, or rejects with its error.\n * @example\n * ```ts\n * await observer.mutate(\n * { title: 'New post' },\n * { onSuccess: (data) => console.log(data) },\n * )\n * ```\n */\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, this.options)\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TOnMutateResult>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TOnMutateResult>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TOnMutateResult>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n // Snapshot mutateOptions before dispatching — a callback (e.g. onSuccess)\n // may call mutate() again, which overwrites this.#mutateOptions in place.\n // Without the snapshot, onSettled would fire with the *next* mutation's\n // options but the *current* mutation's data/variables.\n const mutateOptions = this.#mutateOptions\n if (mutateOptions && this.hasListeners()) {\n const variables = this.#currentResult.variables!\n const onMutateResult = this.#currentResult.context\n\n const context = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n if (action?.type === 'success') {\n try {\n mutateOptions.onSuccess?.(\n action.data,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n mutateOptions.onSettled?.(\n action.data,\n null,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n } else if (action?.type === 'error') {\n try {\n mutateOptions.onError?.(\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n mutateOptions.onSettled?.(\n undefined,\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,IAAa,mBAAb,cAKU,aAER;CAaA,YACE,QACA,SAMA;EACA,MAAM;;;EAbJ,2BAAA,MAAA,gBAAA,KAAA,CAAA;;;EAeF,uBAAA,SAAA,MAAe,MAAA;EACf,KAAK,WAAW,OAAO;EACvB,KAAK,YAAY;EACjB,kBAAA,yBAAA,MAAA,aAAA,CAAA,CAAA,KAAA,IAAmB;CACrB;;;;;CAMA,cAA8B;EAC5B,KAAK,SAAS,KAAK,OAAO,KAAK,IAAI;EACnC,KAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;CACnC;;;;;;;;;;;;;;;;;;CAmBA,WACE,SAMA;;EACA,MAAM,cAAc,KAAK;EAGzB,KAAK,UAAA,uBAAA,SAAU,IAAA,CAAA,CAAa,uBAAuB,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AAA,WAAA,KAAA,IAAA,KAAA,IAAI,OAAQ,UAAS,WAAW;IAC9B,IAAI;;KACF,CAAA,wBAAA,cAAc,eAAA,QAAA,0BAAA,KAAA,KAAA,sBAAA,KAAA,eACZ,OAAO,MACP,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;IACA,IAAI;;KACF,CAAA,wBAAA,cAAc,eAAA,QAAA,0BAAA,KAAA,KAAA,sBAAA,KAAA,eACZ,OAAO,MACP,MACA,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;GACF,OAAO,KAAA,WAAA,QAAA,WAAA,KAAA,IAAA,KAAA,IAAI,OAAQ,UAAS,SAAS;IACnC,IAAI;;KACF,CAAA,wBAAA,cAAc,aAAA,QAAA,0BAAA,KAAA,KAAA,sBAAA,KAAA,eACZ,OAAO,OACP,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;IACA,IAAI;;KACF,CAAA,yBAAA,cAAc,eAAA,QAAA,2BAAA,KAAA,KAAA,uBAAA,KAAA,eACZ,KAAA,GACA,OAAO,OACP,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;GACF;EACF;EAGA,KAAK,UAAU,SAAS,aAAa;GACnC,SAAA,uBAAA,gBAAS,IAAA,CAAmB;EAC9B,CAAC;CACH,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"mutationObserver.js","names":[],"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { hashKey, shallowEqualObjects } from './utils'\nimport type { Action, Mutation } from './mutation'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationFunctionContext,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TOnMutateResult> = (\n result: MutationObserverResult<TData, TError, TVariables, TOnMutateResult>,\n) => void\n\n// CLASS\n\n/**\n * Observes a single mutation and derives a `MutationObserverResult` from it.\n * A framework hook like `useMutation` creates one `MutationObserver` per hook\n * call, keeps it stable across re-renders, calls `setOptions` when the options\n * passed to the hook change, subscribes to it to re-render on updates, and\n * reads `getCurrentResult()` for the value to return. Calling `mutate()`\n * builds a new underlying `Mutation` in the `MutationCache` and executes it.\n * @example\n * ```ts\n * const observer = new MutationObserver(queryClient, {\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * })\n * ```\n */\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TOnMutateResult>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > = undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TOnMutateResult>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TOnMutateResult>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n /**\n * Binds the methods of the result (`mutate` and `reset`) to the observer, so they keep working when\n * destructured from it.\n */\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n /**\n * Updates the observer's options.\n *\n * If the new `mutationKey` differs from the previous one (and both were\n * defined), the observer is reset, detaching it from the mutation it was\n * observing. Otherwise, if the currently observed mutation is still\n * `pending`, its options are updated in place as well.\n * @param options - The new mutation observer options. They are defaulted with {@link\n * QueryClient#defaultMutationOptions} before being applied.\n * @example\n * ```ts\n * observer.setOptions({\n * mutationFn: (variables: { title: string }) => addPost(variables),\n * onSuccess: (data) => console.log(data),\n * })\n * ```\n */\n setOptions(\n options: MutationObserverOptions<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n >,\n ) {\n const prevOptions = this.options as\n | MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>\n | undefined\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(this.options, prevOptions)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n\n if (\n prevOptions?.mutationKey &&\n this.options.mutationKey &&\n hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)\n ) {\n this.reset()\n } else if (this.#currentMutation?.state.status === 'pending') {\n this.#currentMutation.setOptions(this.options)\n }\n }\n\n protected override onSubscribe(): void {\n if (this.listeners.size === 1 && this.#currentMutation) {\n this.#currentMutation.addObserver(this)\n\n this.#updateResult()\n }\n }\n\n protected override onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n /**\n * Updates the observer's result when the observed mutation changes, and notifies its listeners.\n * @internal\n * @param action - The action that updated the observed mutation, passed on to the observer's\n * callbacks.\n */\n onMutationUpdate(\n action: Action<TData, TError, TVariables, TOnMutateResult>,\n ): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n /**\n * Returns the observer's current result, derived from the observed\n * mutation's state (or the default, `idle` state if no mutation has been\n * built yet, e.g. before the first `mutate()` call or after `reset()`).\n * @returns The observer's latest result.\n */\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TOnMutateResult\n > {\n return this.#currentResult\n }\n\n /**\n * Detaches the observer from the mutation it is currently observing (if\n * any) and resets the observed result back to its default, `idle` state.\n *\n * This does not cancel an in-flight mutation; the mutation itself keeps\n * running to completion and its own callbacks still fire, but this\n * observer stops reflecting its state and a subsequent `mutate()` call\n * will build a brand new mutation.\n * @see {@link MutationObserver#mutate}\n * @example\n * ```ts\n * observer.reset()\n * ```\n */\n reset(): void {\n // reset needs to remove the observer from the mutation because there is no way to \"get it back\"\n // another mutate call will yield a new mutation!\n this.#currentMutation?.removeObserver(this)\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n /**\n * Builds a new `Mutation` in the `MutationCache` using the observer's\n * current options, detaches this observer from any previously observed\n * mutation, attaches it to the new one, and executes it with the given\n * variables.\n *\n * The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are\n * invoked once the mutation settles, in addition to any callbacks defined\n * on the observer's own options.\n * @param variables - The variables passed to the `mutationFn`.\n * @param options - Per-call `onSuccess`, `onError`, and `onSettled` callbacks.\n * @returns A promise that resolves with the mutation's data, or rejects with its error.\n * @example\n * ```ts\n * await observer.mutate(\n * { title: 'New post' },\n * { onSuccess: (data) => console.log(data) },\n * )\n * ```\n */\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,\n ): Promise<TData> {\n this.#mutateOptions = options\n\n this.#currentMutation?.removeObserver(this)\n\n this.#currentMutation = this.#client\n .getMutationCache()\n .build(this.#client, this.options)\n\n this.#currentMutation.addObserver(this)\n\n return this.#currentMutation.execute(variables)\n }\n\n #updateResult(): void {\n const state =\n this.#currentMutation?.state ??\n getDefaultState<TData, TError, TVariables, TOnMutateResult>()\n\n this.#currentResult = {\n ...state,\n isPending: state.status === 'pending',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n } as MutationObserverResult<TData, TError, TVariables, TOnMutateResult>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TOnMutateResult>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n // Snapshot mutateOptions before dispatching — a callback (e.g. onSuccess)\n // may call mutate() again, which overwrites this.#mutateOptions in place.\n // Without the snapshot, onSettled would fire with the *next* mutation's\n // options but the *current* mutation's data/variables.\n const mutateOptions = this.#mutateOptions\n if (mutateOptions && this.hasListeners()) {\n const variables = this.#currentResult.variables!\n const onMutateResult = this.#currentResult.context\n\n const context = {\n client: this.#client,\n meta: this.options.meta,\n mutationKey: this.options.mutationKey,\n } satisfies MutationFunctionContext\n\n if (action?.type === 'success') {\n try {\n mutateOptions.onSuccess?.(\n action.data,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n mutateOptions.onSettled?.(\n action.data,\n null,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n } else if (action?.type === 'error') {\n try {\n mutateOptions.onError?.(\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n try {\n mutateOptions.onSettled?.(\n undefined,\n action.error,\n variables,\n onMutateResult,\n context,\n )\n } catch (e) {\n void Promise.reject(e)\n }\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,IAAa,mBAAb,cAKU,aAER;CAaA,YACE,QACA,SAMA;EACA,MAAM;;;EAbJ,2BAAA,MAAA,gBAAA,KAAA,CAAA;;;EAeF,uBAAA,SAAA,MAAe,MAAA;EACf,KAAK,WAAW,OAAO;EACvB,KAAK,YAAY;EACjB,kBAAA,yBAAA,MAAA,aAAA,CAAA,CAAA,KAAA,IAAmB;CACrB;;;;;CAMA,cAA8B;EAC5B,KAAK,SAAS,KAAK,OAAO,KAAK,IAAI;EACnC,KAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;CACnC;;;;;;;;;;;;;;;;;;CAmBA,WACE,SAMA;;EACA,MAAM,cAAc,KAAK;EAGzB,KAAK,UAAA,uBAAA,SAAU,IAAA,CAAA,CAAa,uBAAuB,OAAO;EAC1D,IAAI,CAAC,oBAAoB,KAAK,SAAS,WAAW,GAChD,uBAAA,SAAA,IAAA,CAAA,CAAa,iBAAiB,CAAC,CAAC,OAAO;GACrC,MAAM;GACN,UAAA,uBAAA,kBAAU,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,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;IACA,IAAI;;KACF,CAAA,wBAAA,cAAc,eAAA,QAAA,0BAAA,KAAA,KAAA,sBAAA,KAAA,eACZ,OAAO,MACP,MACA,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;GACF,OAAO,KAAA,WAAA,QAAA,WAAA,KAAA,IAAA,KAAA,IAAI,OAAQ,UAAS,SAAS;IACnC,IAAI;;KACF,CAAA,wBAAA,cAAc,aAAA,QAAA,0BAAA,KAAA,KAAA,sBAAA,KAAA,eACZ,OAAO,OACP,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;IACA,IAAI;;KACF,CAAA,yBAAA,cAAc,eAAA,QAAA,2BAAA,KAAA,KAAA,uBAAA,KAAA,eACZ,KAAA,GACA,OAAO,OACP,WACA,gBACA,OACF;IACF,SAAS,GAAG;KACV,QAAa,OAAO,CAAC;IACvB;GACF;EACF;EAGA,KAAK,UAAU,SAAS,aAAa;GACnC,SAAA,uBAAA,gBAAS,IAAA,CAAmB;EAC9B,CAAC;CACH,CAAC;AACH"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queriesObserver.cjs","names":["Subscribable","shallowEqualObjects","replaceEqualDeep","QueryObserver"],"sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\nimport { replaceEqualDeep, shallowEqualObjects } from './utils'\nimport type {\n DefaultedQueryObserverOptions,\n QueryObserverOptions,\n QueryObserverResult,\n} from './types'\nimport type { QueryClient } from './queryClient'\n\n/**\n * Returns the items of `array1` that are not in `array2`.\n * @param array1 - The items to filter.\n * @param array2 - The items to exclude.\n * @returns A new array with the items of `array1` that are not in `array2`.\n */\nfunction difference<T>(array1: Array<T>, array2: Array<T>): Array<T> {\n const excludeSet = new Set(array2)\n return array1.filter((x) => !excludeSet.has(x))\n}\n\ntype QueriesObserverListener = (result: Array<QueryObserverResult>) => void\n\ntype CombineFn<TCombinedResult> = (\n result: Array<QueryObserverResult>,\n) => TCombinedResult\n\n/**\n * Options for a `QueriesObserver` that apply to all of its queries at once.\n */\nexport interface QueriesObserverOptions<\n TCombinedResult = Array<QueryObserverResult>,\n> {\n /**\n * A function that combines the array of `QueryObserverResult`s (one per\n * observed query) into a single value. The combined value is memoized and\n * only recomputed when one of the underlying results, the query hashes, or\n * the `combine` function itself changes.\n *\n * Defaults to returning the array of `QueryObserverResult`s unchanged.\n */\n combine?: CombineFn<TCombinedResult>\n}\n\n/**\n * A `QueriesObserver` watches an array of queries at once, exposing them as\n * a single array of `QueryObserverResult`s (or, when a `combine` option is\n * given, as a combined value derived from that array). It manages one\n * internal `QueryObserver` per query, and is the primitive that framework\n * adapters (e.g. `useQueries`) build their hooks on top of.\n * @example\n * ```ts\n * const observer = new QueriesObserver(queryClient, [\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 2], queryFn: fetchPost },\n * ])\n *\n * const unsubscribe = observer.subscribe((result) => {\n * console.log(result)\n * })\n * ```\n */\nexport class QueriesObserver<\n TCombinedResult = Array<QueryObserverResult>,\n> extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result!: Array<QueryObserverResult>\n #queries: Array<QueryObserverOptions>\n #options?: QueriesObserverOptions<TCombinedResult>\n #observers: Array<QueryObserver>\n #combinedResult!: TCombinedResult\n #lastCombine?: CombineFn<TCombinedResult>\n #lastResult?: Array<QueryObserverResult>\n #lastQueryHashes?: Array<string>\n #observerMatches: Array<QueryObserverMatch> = []\n\n constructor(\n client: QueryClient,\n queries: Array<QueryObserverOptions<any, any, any, any, any>>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ) {\n super()\n\n this.#client = client\n this.#options = options\n this.#queries = []\n this.#observers = []\n this.#result = []\n\n this.setQueries(queries)\n }\n\n protected override onSubscribe(): void {\n if (this.listeners.size === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected override onUnsubscribe(): void {\n if (!this.listeners.size) {\n this.destroy()\n }\n }\n\n /**\n * Stops observing all queries: clears all listeners and destroys every\n * underlying `QueryObserver` this observer manages.\n */\n destroy(): void {\n this.listeners = new Set()\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n /**\n * Replaces the set of queries being observed. Existing `QueryObserver`s\n * are reused for queries that match an already-observed query hash;\n * observers for queries that are no longer present are destroyed, and new\n * observers are created and subscribed to for newly added queries.\n * @param queries - The options of the queries to observe.\n * @param options - Replaces the observer's options, e.g. its `combine` function.\n * @example\n * ```ts\n * observer.setQueries([\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 3], queryFn: fetchPost },\n * ])\n * ```\n */\n setQueries(\n queries: Array<QueryObserverOptions>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ): void {\n this.#queries = queries\n this.#options = options\n\n if (process.env.NODE_ENV !== 'production') {\n const queryHashes = queries.map(\n (query) => this.#client.defaultQueryOptions(query).queryHash,\n )\n if (new Set(queryHashes).size !== queryHashes.length) {\n console.warn(\n '[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.',\n )\n }\n }\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasLengthChange = prevObservers.length !== newObservers.length\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n const hasStructuralChange = hasLengthChange || hasIndexChange\n\n const hasResultChange = hasStructuralChange\n ? true\n : newResult.some((result, index) => {\n const prev = this.#result[index]\n return !prev || !shallowEqualObjects(result, prev)\n })\n\n if (!hasStructuralChange && !hasResultChange) return\n\n if (hasStructuralChange) {\n this.#observerMatches = newObserverMatches\n this.#observers = newObservers\n }\n\n this.#result = newResult\n\n if (!this.hasListeners()) return\n\n if (hasStructuralChange) {\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n\n this.#notify()\n })\n }\n\n /**\n * Returns the most recently computed array of `QueryObserverResult`s, one\n * per observed query, in the same order as the queries passed to the\n * constructor or `setQueries`.\n * @returns The current results.\n * @example\n * ```ts\n * const results = observer.getCurrentResult()\n * const data = results.map((result) => result.data)\n * ```\n */\n getCurrentResult(): Array<QueryObserverResult> {\n return this.#result\n }\n\n /**\n * Returns the underlying `Query` instances currently being observed, in\n * the same order as the queries passed to the constructor or `setQueries`.\n * @returns The observed queries.\n */\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n /**\n * Returns the underlying `QueryObserver` instances this observer manages,\n * in the same order as the queries passed to the constructor or\n * `setQueries`.\n * @returns The managed query observers.\n */\n getObservers() {\n return this.#observers\n }\n\n /**\n * The `QueriesObserver` counterpart of {@link QueryObserver#getOptimisticResult} — computes\n * the result for the given (already-defaulted) queries right now, synchronously. Called by\n * framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw\n * per-query results, a function to compute the combined result from them, and a function to\n * wrap the results for property-access tracking.\n * @param queries - The defaulted options of the queries to compute the result for.\n * @param combine - The `combine` function used by the returned `combineResult`, if any.\n * @returns A tuple of the per-query results, a function that computes the combined result, and a\n * function that returns the results wrapped for property-access tracking.\n */\n getOptimisticResult(\n queries: Array<QueryObserverOptions>,\n combine: CombineFn<TCombinedResult> | undefined,\n ): [\n rawResult: Array<QueryObserverResult>,\n combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult,\n trackResult: () => Array<QueryObserverResult>,\n ] {\n const matches = this.#findMatchingObservers(queries)\n const result = matches.map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n const queryHashes = matches.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n\n return [\n result,\n (r?: Array<QueryObserverResult>) => {\n return this.#combineResult(r ?? result, combine, queryHashes)\n },\n () => {\n return this.#trackResult(result, matches)\n },\n ]\n }\n\n #trackResult(\n result: Array<QueryObserverResult>,\n matches: Array<QueryObserverMatch>,\n ) {\n const trackedProps = new Set<keyof QueryObserverResult>()\n\n return matches.map((match, index) => {\n const observerResult = result[index]!\n return !match.defaultedQueryOptions.notifyOnChangeProps\n ? match.observer.trackResult(observerResult, (accessedProp) => {\n // track property on all observers to ensure proper (synchronized) tracking (#7000)\n if (!trackedProps.has(accessedProp)) {\n trackedProps.add(accessedProp)\n matches.forEach((m) => {\n m.observer.trackProp(accessedProp)\n })\n }\n })\n : observerResult\n })\n }\n\n #combineResult(\n input: Array<QueryObserverResult>,\n combine: CombineFn<TCombinedResult> | undefined,\n queryHashes?: Array<string>,\n ): TCombinedResult {\n if (combine) {\n const lastHashes = this.#lastQueryHashes\n const queryHashesChanged =\n queryHashes !== undefined &&\n lastHashes !== undefined &&\n (lastHashes.length !== queryHashes.length ||\n queryHashes.some((hash, i) => hash !== lastHashes[i]))\n\n if (\n this.#result !== this.#lastResult ||\n queryHashesChanged ||\n combine !== this.#lastCombine\n ) {\n this.#lastCombine = combine\n this.#lastResult = this.#result\n\n if (queryHashes !== undefined) {\n this.#lastQueryHashes = queryHashes\n }\n this.#combinedResult = replaceEqualDeep(\n this.#combinedResult,\n combine(input),\n )\n }\n\n return this.#combinedResult\n }\n return input as any\n }\n\n #shouldSkipCombine(): boolean {\n return (\n !this.#options?.combine ||\n this.#observers.some((observer, index) => {\n return (\n observer.options.suspense && this.#result[index]?.data === undefined\n )\n })\n )\n }\n\n #findMatchingObservers(\n queries: Array<QueryObserverOptions>,\n ): Array<QueryObserverMatch> {\n const prevObserversMap = new Map<string, Array<QueryObserver>>()\n\n this.#observers.forEach((observer) => {\n const key = observer.options.queryHash\n if (!key) return\n\n const previousObservers = prevObserversMap.get(key)\n\n if (previousObservers) {\n previousObservers.push(observer)\n } else {\n prevObserversMap.set(key, [observer])\n }\n })\n\n const observers: Array<QueryObserverMatch> = []\n\n queries.forEach((options) => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const match = prevObserversMap.get(defaultedOptions.queryHash)?.shift()\n const observer =\n match ?? new QueryObserver(this.#client, defaultedOptions)\n\n observers.push({\n defaultedQueryOptions: defaultedOptions,\n observer,\n })\n })\n\n return observers\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = this.#result.slice()\n this.#result[index] = result\n this.#notify()\n }\n }\n\n #notify(): void {\n if (this.hasListeners()) {\n const shouldSkipCombine = this.#shouldSkipCombine()\n const previousResult = this.#combinedResult\n const newResult = shouldSkipCombine\n ? previousResult\n : this.#combineResult(\n this.#trackResult(this.#result, this.#observerMatches),\n this.#options?.combine,\n )\n\n if (shouldSkipCombine || previousResult !== newResult) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n }\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"mappings":";;;;;;;;;;;;;;AAiBA,SAAS,WAAc,QAAkB,QAA4B;CACnE,MAAM,aAAa,IAAI,IAAI,MAAM;CACjC,OAAO,OAAO,QAAQ,MAAM,CAAC,WAAW,IAAI,CAAC,CAAC;AAChD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,kBAAb,cAEUA,qBAAAA,aAAsC;CAY9C,YACE,QACA,SACA,SACA;EACA,MAAM;;;;;;;;;;;EAPsC,8BAAA,2BAAA,MAAA,kBAAA,CAAC,CAAA;EAS7C,8BAAA,uBAAA,SAAA,MAAe,MA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","shallowEqualObjects","replaceEqualDeep","QueryObserver"],"sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\nimport { replaceEqualDeep, shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultedQueryObserverOptions,\n QueryObserverOptions,\n QueryObserverResult,\n} from './types'\n\n/**\n * Returns the items of `array1` that are not in `array2`.\n * @param array1 - The items to filter.\n * @param array2 - The items to exclude.\n * @returns A new array with the items of `array1` that are not in `array2`.\n */\nfunction difference<T>(array1: Array<T>, array2: Array<T>): Array<T> {\n const excludeSet = new Set(array2)\n return array1.filter((x) => !excludeSet.has(x))\n}\n\ntype QueriesObserverListener = (result: Array<QueryObserverResult>) => void\n\ntype CombineFn<TCombinedResult> = (\n result: Array<QueryObserverResult>,\n) => TCombinedResult\n\n/**\n * Options for a `QueriesObserver` that apply to all of its queries at once.\n */\nexport interface QueriesObserverOptions<\n TCombinedResult = Array<QueryObserverResult>,\n> {\n /**\n * A function that combines the array of `QueryObserverResult`s (one per\n * observed query) into a single value. The combined value is memoized and\n * only recomputed when one of the underlying results, the query hashes, or\n * the `combine` function itself changes.\n *\n * Defaults to returning the array of `QueryObserverResult`s unchanged.\n */\n combine?: CombineFn<TCombinedResult>\n}\n\n/**\n * A `QueriesObserver` watches an array of queries at once, exposing them as\n * a single array of `QueryObserverResult`s (or, when a `combine` option is\n * given, as a combined value derived from that array). It manages one\n * internal `QueryObserver` per query, and is the primitive that framework\n * adapters (e.g. `useQueries`) build their hooks on top of.\n * @example\n * ```ts\n * const observer = new QueriesObserver(queryClient, [\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 2], queryFn: fetchPost },\n * ])\n *\n * const unsubscribe = observer.subscribe((result) => {\n * console.log(result)\n * })\n * ```\n */\nexport class QueriesObserver<\n TCombinedResult = Array<QueryObserverResult>,\n> extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result!: Array<QueryObserverResult>\n #queries: Array<QueryObserverOptions>\n #options?: QueriesObserverOptions<TCombinedResult>\n #observers: Array<QueryObserver>\n #combinedResult!: TCombinedResult\n #lastCombine?: CombineFn<TCombinedResult>\n #lastResult?: Array<QueryObserverResult>\n #lastQueryHashes?: Array<string>\n #observerMatches: Array<QueryObserverMatch> = []\n\n constructor(\n client: QueryClient,\n queries: Array<QueryObserverOptions<any, any, any, any, any>>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ) {\n super()\n\n this.#client = client\n this.#options = options\n this.#queries = []\n this.#observers = []\n this.#result = []\n\n this.setQueries(queries)\n }\n\n protected override onSubscribe(): void {\n if (this.listeners.size === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected override onUnsubscribe(): void {\n if (!this.listeners.size) {\n this.destroy()\n }\n }\n\n /**\n * Stops observing all queries: clears all listeners and destroys every\n * underlying `QueryObserver` this observer manages.\n */\n destroy(): void {\n this.listeners = new Set()\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n /**\n * Replaces the set of queries being observed. Existing `QueryObserver`s\n * are reused for queries that match an already-observed query hash;\n * observers for queries that are no longer present are destroyed, and new\n * observers are created and subscribed to for newly added queries.\n * @param queries - The options of the queries to observe.\n * @param options - Replaces the observer's options, e.g. its `combine` function.\n * @example\n * ```ts\n * observer.setQueries([\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 3], queryFn: fetchPost },\n * ])\n * ```\n */\n setQueries(\n queries: Array<QueryObserverOptions>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ): void {\n this.#queries = queries\n this.#options = options\n\n if (process.env.NODE_ENV !== 'production') {\n const queryHashes = queries.map(\n (query) => this.#client.defaultQueryOptions(query).queryHash,\n )\n if (new Set(queryHashes).size !== queryHashes.length) {\n console.warn(\n '[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.',\n )\n }\n }\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasLengthChange = prevObservers.length !== newObservers.length\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n const hasStructuralChange = hasLengthChange || hasIndexChange\n\n const hasResultChange = hasStructuralChange\n ? true\n : newResult.some((result, index) => {\n const prev = this.#result[index]\n return !prev || !shallowEqualObjects(result, prev)\n })\n\n if (!hasStructuralChange && !hasResultChange) return\n\n if (hasStructuralChange) {\n this.#observerMatches = newObserverMatches\n this.#observers = newObservers\n }\n\n this.#result = newResult\n\n if (!this.hasListeners()) return\n\n if (hasStructuralChange) {\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n\n this.#notify()\n })\n }\n\n /**\n * Returns the most recently computed array of `QueryObserverResult`s, one\n * per observed query, in the same order as the queries passed to the\n * constructor or `setQueries`.\n * @returns The current results.\n * @example\n * ```ts\n * const results = observer.getCurrentResult()\n * const data = results.map((result) => result.data)\n * ```\n */\n getCurrentResult(): Array<QueryObserverResult> {\n return this.#result\n }\n\n /**\n * Returns the underlying `Query` instances currently being observed, in\n * the same order as the queries passed to the constructor or `setQueries`.\n * @returns The observed queries.\n */\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n /**\n * Returns the underlying `QueryObserver` instances this observer manages,\n * in the same order as the queries passed to the constructor or\n * `setQueries`.\n * @returns The managed query observers.\n */\n getObservers() {\n return this.#observers\n }\n\n /**\n * The `QueriesObserver` counterpart of {@link QueryObserver#getOptimisticResult} — computes\n * the result for the given (already-defaulted) queries right now, synchronously. Called by\n * framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw\n * per-query results, a function to compute the combined result from them, and a function to\n * wrap the results for property-access tracking.\n * @param queries - The defaulted options of the queries to compute the result for.\n * @param combine - The `combine` function used by the returned `combineResult`, if any.\n * @returns A tuple of the per-query results, a function that computes the combined result, and a\n * function that returns the results wrapped for property-access tracking.\n */\n getOptimisticResult(\n queries: Array<QueryObserverOptions>,\n combine: CombineFn<TCombinedResult> | undefined,\n ): [\n rawResult: Array<QueryObserverResult>,\n combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult,\n trackResult: () => Array<QueryObserverResult>,\n ] {\n const matches = this.#findMatchingObservers(queries)\n const result = matches.map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n const queryHashes = matches.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n\n return [\n result,\n (r?: Array<QueryObserverResult>) => {\n return this.#combineResult(r ?? result, combine, queryHashes)\n },\n () => {\n return this.#trackResult(result, matches)\n },\n ]\n }\n\n #trackResult(\n result: Array<QueryObserverResult>,\n matches: Array<QueryObserverMatch>,\n ) {\n const trackedProps = new Set<keyof QueryObserverResult>()\n\n return matches.map((match, index) => {\n const observerResult = result[index]!\n return !match.defaultedQueryOptions.notifyOnChangeProps\n ? match.observer.trackResult(observerResult, (accessedProp) => {\n // track property on all observers to ensure proper (synchronized) tracking (#7000)\n if (!trackedProps.has(accessedProp)) {\n trackedProps.add(accessedProp)\n matches.forEach((m) => {\n m.observer.trackProp(accessedProp)\n })\n }\n })\n : observerResult\n })\n }\n\n #combineResult(\n input: Array<QueryObserverResult>,\n combine: CombineFn<TCombinedResult> | undefined,\n queryHashes?: Array<string>,\n ): TCombinedResult {\n if (combine) {\n const lastHashes = this.#lastQueryHashes\n const queryHashesChanged =\n queryHashes !== undefined &&\n lastHashes !== undefined &&\n (lastHashes.length !== queryHashes.length ||\n queryHashes.some((hash, i) => hash !== lastHashes[i]))\n\n if (\n this.#result !== this.#lastResult ||\n queryHashesChanged ||\n combine !== this.#lastCombine\n ) {\n this.#lastCombine = combine\n this.#lastResult = this.#result\n\n if (queryHashes !== undefined) {\n this.#lastQueryHashes = queryHashes\n }\n this.#combinedResult = replaceEqualDeep(\n this.#combinedResult,\n combine(input),\n )\n }\n\n return this.#combinedResult\n }\n return input as any\n }\n\n #shouldSkipCombine(): boolean {\n return (\n !this.#options?.combine ||\n this.#observers.some((observer, index) => {\n return (\n observer.options.suspense && this.#result[index]?.data === undefined\n )\n })\n )\n }\n\n #findMatchingObservers(\n queries: Array<QueryObserverOptions>,\n ): Array<QueryObserverMatch> {\n const prevObserversMap = new Map<string, Array<QueryObserver>>()\n\n this.#observers.forEach((observer) => {\n const key = observer.options.queryHash\n if (!key) return\n\n const previousObservers = prevObserversMap.get(key)\n\n if (previousObservers) {\n previousObservers.push(observer)\n } else {\n prevObserversMap.set(key, [observer])\n }\n })\n\n const observers: Array<QueryObserverMatch> = []\n\n queries.forEach((options) => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const match = prevObserversMap.get(defaultedOptions.queryHash)?.shift()\n const observer =\n match ?? new QueryObserver(this.#client, defaultedOptions)\n\n observers.push({\n defaultedQueryOptions: defaultedOptions,\n observer,\n })\n })\n\n return observers\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = this.#result.slice()\n this.#result[index] = result\n this.#notify()\n }\n }\n\n #notify(): void {\n if (this.hasListeners()) {\n const shouldSkipCombine = this.#shouldSkipCombine()\n const previousResult = this.#combinedResult\n const newResult = shouldSkipCombine\n ? previousResult\n : this.#combineResult(\n this.#trackResult(this.#result, this.#observerMatches),\n this.#options?.combine,\n )\n\n if (shouldSkipCombine || previousResult !== newResult) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n }\n }\n}\n\ntype QueryObserverMatch = {\n default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ACE,MAAA,YAAA,CAAA,CAAA,KAAA,MAAA,8BAAA,uBAAA,SAAkB,IAAA,GAAA,8BAAA,uBAAA,kBAAc,IAAA,CAAqB,IAAA,yBAAA,8BAAA,uBAAA,UACrD,IAAA,OAAA,QAAA,2BAAA,KAAA,IAAA,KAAA,IAAA,uBAAe,OACjB;EAEJ,IAAI,qBAAqB,mBAAmB,WAC1C,sBAAA,cAAc,YAAY;GACxB,KAAK,UAAU,SAAS,aAAa;IACnC,SAAA,8BAAA,uBAAA,SAAS,IAAA,CAAY;GACvB,CAAC;EACH,CAAC;CAEL;AACF"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as Subscribable } from "./subscribable-D2lAzdDi.cjs";
|
|
2
|
-
import {
|
|
2
|
+
import { $t as QueryObserverOptions, D as Query, _ as QueryClient, j as QueryObserver, rn as QueryObserverResult } from "./hydration-DRPiSXsi.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 { $t as QueryObserverOptions, D as Query, _ as QueryClient, j as QueryObserver, rn as QueryObserverResult } from "./hydration-Bwo9mbMU.js";
|
|
3
3
|
type QueriesObserverListener = (result: Array<QueryObserverResult>) => void;
|
|
4
4
|
type CombineFn<TCombinedResult> = (result: Array<QueryObserverResult>) => TCombinedResult;
|
|
5
5
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queriesObserver.js","names":[],"sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\nimport { replaceEqualDeep, shallowEqualObjects } from './utils'\nimport type {\n DefaultedQueryObserverOptions,\n QueryObserverOptions,\n QueryObserverResult,\n} from './types'\nimport type { QueryClient } from './queryClient'\n\n/**\n * Returns the items of `array1` that are not in `array2`.\n * @param array1 - The items to filter.\n * @param array2 - The items to exclude.\n * @returns A new array with the items of `array1` that are not in `array2`.\n */\nfunction difference<T>(array1: Array<T>, array2: Array<T>): Array<T> {\n const excludeSet = new Set(array2)\n return array1.filter((x) => !excludeSet.has(x))\n}\n\ntype QueriesObserverListener = (result: Array<QueryObserverResult>) => void\n\ntype CombineFn<TCombinedResult> = (\n result: Array<QueryObserverResult>,\n) => TCombinedResult\n\n/**\n * Options for a `QueriesObserver` that apply to all of its queries at once.\n */\nexport interface QueriesObserverOptions<\n TCombinedResult = Array<QueryObserverResult>,\n> {\n /**\n * A function that combines the array of `QueryObserverResult`s (one per\n * observed query) into a single value. The combined value is memoized and\n * only recomputed when one of the underlying results, the query hashes, or\n * the `combine` function itself changes.\n *\n * Defaults to returning the array of `QueryObserverResult`s unchanged.\n */\n combine?: CombineFn<TCombinedResult>\n}\n\n/**\n * A `QueriesObserver` watches an array of queries at once, exposing them as\n * a single array of `QueryObserverResult`s (or, when a `combine` option is\n * given, as a combined value derived from that array). It manages one\n * internal `QueryObserver` per query, and is the primitive that framework\n * adapters (e.g. `useQueries`) build their hooks on top of.\n * @example\n * ```ts\n * const observer = new QueriesObserver(queryClient, [\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 2], queryFn: fetchPost },\n * ])\n *\n * const unsubscribe = observer.subscribe((result) => {\n * console.log(result)\n * })\n * ```\n */\nexport class QueriesObserver<\n TCombinedResult = Array<QueryObserverResult>,\n> extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result!: Array<QueryObserverResult>\n #queries: Array<QueryObserverOptions>\n #options?: QueriesObserverOptions<TCombinedResult>\n #observers: Array<QueryObserver>\n #combinedResult!: TCombinedResult\n #lastCombine?: CombineFn<TCombinedResult>\n #lastResult?: Array<QueryObserverResult>\n #lastQueryHashes?: Array<string>\n #observerMatches: Array<QueryObserverMatch> = []\n\n constructor(\n client: QueryClient,\n queries: Array<QueryObserverOptions<any, any, any, any, any>>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ) {\n super()\n\n this.#client = client\n this.#options = options\n this.#queries = []\n this.#observers = []\n this.#result = []\n\n this.setQueries(queries)\n }\n\n protected override onSubscribe(): void {\n if (this.listeners.size === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected override onUnsubscribe(): void {\n if (!this.listeners.size) {\n this.destroy()\n }\n }\n\n /**\n * Stops observing all queries: clears all listeners and destroys every\n * underlying `QueryObserver` this observer manages.\n */\n destroy(): void {\n this.listeners = new Set()\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n /**\n * Replaces the set of queries being observed. Existing `QueryObserver`s\n * are reused for queries that match an already-observed query hash;\n * observers for queries that are no longer present are destroyed, and new\n * observers are created and subscribed to for newly added queries.\n * @param queries - The options of the queries to observe.\n * @param options - Replaces the observer's options, e.g. its `combine` function.\n * @example\n * ```ts\n * observer.setQueries([\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 3], queryFn: fetchPost },\n * ])\n * ```\n */\n setQueries(\n queries: Array<QueryObserverOptions>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ): void {\n this.#queries = queries\n this.#options = options\n\n if (process.env.NODE_ENV !== 'production') {\n const queryHashes = queries.map(\n (query) => this.#client.defaultQueryOptions(query).queryHash,\n )\n if (new Set(queryHashes).size !== queryHashes.length) {\n console.warn(\n '[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.',\n )\n }\n }\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasLengthChange = prevObservers.length !== newObservers.length\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n const hasStructuralChange = hasLengthChange || hasIndexChange\n\n const hasResultChange = hasStructuralChange\n ? true\n : newResult.some((result, index) => {\n const prev = this.#result[index]\n return !prev || !shallowEqualObjects(result, prev)\n })\n\n if (!hasStructuralChange && !hasResultChange) return\n\n if (hasStructuralChange) {\n this.#observerMatches = newObserverMatches\n this.#observers = newObservers\n }\n\n this.#result = newResult\n\n if (!this.hasListeners()) return\n\n if (hasStructuralChange) {\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n\n this.#notify()\n })\n }\n\n /**\n * Returns the most recently computed array of `QueryObserverResult`s, one\n * per observed query, in the same order as the queries passed to the\n * constructor or `setQueries`.\n * @returns The current results.\n * @example\n * ```ts\n * const results = observer.getCurrentResult()\n * const data = results.map((result) => result.data)\n * ```\n */\n getCurrentResult(): Array<QueryObserverResult> {\n return this.#result\n }\n\n /**\n * Returns the underlying `Query` instances currently being observed, in\n * the same order as the queries passed to the constructor or `setQueries`.\n * @returns The observed queries.\n */\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n /**\n * Returns the underlying `QueryObserver` instances this observer manages,\n * in the same order as the queries passed to the constructor or\n * `setQueries`.\n * @returns The managed query observers.\n */\n getObservers() {\n return this.#observers\n }\n\n /**\n * The `QueriesObserver` counterpart of {@link QueryObserver#getOptimisticResult} — computes\n * the result for the given (already-defaulted) queries right now, synchronously. Called by\n * framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw\n * per-query results, a function to compute the combined result from them, and a function to\n * wrap the results for property-access tracking.\n * @param queries - The defaulted options of the queries to compute the result for.\n * @param combine - The `combine` function used by the returned `combineResult`, if any.\n * @returns A tuple of the per-query results, a function that computes the combined result, and a\n * function that returns the results wrapped for property-access tracking.\n */\n getOptimisticResult(\n queries: Array<QueryObserverOptions>,\n combine: CombineFn<TCombinedResult> | undefined,\n ): [\n rawResult: Array<QueryObserverResult>,\n combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult,\n trackResult: () => Array<QueryObserverResult>,\n ] {\n const matches = this.#findMatchingObservers(queries)\n const result = matches.map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n const queryHashes = matches.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n\n return [\n result,\n (r?: Array<QueryObserverResult>) => {\n return this.#combineResult(r ?? result, combine, queryHashes)\n },\n () => {\n return this.#trackResult(result, matches)\n },\n ]\n }\n\n #trackResult(\n result: Array<QueryObserverResult>,\n matches: Array<QueryObserverMatch>,\n ) {\n const trackedProps = new Set<keyof QueryObserverResult>()\n\n return matches.map((match, index) => {\n const observerResult = result[index]!\n return !match.defaultedQueryOptions.notifyOnChangeProps\n ? match.observer.trackResult(observerResult, (accessedProp) => {\n // track property on all observers to ensure proper (synchronized) tracking (#7000)\n if (!trackedProps.has(accessedProp)) {\n trackedProps.add(accessedProp)\n matches.forEach((m) => {\n m.observer.trackProp(accessedProp)\n })\n }\n })\n : observerResult\n })\n }\n\n #combineResult(\n input: Array<QueryObserverResult>,\n combine: CombineFn<TCombinedResult> | undefined,\n queryHashes?: Array<string>,\n ): TCombinedResult {\n if (combine) {\n const lastHashes = this.#lastQueryHashes\n const queryHashesChanged =\n queryHashes !== undefined &&\n lastHashes !== undefined &&\n (lastHashes.length !== queryHashes.length ||\n queryHashes.some((hash, i) => hash !== lastHashes[i]))\n\n if (\n this.#result !== this.#lastResult ||\n queryHashesChanged ||\n combine !== this.#lastCombine\n ) {\n this.#lastCombine = combine\n this.#lastResult = this.#result\n\n if (queryHashes !== undefined) {\n this.#lastQueryHashes = queryHashes\n }\n this.#combinedResult = replaceEqualDeep(\n this.#combinedResult,\n combine(input),\n )\n }\n\n return this.#combinedResult\n }\n return input as any\n }\n\n #shouldSkipCombine(): boolean {\n return (\n !this.#options?.combine ||\n this.#observers.some((observer, index) => {\n return (\n observer.options.suspense && this.#result[index]?.data === undefined\n )\n })\n )\n }\n\n #findMatchingObservers(\n queries: Array<QueryObserverOptions>,\n ): Array<QueryObserverMatch> {\n const prevObserversMap = new Map<string, Array<QueryObserver>>()\n\n this.#observers.forEach((observer) => {\n const key = observer.options.queryHash\n if (!key) return\n\n const previousObservers = prevObserversMap.get(key)\n\n if (previousObservers) {\n previousObservers.push(observer)\n } else {\n prevObserversMap.set(key, [observer])\n }\n })\n\n const observers: Array<QueryObserverMatch> = []\n\n queries.forEach((options) => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const match = prevObserversMap.get(defaultedOptions.queryHash)?.shift()\n const observer =\n match ?? new QueryObserver(this.#client, defaultedOptions)\n\n observers.push({\n defaultedQueryOptions: defaultedOptions,\n observer,\n })\n })\n\n return observers\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = this.#result.slice()\n this.#result[index] = result\n this.#notify()\n }\n }\n\n #notify(): void {\n if (this.hasListeners()) {\n const shouldSkipCombine = this.#shouldSkipCombine()\n const previousResult = this.#combinedResult\n const newResult = shouldSkipCombine\n ? previousResult\n : this.#combineResult(\n this.#trackResult(this.#result, this.#observerMatches),\n this.#options?.combine,\n )\n\n if (shouldSkipCombine || previousResult !== newResult) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n }\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"mappings":";;;;;;;;;;;;;AAiBA,SAAS,WAAc,QAAkB,QAA4B;CACnE,MAAM,aAAa,IAAI,IAAI,MAAM;CACjC,OAAO,OAAO,QAAQ,MAAM,CAAC,WAAW,IAAI,CAAC,CAAC;A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vB;EACF,CAAC;CACH,CAAC;CAED,OAAO;AACT;AAEA,SAAA,UAAU,UAAyB,QAAmC;CACpE,MAAM,QAAA,uBAAA,YAAQ,IAAA,CAAA,CAAgB,QAAQ,QAAQ;CAC9C,IAAI,UAAU,IAAI;EAChB,uBAAA,SAAA,MAAA,uBAAA,SAAe,IAAA,CAAA,CAAa,MAAM,CAAA;EAClC,uBAAA,SAAA,IAAA,CAAA,CAAa,SAAS;EACtB,kBAAA,wBAAA,MAAA,OAAA,CAAA,CAAA,KAAA,IAAa;CACf;AACF;AAEA,SAAA,UAAgB;CACd,IAAI,KAAK,aAAa,GAAG;;EACvB,MAAM,oBAAA,kBAAA,wBAAoB,MAAA,kBAAA,CAAA,CAAA,KAAA,IAAwB;EAClD,MAAM,iBAAA,uBAAA,iBAAiB,IAAA;EACvB,MAAM,YAAY,oBACd,iBAAA,kBAAA,wBACA,MAAA,cAAA,CAAA,CAAA,KAAA,MAAA,kBAAA,wBACE,MAAA,YAAA,CAAA,CAAA,KAAA,MAAA,uBAAA,SAAkB,IAAA,GAAA,uBAAA,kBAAc,IAAA,CAAqB,IAAA,yBAAA,uBAAA,UACrD,IAAA,OAAA,QAAA,2BAAA,KAAA,IAAA,KAAA,IAAA,uBAAe,OACjB;EAEJ,IAAI,qBAAqB,mBAAmB,WAC1C,cAAc,YAAY;GACxB,KAAK,UAAU,SAAS,aAAa;IACnC,SAAA,uBAAA,SAAS,IAAA,CAAY;GACvB,CAAC;EACH,CAAC;CAEL;AACF"}
|
|
1
|
+
{"version":3,"file":"queriesObserver.js","names":[],"sources":["../../src/queriesObserver.ts"],"sourcesContent":["import { notifyManager } from './notifyManager'\nimport { QueryObserver } from './queryObserver'\nimport { Subscribable } from './subscribable'\nimport { replaceEqualDeep, shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultedQueryObserverOptions,\n QueryObserverOptions,\n QueryObserverResult,\n} from './types'\n\n/**\n * Returns the items of `array1` that are not in `array2`.\n * @param array1 - The items to filter.\n * @param array2 - The items to exclude.\n * @returns A new array with the items of `array1` that are not in `array2`.\n */\nfunction difference<T>(array1: Array<T>, array2: Array<T>): Array<T> {\n const excludeSet = new Set(array2)\n return array1.filter((x) => !excludeSet.has(x))\n}\n\ntype QueriesObserverListener = (result: Array<QueryObserverResult>) => void\n\ntype CombineFn<TCombinedResult> = (\n result: Array<QueryObserverResult>,\n) => TCombinedResult\n\n/**\n * Options for a `QueriesObserver` that apply to all of its queries at once.\n */\nexport interface QueriesObserverOptions<\n TCombinedResult = Array<QueryObserverResult>,\n> {\n /**\n * A function that combines the array of `QueryObserverResult`s (one per\n * observed query) into a single value. The combined value is memoized and\n * only recomputed when one of the underlying results, the query hashes, or\n * the `combine` function itself changes.\n *\n * Defaults to returning the array of `QueryObserverResult`s unchanged.\n */\n combine?: CombineFn<TCombinedResult>\n}\n\n/**\n * A `QueriesObserver` watches an array of queries at once, exposing them as\n * a single array of `QueryObserverResult`s (or, when a `combine` option is\n * given, as a combined value derived from that array). It manages one\n * internal `QueryObserver` per query, and is the primitive that framework\n * adapters (e.g. `useQueries`) build their hooks on top of.\n * @example\n * ```ts\n * const observer = new QueriesObserver(queryClient, [\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 2], queryFn: fetchPost },\n * ])\n *\n * const unsubscribe = observer.subscribe((result) => {\n * console.log(result)\n * })\n * ```\n */\nexport class QueriesObserver<\n TCombinedResult = Array<QueryObserverResult>,\n> extends Subscribable<QueriesObserverListener> {\n #client: QueryClient\n #result!: Array<QueryObserverResult>\n #queries: Array<QueryObserverOptions>\n #options?: QueriesObserverOptions<TCombinedResult>\n #observers: Array<QueryObserver>\n #combinedResult!: TCombinedResult\n #lastCombine?: CombineFn<TCombinedResult>\n #lastResult?: Array<QueryObserverResult>\n #lastQueryHashes?: Array<string>\n #observerMatches: Array<QueryObserverMatch> = []\n\n constructor(\n client: QueryClient,\n queries: Array<QueryObserverOptions<any, any, any, any, any>>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ) {\n super()\n\n this.#client = client\n this.#options = options\n this.#queries = []\n this.#observers = []\n this.#result = []\n\n this.setQueries(queries)\n }\n\n protected override onSubscribe(): void {\n if (this.listeners.size === 1) {\n this.#observers.forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n }\n\n protected override onUnsubscribe(): void {\n if (!this.listeners.size) {\n this.destroy()\n }\n }\n\n /**\n * Stops observing all queries: clears all listeners and destroys every\n * underlying `QueryObserver` this observer manages.\n */\n destroy(): void {\n this.listeners = new Set()\n this.#observers.forEach((observer) => {\n observer.destroy()\n })\n }\n\n /**\n * Replaces the set of queries being observed. Existing `QueryObserver`s\n * are reused for queries that match an already-observed query hash;\n * observers for queries that are no longer present are destroyed, and new\n * observers are created and subscribed to for newly added queries.\n * @param queries - The options of the queries to observe.\n * @param options - Replaces the observer's options, e.g. its `combine` function.\n * @example\n * ```ts\n * observer.setQueries([\n * { queryKey: ['post', 1], queryFn: fetchPost },\n * { queryKey: ['post', 3], queryFn: fetchPost },\n * ])\n * ```\n */\n setQueries(\n queries: Array<QueryObserverOptions>,\n options?: QueriesObserverOptions<TCombinedResult>,\n ): void {\n this.#queries = queries\n this.#options = options\n\n if (process.env.NODE_ENV !== 'production') {\n const queryHashes = queries.map(\n (query) => this.#client.defaultQueryOptions(query).queryHash,\n )\n if (new Set(queryHashes).size !== queryHashes.length) {\n console.warn(\n '[QueriesObserver]: Duplicate Queries found. This might result in unexpected behavior.',\n )\n }\n }\n\n notifyManager.batch(() => {\n const prevObservers = this.#observers\n\n const newObserverMatches = this.#findMatchingObservers(this.#queries)\n\n // set options for the new observers to notify of changes\n newObserverMatches.forEach((match) =>\n match.observer.setOptions(match.defaultedQueryOptions),\n )\n\n const newObservers = newObserverMatches.map((match) => match.observer)\n const newResult = newObservers.map((observer) =>\n observer.getCurrentResult(),\n )\n\n const hasLengthChange = prevObservers.length !== newObservers.length\n const hasIndexChange = newObservers.some(\n (observer, index) => observer !== prevObservers[index],\n )\n const hasStructuralChange = hasLengthChange || hasIndexChange\n\n const hasResultChange = hasStructuralChange\n ? true\n : newResult.some((result, index) => {\n const prev = this.#result[index]\n return !prev || !shallowEqualObjects(result, prev)\n })\n\n if (!hasStructuralChange && !hasResultChange) return\n\n if (hasStructuralChange) {\n this.#observerMatches = newObserverMatches\n this.#observers = newObservers\n }\n\n this.#result = newResult\n\n if (!this.hasListeners()) return\n\n if (hasStructuralChange) {\n difference(prevObservers, newObservers).forEach((observer) => {\n observer.destroy()\n })\n difference(newObservers, prevObservers).forEach((observer) => {\n observer.subscribe((result) => {\n this.#onUpdate(observer, result)\n })\n })\n }\n\n this.#notify()\n })\n }\n\n /**\n * Returns the most recently computed array of `QueryObserverResult`s, one\n * per observed query, in the same order as the queries passed to the\n * constructor or `setQueries`.\n * @returns The current results.\n * @example\n * ```ts\n * const results = observer.getCurrentResult()\n * const data = results.map((result) => result.data)\n * ```\n */\n getCurrentResult(): Array<QueryObserverResult> {\n return this.#result\n }\n\n /**\n * Returns the underlying `Query` instances currently being observed, in\n * the same order as the queries passed to the constructor or `setQueries`.\n * @returns The observed queries.\n */\n getQueries() {\n return this.#observers.map((observer) => observer.getCurrentQuery())\n }\n\n /**\n * Returns the underlying `QueryObserver` instances this observer manages,\n * in the same order as the queries passed to the constructor or\n * `setQueries`.\n * @returns The managed query observers.\n */\n getObservers() {\n return this.#observers\n }\n\n /**\n * The `QueriesObserver` counterpart of {@link QueryObserver#getOptimisticResult} — computes\n * the result for the given (already-defaulted) queries right now, synchronously. Called by\n * framework adapters (e.g. `useQueries`) ahead of subscribing, returning a tuple of the raw\n * per-query results, a function to compute the combined result from them, and a function to\n * wrap the results for property-access tracking.\n * @param queries - The defaulted options of the queries to compute the result for.\n * @param combine - The `combine` function used by the returned `combineResult`, if any.\n * @returns A tuple of the per-query results, a function that computes the combined result, and a\n * function that returns the results wrapped for property-access tracking.\n */\n getOptimisticResult(\n queries: Array<QueryObserverOptions>,\n combine: CombineFn<TCombinedResult> | undefined,\n ): [\n rawResult: Array<QueryObserverResult>,\n combineResult: (r?: Array<QueryObserverResult>) => TCombinedResult,\n trackResult: () => Array<QueryObserverResult>,\n ] {\n const matches = this.#findMatchingObservers(queries)\n const result = matches.map((match) =>\n match.observer.getOptimisticResult(match.defaultedQueryOptions),\n )\n const queryHashes = matches.map(\n (match) => match.defaultedQueryOptions.queryHash,\n )\n\n return [\n result,\n (r?: Array<QueryObserverResult>) => {\n return this.#combineResult(r ?? result, combine, queryHashes)\n },\n () => {\n return this.#trackResult(result, matches)\n },\n ]\n }\n\n #trackResult(\n result: Array<QueryObserverResult>,\n matches: Array<QueryObserverMatch>,\n ) {\n const trackedProps = new Set<keyof QueryObserverResult>()\n\n return matches.map((match, index) => {\n const observerResult = result[index]!\n return !match.defaultedQueryOptions.notifyOnChangeProps\n ? match.observer.trackResult(observerResult, (accessedProp) => {\n // track property on all observers to ensure proper (synchronized) tracking (#7000)\n if (!trackedProps.has(accessedProp)) {\n trackedProps.add(accessedProp)\n matches.forEach((m) => {\n m.observer.trackProp(accessedProp)\n })\n }\n })\n : observerResult\n })\n }\n\n #combineResult(\n input: Array<QueryObserverResult>,\n combine: CombineFn<TCombinedResult> | undefined,\n queryHashes?: Array<string>,\n ): TCombinedResult {\n if (combine) {\n const lastHashes = this.#lastQueryHashes\n const queryHashesChanged =\n queryHashes !== undefined &&\n lastHashes !== undefined &&\n (lastHashes.length !== queryHashes.length ||\n queryHashes.some((hash, i) => hash !== lastHashes[i]))\n\n if (\n this.#result !== this.#lastResult ||\n queryHashesChanged ||\n combine !== this.#lastCombine\n ) {\n this.#lastCombine = combine\n this.#lastResult = this.#result\n\n if (queryHashes !== undefined) {\n this.#lastQueryHashes = queryHashes\n }\n this.#combinedResult = replaceEqualDeep(\n this.#combinedResult,\n combine(input),\n )\n }\n\n return this.#combinedResult\n }\n return input as any\n }\n\n #shouldSkipCombine(): boolean {\n return (\n !this.#options?.combine ||\n this.#observers.some((observer, index) => {\n return (\n observer.options.suspense && this.#result[index]?.data === undefined\n )\n })\n )\n }\n\n #findMatchingObservers(\n queries: Array<QueryObserverOptions>,\n ): Array<QueryObserverMatch> {\n const prevObserversMap = new Map<string, Array<QueryObserver>>()\n\n this.#observers.forEach((observer) => {\n const key = observer.options.queryHash\n if (!key) return\n\n const previousObservers = prevObserversMap.get(key)\n\n if (previousObservers) {\n previousObservers.push(observer)\n } else {\n prevObserversMap.set(key, [observer])\n }\n })\n\n const observers: Array<QueryObserverMatch> = []\n\n queries.forEach((options) => {\n const defaultedOptions = this.#client.defaultQueryOptions(options)\n const match = prevObserversMap.get(defaultedOptions.queryHash)?.shift()\n const observer =\n match ?? new QueryObserver(this.#client, defaultedOptions)\n\n observers.push({\n defaultedQueryOptions: defaultedOptions,\n observer,\n })\n })\n\n return observers\n }\n\n #onUpdate(observer: QueryObserver, result: QueryObserverResult): void {\n const index = this.#observers.indexOf(observer)\n if (index !== -1) {\n this.#result = this.#result.slice()\n this.#result[index] = result\n this.#notify()\n }\n }\n\n #notify(): void {\n if (this.hasListeners()) {\n const shouldSkipCombine = this.#shouldSkipCombine()\n const previousResult = this.#combinedResult\n const newResult = shouldSkipCombine\n ? previousResult\n : this.#combineResult(\n this.#trackResult(this.#result, this.#observerMatches),\n this.#options?.combine,\n )\n\n if (shouldSkipCombine || previousResult !== newResult) {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(this.#result)\n })\n })\n }\n }\n }\n}\n\ntype QueryObserverMatch = {\n defaultedQueryOptions: DefaultedQueryObserverOptions\n observer: QueryObserver\n}\n"],"mappings":";;;;;;;;;;;;;AAiBA,SAAS,WAAc,QAAkB,QAA4B;CACnE,MAAM,aAAa,IAAI,IAAI,MAAM;CACjC,OAAO,OAAO,QAAQ,MAAM,CAAC,WAAW,IAAI,CAAC,CAAC;AAChD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,kBAAb,cAEU,aAAsC;CAY9C,YACE,QACA,SAC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BAAA,YAAQ,IAAA,CAAA,CAAgB,QAAQ,QAAQ;CAC9C,IAAI,UAAU,IAAI;EAChB,uBAAA,SAAA,MAAA,uBAAA,SAAe,IAAA,CAAA,CAAa,MAAM,CAAA;EAClC,uBAAA,SAAA,IAAA,CAAA,CAAa,SAAS;EACtB,kBAAA,wBAAA,MAAA,OAAA,CAAA,CAAA,KAAA,IAAa;CACf;AACF;AAEA,SAAA,UAAgB;CACd,IAAI,KAAK,aAAa,GAAG;;EACvB,MAAM,oBAAA,kBAAA,wBAAoB,MAAA,kBAAA,CAAA,CAAA,KAAA,IAAwB;EAClD,MAAM,iBAAA,uBAAA,iBAAiB,IAAA;EACvB,MAAM,YAAY,oBACd,iBAAA,kBAAA,wBACA,MAAA,cAAA,CAAA,CAAA,KAAA,MAAA,kBAAA,wBACE,MAAA,YAAA,CAAA,CAAA,KAAA,MAAA,uBAAA,SAAkB,IAAA,GAAA,uBAAA,kBAAc,IAAA,CAAqB,IAAA,yBAAA,uBAAA,UACrD,IAAA,OAAA,QAAA,2BAAA,KAAA,IAAA,KAAA,IAAA,uBAAe,OACjB;EAEJ,IAAI,qBAAqB,mBAAmB,WAC1C,cAAc,YAAY;GACxB,KAAK,UAAU,SAAS,aAAa;IACnC,SAAA,uBAAA,SAAS,IAAA,CAAY;GACvB,CAAC;EACH,CAAC;CAEL;AACF"}
|
package/build/legacy/query.cjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_classPrivateFieldSet2 = require("./classPrivateFieldSet2-C_J9s9he.cjs");
|
|
3
3
|
const require_utils = require("./utils.cjs");
|
|
4
|
+
const require_infiniteQueryBehavior = require("./infiniteQueryBehavior.cjs");
|
|
4
5
|
const require_notifyManager = require("./notifyManager.cjs");
|
|
5
|
-
const require_retryer = require("./retryer.cjs");
|
|
6
6
|
const require_removable = require("./removable.cjs");
|
|
7
|
-
const
|
|
7
|
+
const require_retryer = require("./retryer.cjs");
|
|
8
8
|
const require_classPrivateMethodInitSpec = require("./classPrivateMethodInitSpec-BMGv9vbZ.cjs");
|
|
9
9
|
// src/query.ts
|
|
10
10
|
var _queryType = /* @__PURE__ */ new WeakMap();
|
|
@@ -534,7 +534,7 @@ function _dispatch(action) {
|
|
|
534
534
|
...fetchState(state.data, this.options),
|
|
535
535
|
fetchMeta: action.meta ?? null
|
|
536
536
|
};
|
|
537
|
-
case "success":
|
|
537
|
+
case "success": {
|
|
538
538
|
const newState = {
|
|
539
539
|
...state,
|
|
540
540
|
...successState(action.data, action.dataUpdatedAt),
|
|
@@ -547,7 +547,8 @@ function _dispatch(action) {
|
|
|
547
547
|
};
|
|
548
548
|
require_classPrivateFieldSet2._classPrivateFieldSet2(_revertState, this, action.manual ? newState : void 0);
|
|
549
549
|
return newState;
|
|
550
|
-
|
|
550
|
+
}
|
|
551
|
+
case "error": {
|
|
551
552
|
const error = action.error;
|
|
552
553
|
return {
|
|
553
554
|
...state,
|
|
@@ -560,6 +561,7 @@ function _dispatch(action) {
|
|
|
560
561
|
status: "error",
|
|
561
562
|
isInvalidated: true
|
|
562
563
|
};
|
|
564
|
+
}
|
|
563
565
|
case "invalidate": return {
|
|
564
566
|
...state,
|
|
565
567
|
isInvalidated: true
|