@tanstack/query-core 5.28.0 → 5.28.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/mutationObserver.cjs +2 -2
- package/build/legacy/mutationObserver.cjs.map +1 -1
- package/build/legacy/mutationObserver.js +2 -2
- package/build/legacy/mutationObserver.js.map +1 -1
- package/build/modern/mutationObserver.cjs +2 -2
- package/build/modern/mutationObserver.cjs.map +1 -1
- package/build/modern/mutationObserver.js +2 -2
- package/build/modern/mutationObserver.js.map +1 -1
- package/package.json +1 -1
- package/src/mutationObserver.ts +2 -2
- package/src/tests/mutationObserver.test.tsx +154 -0
|
@@ -81,8 +81,8 @@ var MutationObserver = class extends import_subscribable.Subscribable {
|
|
|
81
81
|
}
|
|
82
82
|
if ((prevOptions == null ? void 0 : prevOptions.mutationKey) && this.options.mutationKey && (0, import_utils.hashKey)(prevOptions.mutationKey) !== (0, import_utils.hashKey)(this.options.mutationKey)) {
|
|
83
83
|
this.reset();
|
|
84
|
-
} else {
|
|
85
|
-
|
|
84
|
+
} else if (((_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.state.status) === "pending") {
|
|
85
|
+
__privateGet(this, _currentMutation).setOptions(this.options);
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
onUnsubscribe() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"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 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 as\n | MutationObserverOptions<TData, TError, TVariables, TContext>\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 {\n this.#currentMutation
|
|
1
|
+
{"version":3,"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 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 as\n | MutationObserverOptions<TData, TError, TVariables, TContext>\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 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 const variables = this.#currentResult.variables!\n const context = this.#currentResult.context\n\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(action.data, variables, context!)\n this.#mutateOptions.onSettled?.(action.data, null, variables, context)\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(action.error, variables, context)\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n variables,\n 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,mBAA6C;AAH7C;AAqBO,IAAM,mBAAN,cAKG,iCAER;AAAA,EASA,YACE,QACA,SACA;AACA,UAAM;AAsFR;AAgBA;AAhHA;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,KA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E;AACA;AAEA;AACA;AA4FA;AAAA,kBAAa,WAAS;AA/HxB;AAgII,QAAM,UACJ,wBAAK,sBAAL,mBAAuB,cACvB,iCAAqD;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,qCAAc,MAAM,MAAM;AAhJ9B;AAkJM,QAAI,mBAAK,mBAAkB,KAAK,aAAa,GAAG;AAC9C,YAAM,YAAY,mBAAK,gBAAe;AACtC,YAAM,UAAU,mBAAK,gBAAe;AAEpC,WAAI,iCAAQ,UAAS,WAAW;AAC9B,uCAAK,iBAAe,cAApB,4BAAgC,OAAO,MAAM,WAAW;AACxD,uCAAK,iBAAe,cAApB,4BAAgC,OAAO,MAAM,MAAM,WAAW;AAAA,MAChE,YAAW,iCAAQ,UAAS,SAAS;AACnC,uCAAK,iBAAe,YAApB,4BAA8B,OAAO,OAAO,WAAW;AACvD,uCAAK,iBAAe,cAApB;AAAA;AAAA,UACE;AAAA,UACA,OAAO;AAAA,UACP;AAAA,UACA;AAAA;AAAA,MAEJ;AAAA,IACF;AAGA,SAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,eAAS,mBAAK,eAAc;AAAA,IAC9B,CAAC;AAAA,EACH,CAAC;AACH;","names":[]}
|
|
@@ -42,8 +42,8 @@ var MutationObserver = class extends Subscribable {
|
|
|
42
42
|
}
|
|
43
43
|
if ((prevOptions == null ? void 0 : prevOptions.mutationKey) && this.options.mutationKey && hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)) {
|
|
44
44
|
this.reset();
|
|
45
|
-
} else {
|
|
46
|
-
|
|
45
|
+
} else if (((_a = __privateGet(this, _currentMutation)) == null ? void 0 : _a.state.status) === "pending") {
|
|
46
|
+
__privateGet(this, _currentMutation).setOptions(this.options);
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
onUnsubscribe() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"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 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 as\n | MutationObserverOptions<TData, TError, TVariables, TContext>\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 {\n this.#currentMutation
|
|
1
|
+
{"version":3,"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 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 as\n | MutationObserverOptions<TData, TError, TVariables, TContext>\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 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 const variables = this.#currentResult.variables!\n const context = this.#currentResult.context\n\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(action.data, variables, context!)\n this.#mutateOptions.onSettled?.(action.data, null, variables, context)\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(action.error, variables, context)\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n variables,\n 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,SAAS,2BAA2B;AAH7C;AAqBO,IAAM,mBAAN,cAKG,aAER;AAAA,EASA,YACE,QACA,SACA;AACA,UAAM;AAsFR;AAgBA;AAhHA;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;AAGzB,SAAK,UAAU,mBAAK,SAAQ,uBAAuB,OAAO;AAC1D,QAAI,CAAC,oBAAoB,KAAK,SAAS,WAAW,GAAG;AACnD,yBAAK,SAAQ,iBAAiB,EAAE,OAAO;AAAA,QACrC,MAAM;AAAA,QACN,UAAU,mBAAK;AAAA,QACf,UAAU;AAAA,MACZ,CAAC;AAAA,IACH;AAEA,SACE,2CAAa,gBACb,KAAK,QAAQ,eACb,QAAQ,YAAY,WAAW,MAAM,QAAQ,KAAK,QAAQ,WAAW,GACrE;AACA,WAAK,MAAM;AAAA,IACb,aAAW,wBAAK,sBAAL,mBAAuB,MAAM,YAAW,WAAW;AAC5D,yBAAK,kBAAiB,WAAW,KAAK,OAAO;AAAA,IAC/C;AAAA,EACF;AAAA,EAEU,gBAAsB;AAhFlC;AAiFI,QAAI,CAAC,KAAK,aAAa,GAAG;AACxB,+BAAK,sBAAL,mBAAuB,eAAe;AAAA,IACxC;AAAA,EACF;AAAA,EAEA,iBAAiB,QAA2D;AAC1E,0BAAK,gCAAL;AAEA,0BAA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AO,WAAW;AACvD,uCAAK,iBAAe,cAApB;AAAA;AAAA,UACE;AAAA,UACA,OAAO;AAAA,UACP;AAAA,UACA;AAAA;AAAA,MAEJ;AAAA,IACF;AAGA,SAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,eAAS,mBAAK,eAAc;AAAA,IAC9B,CAAC;AAAA,EACH,CAAC;AACH;","names":[]}
|
|
@@ -55,8 +55,8 @@ var MutationObserver = class extends import_subscribable.Subscribable {
|
|
|
55
55
|
}
|
|
56
56
|
if (prevOptions?.mutationKey && this.options.mutationKey && (0, import_utils.hashKey)(prevOptions.mutationKey) !== (0, import_utils.hashKey)(this.options.mutationKey)) {
|
|
57
57
|
this.reset();
|
|
58
|
-
} else {
|
|
59
|
-
this.#currentMutation
|
|
58
|
+
} else if (this.#currentMutation?.state.status === "pending") {
|
|
59
|
+
this.#currentMutation.setOptions(this.options);
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
onUnsubscribe() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"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 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 as\n | MutationObserverOptions<TData, TError, TVariables, TContext>\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 {\n this.#currentMutation
|
|
1
|
+
{"version":3,"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 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 as\n | MutationObserverOptions<TData, TError, TVariables, TContext>\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 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 const variables = this.#currentResult.variables!\n const context = this.#currentResult.context\n\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(action.data, variables, context!)\n this.#mutateOptions.onSettled?.(action.data, null, variables, context)\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(action.error, variables, context)\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n variables,\n 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,mBAA6C;AAkBtC,IAAM,mBAAN,cAKG,iCAER;AAAA,EAGA;AAAA,EACA,iBACE;AAAA,EACF;AAAA,EACA;AAAA,EAEA,YACE,QACA,SACA;AACA,UAAM;AAEN,SAAK,UAAU;AACf,SAAK,WAAW,OAAO;AACvB,SAAK,YAAY;AACjB,SAAK,cAAc;AAAA,EACrB;AAAA,EAEU,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;AAGzB,SAAK,UAAU,KAAK,QAAQ,uBAAuB,OAAO;AAC1D,QAAI,KAAC,kCAAoB,KAAK,SAAS,WAAW,GAAG;AACnD,WAAK,QAAQ,iBAAiB,EAAE,OAAO;AAAA,QACrC,MAAM;AAAA,QACN,UAA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E,uCAAc,MAAM,MAAM;AAExB,UAAI,KAAK,kBAAkB,KAAK,aAAa,GAAG;AAC9C,cAAM,YAAY,KAAK,eAAe;AACtC,cAAM,UAAU,KAAK,eAAe;AAEpC,YAAI,QAAQ,SAAS,WAAW;AAC9B,eAAK,eAAe,YAAY,OAAO,MAAM,WAAW,OAAQ;AAChE,eAAK,eAAe,YAAY,OAAO,MAAM,MAAM,WAAW,OAAO;AAAA,QACvE,WAAW,QAAQ,SAAS,SAAS;AACnC,eAAK,eAAe,UAAU,OAAO,OAAO,WAAW,OAAO;AAC9D,eAAK,eAAe;AAAA,YAClB;AAAA,YACA,OAAO;AAAA,YACP;AAAA,YACA;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAGA,WAAK,UAAU,QAAQ,CAAC,aAAa;AACnC,iBAAS,KAAK,cAAc;AAAA,MAC9B,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AACF;","names":[]}
|
|
@@ -31,8 +31,8 @@ var MutationObserver = class extends Subscribable {
|
|
|
31
31
|
}
|
|
32
32
|
if (prevOptions?.mutationKey && this.options.mutationKey && hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)) {
|
|
33
33
|
this.reset();
|
|
34
|
-
} else {
|
|
35
|
-
this.#currentMutation
|
|
34
|
+
} else if (this.#currentMutation?.state.status === "pending") {
|
|
35
|
+
this.#currentMutation.setOptions(this.options);
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
onUnsubscribe() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"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 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 as\n | MutationObserverOptions<TData, TError, TVariables, TContext>\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 {\n this.#currentMutation
|
|
1
|
+
{"version":3,"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 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 as\n | MutationObserverOptions<TData, TError, TVariables, TContext>\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 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 const variables = this.#currentResult.variables!\n const context = this.#currentResult.context\n\n if (action?.type === 'success') {\n this.#mutateOptions.onSuccess?.(action.data, variables, context!)\n this.#mutateOptions.onSettled?.(action.data, null, variables, context)\n } else if (action?.type === 'error') {\n this.#mutateOptions.onError?.(action.error, variables, context)\n this.#mutateOptions.onSettled?.(\n undefined,\n action.error,\n variables,\n 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,SAAS,2BAA2B;AAkBtC,IAAM,mBAAN,cAKG,aAER;AAAA,EAGA;AAAA,EACA,iBACE;AAAA,EACF;AAAA,EACA;AAAA,EAEA,YACE,QACA,SACA;AACA,UAAM;AAEN,SAAK,UAAU;AACf,SAAK,WAAW,OAAO;AACvB,SAAK,YAAY;AACjB,SAAK,cAAc;AAAA,EACrB;AAAA,EAEU,cAAoB;AAC5B,SAAK,SAAS,KAAK,O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AAkB,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,cAAM,YAAY,KAAK,eAAe;AACtC,cAAM,UAAU,KAAK,eAAe;AAEpC,YAAI,QAAQ,SAAS,WAAW;AAC9B,eAAK,eAAe,YAAY,OAAO,MAAM,WAAW,OAAQ;AAChE,eAAK,eAAe,YAAY,OAAO,MAAM,MAAM,WAAW,OAAO;AAAA,QACvE,WAAW,QAAQ,SAAS,SAAS;AACnC,eAAK,eAAe,UAAU,OAAO,OAAO,WAAW,OAAO;AAC9D,eAAK,eAAe;AAAA,YAClB;AAAA,YACA,OAAO;AAAA,YACP;AAAA,YACA;AAAA,UACF;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
|
@@ -73,8 +73,8 @@ export class MutationObserver<
|
|
|
73
73
|
hashKey(prevOptions.mutationKey) !== hashKey(this.options.mutationKey)
|
|
74
74
|
) {
|
|
75
75
|
this.reset()
|
|
76
|
-
} else {
|
|
77
|
-
this.#currentMutation
|
|
76
|
+
} else if (this.#currentMutation?.state.status === 'pending') {
|
|
77
|
+
this.#currentMutation.setOptions(this.options)
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
|
|
@@ -169,4 +169,158 @@ describe('mutationObserver', () => {
|
|
|
169
169
|
|
|
170
170
|
unsubscribe()
|
|
171
171
|
})
|
|
172
|
+
|
|
173
|
+
test('changing mutation meta should not affect successful mutations', async () => {
|
|
174
|
+
const mutationObserver = new MutationObserver(queryClient, {
|
|
175
|
+
meta: { a: 1 },
|
|
176
|
+
mutationFn: async (text: string) => {
|
|
177
|
+
await sleep(5)
|
|
178
|
+
return text
|
|
179
|
+
},
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
const subscriptionHandler = vi.fn()
|
|
183
|
+
|
|
184
|
+
const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
|
|
185
|
+
|
|
186
|
+
await mutationObserver.mutate('input')
|
|
187
|
+
|
|
188
|
+
expect(queryClient.getMutationCache().find({})).toMatchObject({
|
|
189
|
+
options: { meta: { a: 1 } },
|
|
190
|
+
state: {
|
|
191
|
+
status: 'success',
|
|
192
|
+
data: 'input',
|
|
193
|
+
},
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
mutationObserver.setOptions({
|
|
197
|
+
meta: { a: 2 },
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
expect(queryClient.getMutationCache().find({})).toMatchObject({
|
|
201
|
+
options: { meta: { a: 1 } },
|
|
202
|
+
state: {
|
|
203
|
+
status: 'success',
|
|
204
|
+
data: 'input',
|
|
205
|
+
},
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
unsubscribe()
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
test('mutation cache should have different meta when updated between mutations', async () => {
|
|
212
|
+
const mutationFn = async (text: string) => {
|
|
213
|
+
await sleep(5)
|
|
214
|
+
return text
|
|
215
|
+
}
|
|
216
|
+
const mutationObserver = new MutationObserver(queryClient, {
|
|
217
|
+
meta: { a: 1 },
|
|
218
|
+
mutationFn,
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
const subscriptionHandler = vi.fn()
|
|
222
|
+
|
|
223
|
+
const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
|
|
224
|
+
|
|
225
|
+
await mutationObserver.mutate('input')
|
|
226
|
+
|
|
227
|
+
mutationObserver.setOptions({
|
|
228
|
+
meta: { a: 2 },
|
|
229
|
+
mutationFn,
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
await mutationObserver.mutate('input')
|
|
233
|
+
|
|
234
|
+
const mutations = queryClient.getMutationCache().findAll()
|
|
235
|
+
expect(mutations[0]).toMatchObject({
|
|
236
|
+
options: { meta: { a: 1 } },
|
|
237
|
+
state: {
|
|
238
|
+
status: 'success',
|
|
239
|
+
data: 'input',
|
|
240
|
+
},
|
|
241
|
+
})
|
|
242
|
+
expect(mutations[1]).toMatchObject({
|
|
243
|
+
options: { meta: { a: 2 } },
|
|
244
|
+
state: {
|
|
245
|
+
status: 'success',
|
|
246
|
+
data: 'input',
|
|
247
|
+
},
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
unsubscribe()
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
test('changing mutation meta should not affect rejected mutations', async () => {
|
|
254
|
+
const mutationObserver = new MutationObserver(queryClient, {
|
|
255
|
+
meta: { a: 1 },
|
|
256
|
+
mutationFn: async (_: string) => {
|
|
257
|
+
await sleep(5)
|
|
258
|
+
return Promise.reject(new Error('err'))
|
|
259
|
+
},
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
const subscriptionHandler = vi.fn()
|
|
263
|
+
|
|
264
|
+
const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
|
|
265
|
+
|
|
266
|
+
await mutationObserver.mutate('input').catch(() => undefined)
|
|
267
|
+
|
|
268
|
+
expect(queryClient.getMutationCache().find({})).toMatchObject({
|
|
269
|
+
options: { meta: { a: 1 } },
|
|
270
|
+
state: {
|
|
271
|
+
status: 'error',
|
|
272
|
+
},
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
mutationObserver.setOptions({
|
|
276
|
+
meta: { a: 2 },
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
expect(queryClient.getMutationCache().find({})).toMatchObject({
|
|
280
|
+
options: { meta: { a: 1 } },
|
|
281
|
+
state: {
|
|
282
|
+
status: 'error',
|
|
283
|
+
},
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
unsubscribe()
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
test('changing mutation meta should affect pending mutations', async () => {
|
|
290
|
+
const mutationObserver = new MutationObserver(queryClient, {
|
|
291
|
+
meta: { a: 1 },
|
|
292
|
+
mutationFn: async (text: string) => {
|
|
293
|
+
await sleep(20)
|
|
294
|
+
return text
|
|
295
|
+
},
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
const subscriptionHandler = vi.fn()
|
|
299
|
+
|
|
300
|
+
const unsubscribe = mutationObserver.subscribe(subscriptionHandler)
|
|
301
|
+
|
|
302
|
+
mutationObserver.mutate('input')
|
|
303
|
+
|
|
304
|
+
await sleep(0)
|
|
305
|
+
|
|
306
|
+
expect(queryClient.getMutationCache().find({})).toMatchObject({
|
|
307
|
+
options: { meta: { a: 1 } },
|
|
308
|
+
state: {
|
|
309
|
+
status: 'pending',
|
|
310
|
+
},
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
mutationObserver.setOptions({
|
|
314
|
+
meta: { a: 2 },
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
expect(queryClient.getMutationCache().find({})).toMatchObject({
|
|
318
|
+
options: { meta: { a: 2 } },
|
|
319
|
+
state: {
|
|
320
|
+
status: 'pending',
|
|
321
|
+
},
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
unsubscribe()
|
|
325
|
+
})
|
|
172
326
|
})
|