@tanstack/query-core 5.12.0 → 5.13.4
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/mutationObserver.cjs +2 -0
- package/build/legacy/mutationObserver.cjs.map +1 -1
- package/build/legacy/mutationObserver.js +2 -0
- package/build/legacy/mutationObserver.js.map +1 -1
- package/build/modern/mutationObserver.cjs +1 -0
- package/build/modern/mutationObserver.cjs.map +1 -1
- package/build/modern/mutationObserver.js +1 -0
- package/build/modern/mutationObserver.js.map +1 -1
- package/package.json +1 -1
- package/src/mutationObserver.ts +3 -0
- package/src/tests/mutationObserver.test.tsx +50 -0
|
@@ -95,6 +95,8 @@ var MutationObserver = class extends import_subscribable.Subscribable {
|
|
|
95
95
|
return __privateGet(this, _currentResult);
|
|
96
96
|
}
|
|
97
97
|
reset() {
|
|
98
|
+
var _a;
|
|
99
|
+
(_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.removeObserver(this);
|
|
98
100
|
__privateSet(this, _currentMutation, void 0);
|
|
99
101
|
__privateMethod(this, _updateResult, updateResult_fn).call(this);
|
|
100
102
|
__privateMethod(this, _notify, notify_fn).call(this);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n this.#currentMutation?.setOptions(this.options)\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\n }\n\n reset(): void {\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\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, TContext>()\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, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAgC;AAChC,2BAA8B;AAC9B,0BAA6B;AAC7B,mBAAoC;AAHpC;AAqBO,IAAM,mBAAN,cAKG,iCAER;AAAA,EASA,YACE,QACA,SACA;AACA,UAAM;
|
|
1
|
+
{"version":3,"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n this.#currentMutation?.setOptions(this.options)\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\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 mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\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, TContext>()\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, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAgC;AAChC,2BAA8B;AAC9B,0BAA6B;AAC7B,mBAAoC;AAHpC;AAqBO,IAAM,mBAAN,cAKG,iCAER;AAAA,EASA,YACE,QACA,SACA;AACA,UAAM;AA2ER;AAgBA;AArGA;AACA,uCACE;AACF;AACA;AAQE,uBAAK,SAAU;AACf,SAAK,WAAW,OAAO;AACvB,SAAK,YAAY;AACjB,0BAAK,gCAAL;AAAA,EACF;AAAA,EAEU,cAAoB;AAC5B,SAAK,SAAS,KAAK,OAAO,KAAK,IAAI;AACnC,SAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;AAAA,EACnC;AAAA,EAEA,WACE,SACA;AAxDJ;AAyDI,UAAM,cAAc,KAAK;AACzB,SAAK,UAAU,mBAAK,SAAQ,uBAAuB,OAAO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ACd;AACF;AAEA;AAAA,YAAO,SAAC,QAA4D;AAClE,qCAAc,MAAM,MAAM;AArI9B;AAuIM,QAAI,mBAAK,mBAAkB,KAAK,aAAa,GAAG;AAC9C,WAAI,iCAAQ,UAAS,WAAW;AAC9B,uCAAK,iBAAe,cAApB;AAAA;AAAA,UACE,OAAO;AAAA,UACP,mBAAK,gBAAe;AAAA,UACpB,mBAAK,gBAAe;AAAA;AAEtB,uCAAK,iBAAe,cAApB;AAAA;AAAA,UACE,OAAO;AAAA,UACP;AAAA,UACA,mBAAK,gBAAe;AAAA,UACpB,mBAAK,gBAAe;AAAA;AAAA,MAExB,YAAW,iCAAQ,UAAS,SAAS;AACnC,uCAAK,iBAAe,YAApB;AAAA;AAAA,UACE,OAAO;AAAA,UACP,mBAAK,gBAAe;AAAA,UACpB,mBAAK,gBAAe;AAAA;AAEtB,uCAAK,iBAAe,cAApB;AAAA;AAAA,UACE;AAAA,UACA,OAAO;AAAA,UACP,mBAAK,gBAAe;AAAA,UACpB,mBAAK,gBAAe;AAAA;AAAA,MAExB;AAAA,IACF;AAGA,SAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,eAAS,mBAAK,eAAc;AAAA,IAC9B,CAAC;AAAA,EACH,CAAC;AACH;","names":[]}
|
|
@@ -56,6 +56,8 @@ var MutationObserver = class extends Subscribable {
|
|
|
56
56
|
return __privateGet(this, _currentResult);
|
|
57
57
|
}
|
|
58
58
|
reset() {
|
|
59
|
+
var _a;
|
|
60
|
+
(_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.removeObserver(this);
|
|
59
61
|
__privateSet(this, _currentMutation, void 0);
|
|
60
62
|
__privateMethod(this, _updateResult, updateResult_fn).call(this);
|
|
61
63
|
__privateMethod(this, _notify, notify_fn).call(this);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n this.#currentMutation?.setOptions(this.options)\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\n }\n\n reset(): void {\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\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, TContext>()\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, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;AAAA,SAAS,uBAAuB;AAChC,SAAS,qBAAqB;AAC9B,SAAS,oBAAoB;AAC7B,SAAS,2BAA2B;AAHpC;AAqBO,IAAM,mBAAN,cAKG,aAER;AAAA,EASA,YACE,QACA,SACA;AACA,UAAM;
|
|
1
|
+
{"version":3,"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n this.#currentMutation?.setOptions(this.options)\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\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 mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\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, TContext>()\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, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;AAAA,SAAS,uBAAuB;AAChC,SAAS,qBAAqB;AAC9B,SAAS,oBAAoB;AAC7B,SAAS,2BAA2B;AAHpC;AAqBO,IAAM,mBAAN,cAKG,aAER;AAAA,EASA,YACE,QACA,SACA;AACA,UAAM;AA2ER;AAgBA;AArGA;AACA,uCACE;AACF;AACA;AAQE,uBAAK,SAAU;AACf,SAAK,WAAW,OAAO;AACvB,SA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,wBAAK,sBAAL,mBAAuB,UACvB,gBAAqD;AAEvD,qBAAK,gBAAiB;AAAA,IACpB,GAAG;AAAA,IACH,WAAW,MAAM,WAAW;AAAA,IAC5B,WAAW,MAAM,WAAW;AAAA,IAC5B,SAAS,MAAM,WAAW;AAAA,IAC1B,QAAQ,MAAM,WAAW;AAAA,IACzB,QAAQ,KAAK;AAAA,IACb,OAAO,KAAK;AAAA,EACd;AACF;AAEA;AAAA,YAAO,SAAC,QAA4D;AAClE,gBAAc,MAAM,MAAM;AArI9B;AAuIM,QAAI,mBAAK,mBAAkB,KAAK,aAAa,GAAG;AAC9C,WAAI,iCAAQ,UAAS,WAAW;AAC9B,uCAAK,iBAAe,cAApB;AAAA;AAAA,UACE,OAAO;AAAA,UACP,mBAAK,gBAAe;AAAA,UACpB,mBAAK,gBAAe;AAAA;AAEtB,uCAAK,iBAAe,cAApB;AAAA;AAAA,UACE,OAAO;AAAA,UACP;AAAA,UACA,mBAAK,gBAAe;AAAA,UACpB,mBAAK,gBAAe;AAAA;AAAA,MAExB,YAAW,iCAAQ,UAAS,SAAS;AACnC,uCAAK,iBAAe,YAApB;AAAA;AAAA,UACE,OAAO;AAAA,UACP,mBAAK,gBAAe;AAAA,UACpB,mBAAK,gBAAe;AAAA;AAEtB,uCAAK,iBAAe,cAApB;AAAA;AAAA,UACE;AAAA,UACA,OAAO;AAAA,UACP,mBAAK,gBAAe;AAAA,UACpB,mBAAK,gBAAe;AAAA;AAAA,MAExB;AAAA,IACF;AAGA,SAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,eAAS,mBAAK,eAAc;AAAA,IAC9B,CAAC;AAAA,EACH,CAAC;AACH;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n this.#currentMutation?.setOptions(this.options)\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\n }\n\n reset(): void {\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\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, TContext>()\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, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAgC;AAChC,2BAA8B;AAC9B,0BAA6B;AAC7B,mBAAoC;AAkB7B,IAAM,mBAAN,cAKG,iCAER;AAAA,EASA,YACE,QACA,SACA;AACA,UAAM;AATR,0BACE;AAUA,SAAK,UAAU;AACf,SAAK,WAAW,OAAO;AACvB,SAAK,YAAY;AACjB,SAAK,cAAc;AAAA,EACrB;AAAA,EAhBA;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,EAcU,cAAoB;AAC5B,SAAK,SAAS,KAAK,OAAO,KAAK,IAAI;AACnC,SAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;AAAA,EACnC;AAAA,EAEA,WACE,SACA;AACA,UAAM,cAAc,KAAK;AACzB,SAAK,UAAU,KAAK,QAAQ,uBAAuB,OAAO;AAC1D,QAAI,KAAC,kCAAoB,aAAa,KAAK,OAAO,GAAG;AACnD,WAAK,QAAQ,iBAAiB,EAAE,OAAO;AAAA,QACrC,MAAM;AAAA,QACN,UAAU,KAAK;AAAA,QACf,UAAU;AAAA,MACZ,CAAC;AAAA,IACH;AACA,SAAK,kBAAkB,WAAW,KAAK,OAAO;AAAA,EAChD;AAAA,EAEU,gBAAsB;AAC9B,QAAI,CAAC,KAAK,aAAa,GAAG;AACxB,WAAK,kBAAkB,eAAe,IAAI;AAAA,IAC5C;AAAA,EACF;AAAA,EAEA,iBAAiB,QAA2D;AAC1E,SAAK,cAAc;AAEnB,SAAK,QAAQ,MAAM;AAAA,EACrB;AAAA,EAEA,mBAKE;AACA,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,QAAc;
|
|
1
|
+
{"version":3,"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n this.#currentMutation?.setOptions(this.options)\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\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 mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\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, TContext>()\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, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAgC;AAChC,2BAA8B;AAC9B,0BAA6B;AAC7B,mBAAoC;AAkB7B,IAAM,mBAAN,cAKG,iCAER;AAAA,EASA,YACE,QACA,SACA;AACA,UAAM;AATR,0BACE;AAUA,SAAK,UAAU;AACf,SAAK,WAAW,OAAO;AACvB,SAAK,YAAY;AACjB,SAAK,cAAc;AAAA,EACrB;AAAA,EAhBA;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,EAcU,cAAoB;AAC5B,SAAK,SAAS,KAAK,OAAO,KAAK,IAAI;AACnC,SAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;AAAA,EACnC;AAAA,EAEA,WACE,SACA;AACA,UAAM,cAAc,KAAK;AACzB,SAAK,UAAU,KAAK,QAAQ,uBAAuB,OAAO;AAC1D,QAAI,KAAC,kCAAoB,aAAa,KAAK,OAAO,GAAG;AACnD,WAAK,QAAQ,iBAAiB,EAAE,OAAO;AAAA,QACrC,MAAM;AAAA,QACN,UAAU,KAAK;AAAA,QACf,U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AK,eAAe;AAAA,YAClB,OAAO;AAAA,YACP;AAAA,YACA,KAAK,eAAe;AAAA,YACpB,KAAK,eAAe;AAAA,UACtB;AAAA,QACF,WAAW,QAAQ,SAAS,SAAS;AACnC,eAAK,eAAe;AAAA,YAClB,OAAO;AAAA,YACP,KAAK,eAAe;AAAA,YACpB,KAAK,eAAe;AAAA,UACtB;AACA,eAAK,eAAe;AAAA,YAClB;AAAA,YACA,OAAO;AAAA,YACP,KAAK,eAAe;AAAA,YACpB,KAAK,eAAe;AAAA,UACtB;AAAA,QACF;AAAA,MACF;AAGA,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,iBAAS,KAAK,cAAc;AAAA,MAC9B,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n this.#currentMutation?.setOptions(this.options)\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\n }\n\n reset(): void {\n this.#currentMutation = undefined\n this.#updateResult()\n this.#notify()\n }\n\n mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\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, TContext>()\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, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";AAAA,SAAS,uBAAuB;AAChC,SAAS,qBAAqB;AAC9B,SAAS,oBAAoB;AAC7B,SAAS,2BAA2B;AAkB7B,IAAM,mBAAN,cAKG,aAER;AAAA,EASA,YACE,QACA,SACA;AACA,UAAM;AATR,0BACE;AAUA,SAAK,UAAU;AACf,SAAK,WAAW,OAAO;AACvB,SAAK,YAAY;AACjB,SAAK,cAAc;AAAA,EACrB;AAAA,EAhBA;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,EAcU,cAAoB;AAC5B,SAAK,SAAS,KAAK,OAAO,KAAK,IAAI;AACnC,SAAK,QAAQ,KAAK,MAAM,KAAK,IAAI;AAAA,EACnC;AAAA,EAEA,WACE,SACA;AACA,UAAM,cAAc,KAAK;AACzB,SAAK,UAAU,KAAK,QAAQ,uBAAuB,OAAO;AAC1D,QAAI,CAAC,oBAAoB,aAAa,KAAK,OAAO,GAAG;AACnD,WAAK,QAAQ,iBAAiB,EAAE,OAAO;AAAA,QACrC,MAAM;AAAA,QACN,UAAU,KAAK;AAAA,QACf,UAAU;AAAA,MACZ,CAAC;AAAA,IACH;AACA,SAAK,kBAAkB,WAAW,KAAK,OAAO;AAAA,EAChD;AAAA,EAEU,gBAAsB;AAC9B,QAAI,CAAC,KAAK,aAAa,GAAG;AACxB,WAAK,kBAAkB,eAAe,IAAI;AAAA,IAC5C;AAAA,EACF;AAAA,EAEA,iBAAiB,QAA2D;AAC1E,SAAK,cAAc;AAEnB,SAAK,QAAQ,MAAM;AAAA,EACrB;AAAA,EAEA,mBAKE;AACA,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,QAAc;
|
|
1
|
+
{"version":3,"sources":["../../src/mutationObserver.ts"],"sourcesContent":["import { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport { shallowEqualObjects } from './utils'\nimport type { QueryClient } from './queryClient'\nimport type {\n DefaultError,\n MutateOptions,\n MutationObserverOptions,\n MutationObserverResult,\n} from './types'\nimport type { Action, Mutation } from './mutation'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TContext = unknown,\n> extends Subscribable<\n MutationObserverListener<TData, TError, TVariables, TContext>\n> {\n options!: MutationObserverOptions<TData, TError, TVariables, TContext>\n\n #client: QueryClient\n #currentResult: MutationObserverResult<TData, TError, TVariables, TContext> =\n undefined!\n #currentMutation?: Mutation<TData, TError, TVariables, TContext>\n #mutateOptions?: MutateOptions<TData, TError, TVariables, TContext>\n\n constructor(\n client: QueryClient,\n options: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n super()\n\n this.#client = client\n this.setOptions(options)\n this.bindMethods()\n this.#updateResult()\n }\n\n protected bindMethods(): void {\n this.mutate = this.mutate.bind(this)\n this.reset = this.reset.bind(this)\n }\n\n setOptions(\n options?: MutationObserverOptions<TData, TError, TVariables, TContext>,\n ) {\n const prevOptions = this.options\n this.options = this.#client.defaultMutationOptions(options)\n if (!shallowEqualObjects(prevOptions, this.options)) {\n this.#client.getMutationCache().notify({\n type: 'observerOptionsUpdated',\n mutation: this.#currentMutation,\n observer: this,\n })\n }\n this.#currentMutation?.setOptions(this.options)\n }\n\n protected onUnsubscribe(): void {\n if (!this.hasListeners()) {\n this.#currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.#updateResult()\n\n this.#notify(action)\n }\n\n getCurrentResult(): MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n > {\n return this.#currentResult\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 mutate(\n variables: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\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, TContext>()\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, TContext>\n }\n\n #notify(action?: Action<TData, TError, TVariables, TContext>): void {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.#mutateOptions && this.hasListeners()) {\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(\n action.data,\n this.#currentResult.variables!,\n this.#currentResult.context!,\n )\n this.#mutateOptions.onSettled?.(\n action.data,\n null,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n this.#currentResult.variables!,\n this.#currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n this.listeners.forEach((listener) => {\n listener(this.#currentResult)\n })\n })\n }\n}\n"],"mappings":";AAAA,SAAS,uBAAuB;AAChC,SAAS,qBAAqB;A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AK,iBAAiB,YAAY,IAAI;AAEtC,WAAO,KAAK,iBAAiB,QAAQ,SAAS;AAAA,EAChD;AAAA,EAEA,gBAAsB;AACpB,UAAM,QACJ,KAAK,kBAAkB,SACvB,gBAAqD;AAEvD,SAAK,iBAAiB;AAAA,MACpB,GAAG;AAAA,MACH,WAAW,MAAM,WAAW;AAAA,MAC5B,WAAW,MAAM,WAAW;AAAA,MAC5B,SAAS,MAAM,WAAW;AAAA,MAC1B,QAAQ,MAAM,WAAW;AAAA,MACzB,QAAQ,KAAK;AAAA,MACb,OAAO,KAAK;AAAA,IACd;AAAA,EACF;AAAA,EAEA,QAAQ,QAA4D;AAClE,kBAAc,MAAM,MAAM;AAExB,UAAI,KAAK,kBAAkB,KAAK,aAAa,GAAG;AAC9C,YAAI,QAAQ,SAAS,WAAW;AAC9B,eAAK,eAAe;AAAA,YAClB,OAAO;AAAA,YACP,KAAK,eAAe;AAAA,YACpB,KAAK,eAAe;AAAA,UACtB;AACA,eAAK,eAAe;AAAA,YAClB,OAAO;AAAA,YACP;AAAA,YACA,KAAK,eAAe;AAAA,YACpB,KAAK,eAAe;AAAA,UACtB;AAAA,QACF,WAAW,QAAQ,SAAS,SAAS;AACnC,eAAK,eAAe;AAAA,YAClB,OAAO;AAAA,YACP,KAAK,eAAe;AAAA,YACpB,KAAK,eAAe;AAAA,UACtB;AACA,eAAK,eAAe;AAAA,YAClB;AAAA,YACA,OAAO;AAAA,YACP,KAAK,eAAe;AAAA,YACpB,KAAK,eAAe;AAAA,UACtB;AAAA,QACF;AAAA,MACF;AAGA,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,iBAAS,KAAK,cAAc;AAAA,MAC9B,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;","names":[]}
|
package/package.json
CHANGED
package/src/mutationObserver.ts
CHANGED
|
@@ -89,6 +89,9 @@ export class MutationObserver<
|
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
reset(): void {
|
|
92
|
+
// reset needs to remove the observer from the mutation because there is no way to "get it back"
|
|
93
|
+
// another mutate call will yield a new mutation!
|
|
94
|
+
this.#currentMutation?.removeObserver(this)
|
|
92
95
|
this.#currentMutation = undefined
|
|
93
96
|
this.#updateResult()
|
|
94
97
|
this.#notify()
|
|
@@ -44,4 +44,54 @@ describe('mutationObserver', () => {
|
|
|
44
44
|
// Clean-up
|
|
45
45
|
unsubscribe2()
|
|
46
46
|
})
|
|
47
|
+
|
|
48
|
+
test('unsubscribe should remove observer to trigger GC', async () => {
|
|
49
|
+
const mutation = new MutationObserver(queryClient, {
|
|
50
|
+
mutationFn: async (text: string) => {
|
|
51
|
+
await sleep(5)
|
|
52
|
+
return text
|
|
53
|
+
},
|
|
54
|
+
gcTime: 10,
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
const subscriptionHandler = vi.fn()
|
|
58
|
+
|
|
59
|
+
const unsubscribe = mutation.subscribe(subscriptionHandler)
|
|
60
|
+
|
|
61
|
+
await mutation.mutate('input')
|
|
62
|
+
|
|
63
|
+
expect(queryClient.getMutationCache().findAll()).toHaveLength(1)
|
|
64
|
+
|
|
65
|
+
unsubscribe()
|
|
66
|
+
|
|
67
|
+
await waitFor(() =>
|
|
68
|
+
expect(queryClient.getMutationCache().findAll()).toHaveLength(0),
|
|
69
|
+
)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('reset should remove observer to trigger GC', async () => {
|
|
73
|
+
const mutation = new MutationObserver(queryClient, {
|
|
74
|
+
mutationFn: async (text: string) => {
|
|
75
|
+
await sleep(5)
|
|
76
|
+
return text
|
|
77
|
+
},
|
|
78
|
+
gcTime: 10,
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const subscriptionHandler = vi.fn()
|
|
82
|
+
|
|
83
|
+
const unsubscribe = mutation.subscribe(subscriptionHandler)
|
|
84
|
+
|
|
85
|
+
await mutation.mutate('input')
|
|
86
|
+
|
|
87
|
+
expect(queryClient.getMutationCache().findAll()).toHaveLength(1)
|
|
88
|
+
|
|
89
|
+
mutation.reset()
|
|
90
|
+
|
|
91
|
+
await waitFor(() =>
|
|
92
|
+
expect(queryClient.getMutationCache().findAll()).toHaveLength(0),
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
unsubscribe()
|
|
96
|
+
})
|
|
47
97
|
})
|