@tanstack/query-core 4.26.0 → 4.26.1
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/lib/mutation.d.ts +2 -0
- package/build/lib/mutation.esm.js +9 -4
- package/build/lib/mutation.esm.js.map +1 -1
- package/build/lib/mutation.js +9 -4
- package/build/lib/mutation.js.map +1 -1
- package/build/lib/mutation.mjs +9 -4
- package/build/lib/mutation.mjs.map +1 -1
- package/build/lib/mutationObserver.esm.js +6 -2
- package/build/lib/mutationObserver.esm.js.map +1 -1
- package/build/lib/mutationObserver.js +6 -2
- package/build/lib/mutationObserver.js.map +1 -1
- package/build/lib/mutationObserver.mjs +6 -2
- package/build/lib/mutationObserver.mjs.map +1 -1
- package/build/umd/index.development.js +15 -6
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/mutation.ts +12 -6
- package/src/mutationObserver.ts +1 -0
- package/src/tests/mutations.test.tsx +31 -0
|
@@ -23,6 +23,8 @@ class MutationObserver extends subscribable.Subscribable {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
setOptions(options) {
|
|
26
|
+
var _this$currentMutation;
|
|
27
|
+
|
|
26
28
|
const prevOptions = this.options;
|
|
27
29
|
this.options = this.client.defaultMutationOptions(options);
|
|
28
30
|
|
|
@@ -33,13 +35,15 @@ class MutationObserver extends subscribable.Subscribable {
|
|
|
33
35
|
observer: this
|
|
34
36
|
});
|
|
35
37
|
}
|
|
38
|
+
|
|
39
|
+
(_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.setOptions(this.options);
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
onUnsubscribe() {
|
|
39
43
|
if (!this.listeners.length) {
|
|
40
|
-
var _this$
|
|
44
|
+
var _this$currentMutation2;
|
|
41
45
|
|
|
42
|
-
(_this$
|
|
46
|
+
(_this$currentMutation2 = this.currentMutation) == null ? void 0 : _this$currentMutation2.removeObserver(this);
|
|
43
47
|
}
|
|
44
48
|
}
|
|
45
49
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutationObserver.js","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import type { Action, Mutation } from './mutation'\nimport { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport type { QueryClient } from './queryClient'\nimport { Subscribable } from './subscribable'\nimport type {\n MutateOptions,\n MutationObserverBaseResult,\n MutationObserverResult,\n MutationObserverOptions,\n} from './types'\nimport { shallowEqualObjects } from './utils'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\ninterface NotifyOptions {\n listeners?: boolean\n onError?: boolean\n onSuccess?: boolean\n}\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = unknown,\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 private client: QueryClient\n private currentResult!: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n >\n private currentMutation?: Mutation<TData, TError, TVariables, TContext>\n private 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 }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.updateResult()\n\n // Determine which callbacks to trigger\n const notifyOptions: NotifyOptions = {\n listeners: true,\n }\n\n if (action.type === 'success') {\n notifyOptions.onSuccess = true\n } else if (action.type === 'error') {\n notifyOptions.onError = true\n }\n\n this.notify(notifyOptions)\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({ listeners: true })\n }\n\n mutate(\n variables?: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\n ): Promise<TData> {\n this.mutateOptions = options\n\n if (this.currentMutation) {\n this.currentMutation.removeObserver(this)\n }\n\n this.currentMutation = this.client.getMutationCache().build(this.client, {\n ...this.options,\n variables:\n typeof variables !== 'undefined' ? variables : this.options.variables,\n })\n\n this.currentMutation.addObserver(this)\n\n return this.currentMutation.execute()\n }\n\n private updateResult(): void {\n const state = this.currentMutation\n ? this.currentMutation.state\n : getDefaultState<TData, TError, TVariables, TContext>()\n\n const result: MutationObserverBaseResult<\n TData,\n TError,\n TVariables,\n TContext\n > = {\n ...state,\n isLoading: state.status === 'loading',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n }\n\n this.currentResult = result as MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n >\n }\n\n private notify(options: NotifyOptions) {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.mutateOptions && this.hasListeners()) {\n if (options.onSuccess) {\n this.mutateOptions.onSuccess?.(\n this.currentResult.data!,\n this.currentResult.variables!,\n this.currentResult.context!,\n )\n this.mutateOptions.onSettled?.(\n this.currentResult.data!,\n null,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n } else if (options.onError) {\n this.mutateOptions.onError?.(\n this.currentResult.error!,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n this.mutateOptions.onSettled?.(\n undefined,\n this.currentResult.error,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n if (options.listeners) {\n this.listeners.forEach((listener) => {\n listener(this.currentResult)\n })\n }\n })\n }\n}\n"],"names":["MutationObserver","Subscribable","constructor","client","options","setOptions","bindMethods","updateResult","mutate","bind","reset","prevOptions","defaultMutationOptions","shallowEqualObjects","getMutationCache","notify","type","mutation","currentMutation","observer","onUnsubscribe","listeners","length","removeObserver","onMutationUpdate","action","notifyOptions","onSuccess","onError","getCurrentResult","currentResult","undefined","variables","mutateOptions","build","addObserver","execute","state","getDefaultState","result","isLoading","status","isSuccess","isError","isIdle","notifyManager","batch","hasListeners","data","context","onSettled","error","forEach","listener"],"mappings":";;;;;;;;;AAyBA;AAEO,MAAMA,gBAAN,SAKGC,yBALH,CAOL;AAaAC,EAAAA,WAAW,CACTC,MADS,EAETC,OAFS,EAGT;AACA,IAAA,KAAA,EAAA,CAAA;IAEA,IAAKD,CAAAA,MAAL,GAAcA,MAAd,CAAA;IACA,IAAKE,CAAAA,UAAL,CAAgBD,OAAhB,CAAA,CAAA;AACA,IAAA,IAAA,CAAKE,WAAL,EAAA,CAAA;AACA,IAAA,IAAA,CAAKC,YAAL,EAAA,CAAA;AACD,GAAA;;AAESD,EAAAA,WAAW,GAAS;IAC5B,IAAKE,CAAAA,MAAL,GAAc,IAAKA,CAAAA,MAAL,CAAYC,IAAZ,CAAiB,IAAjB,CAAd,CAAA;IACA,IAAKC,CAAAA,KAAL,GAAa,IAAKA,CAAAA,KAAL,CAAWD,IAAX,CAAgB,IAAhB,CAAb,CAAA;AACD,GAAA;;EAEDJ,UAAU,CACRD,OADQ,EAER;IACA,MAAMO,WAAW,GAAG,IAAA,CAAKP,OAAzB,CAAA;IACA,IAAKA,CAAAA,OAAL,GAAe,IAAKD,CAAAA,MAAL,CAAYS,sBAAZ,CAA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AKE,aAAL,CAAmBqB,KADrB,EAEE,IAAA,CAAKrB,aAAL,CAAmBE,SAFrB,EAGE,IAAKF,CAAAA,aAAL,CAAmBmB,OAHrB,CAAA,CAAA;AAKA,UAAA,CAAA,sBAAA,GAAA,CAAA,oBAAA,GAAA,IAAA,CAAKhB,aAAL,EAAmBiB,SAAnB,uEACEnB,SADF,EAEE,KAAKD,aAAL,CAAmBqB,KAFrB,EAGE,IAAA,CAAKrB,aAAL,CAAmBE,SAHrB,EAIE,IAAKF,CAAAA,aAAL,CAAmBmB,OAJrB,CAAA,CAAA;AAMD,SAAA;AACF,OA5BuB;;;MA+BxB,IAAI7C,OAAO,CAACiB,SAAZ,EAAuB;AACrB,QAAA,IAAA,CAAKA,SAAL,CAAe+B,OAAf,CAAwBC,QAAD,IAAc;UACnCA,QAAQ,CAAC,IAAKvB,CAAAA,aAAN,CAAR,CAAA;SADF,CAAA,CAAA;AAGD,OAAA;KAnCH,CAAA,CAAA;AAqCD,GAAA;;AAzKD;;;;"}
|
|
1
|
+
{"version":3,"file":"mutationObserver.js","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import type { Action, Mutation } from './mutation'\nimport { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport type { QueryClient } from './queryClient'\nimport { Subscribable } from './subscribable'\nimport type {\n MutateOptions,\n MutationObserverBaseResult,\n MutationObserverResult,\n MutationObserverOptions,\n} from './types'\nimport { shallowEqualObjects } from './utils'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\ninterface NotifyOptions {\n listeners?: boolean\n onError?: boolean\n onSuccess?: boolean\n}\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = unknown,\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 private client: QueryClient\n private currentResult!: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n >\n private currentMutation?: Mutation<TData, TError, TVariables, TContext>\n private 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.listeners.length) {\n this.currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.updateResult()\n\n // Determine which callbacks to trigger\n const notifyOptions: NotifyOptions = {\n listeners: true,\n }\n\n if (action.type === 'success') {\n notifyOptions.onSuccess = true\n } else if (action.type === 'error') {\n notifyOptions.onError = true\n }\n\n this.notify(notifyOptions)\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({ listeners: true })\n }\n\n mutate(\n variables?: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\n ): Promise<TData> {\n this.mutateOptions = options\n\n if (this.currentMutation) {\n this.currentMutation.removeObserver(this)\n }\n\n this.currentMutation = this.client.getMutationCache().build(this.client, {\n ...this.options,\n variables:\n typeof variables !== 'undefined' ? variables : this.options.variables,\n })\n\n this.currentMutation.addObserver(this)\n\n return this.currentMutation.execute()\n }\n\n private updateResult(): void {\n const state = this.currentMutation\n ? this.currentMutation.state\n : getDefaultState<TData, TError, TVariables, TContext>()\n\n const result: MutationObserverBaseResult<\n TData,\n TError,\n TVariables,\n TContext\n > = {\n ...state,\n isLoading: state.status === 'loading',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n }\n\n this.currentResult = result as MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n >\n }\n\n private notify(options: NotifyOptions) {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.mutateOptions && this.hasListeners()) {\n if (options.onSuccess) {\n this.mutateOptions.onSuccess?.(\n this.currentResult.data!,\n this.currentResult.variables!,\n this.currentResult.context!,\n )\n this.mutateOptions.onSettled?.(\n this.currentResult.data!,\n null,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n } else if (options.onError) {\n this.mutateOptions.onError?.(\n this.currentResult.error!,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n this.mutateOptions.onSettled?.(\n undefined,\n this.currentResult.error,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n if (options.listeners) {\n this.listeners.forEach((listener) => {\n listener(this.currentResult)\n })\n }\n })\n }\n}\n"],"names":["MutationObserver","Subscribable","constructor","client","options","setOptions","bindMethods","updateResult","mutate","bind","reset","prevOptions","defaultMutationOptions","shallowEqualObjects","getMutationCache","notify","type","mutation","currentMutation","observer","onUnsubscribe","listeners","length","removeObserver","onMutationUpdate","action","notifyOptions","onSuccess","onError","getCurrentResult","currentResult","undefined","variables","mutateOptions","build","addObserver","execute","state","getDefaultState","result","isLoading","status","isSuccess","isError","isIdle","notifyManager","batch","hasListeners","data","context","onSettled","error","forEach","listener"],"mappings":";;;;;;;;;AAyBA;AAEO,MAAMA,gBAAN,SAKGC,yBALH,CAOL;AAaAC,EAAAA,WAAW,CACTC,MADS,EAETC,OAFS,EAGT;AACA,IAAA,KAAA,EAAA,CAAA;IAEA,IAAKD,CAAAA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,QAAQ,CAAC,IAAKvB,CAAAA,aAAN,CAAR,CAAA;SADF,CAAA,CAAA;AAGD,OAAA;KAnCH,CAAA,CAAA;AAqCD,GAAA;;AA1KD;;;;"}
|
|
@@ -19,6 +19,8 @@ class MutationObserver extends Subscribable {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
setOptions(options) {
|
|
22
|
+
var _this$currentMutation;
|
|
23
|
+
|
|
22
24
|
const prevOptions = this.options;
|
|
23
25
|
this.options = this.client.defaultMutationOptions(options);
|
|
24
26
|
|
|
@@ -29,13 +31,15 @@ class MutationObserver extends Subscribable {
|
|
|
29
31
|
observer: this
|
|
30
32
|
});
|
|
31
33
|
}
|
|
34
|
+
|
|
35
|
+
(_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.setOptions(this.options);
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
onUnsubscribe() {
|
|
35
39
|
if (!this.listeners.length) {
|
|
36
|
-
var _this$
|
|
40
|
+
var _this$currentMutation2;
|
|
37
41
|
|
|
38
|
-
(_this$
|
|
42
|
+
(_this$currentMutation2 = this.currentMutation) == null ? void 0 : _this$currentMutation2.removeObserver(this);
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
45
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutationObserver.mjs","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import type { Action, Mutation } from './mutation'\nimport { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport type { QueryClient } from './queryClient'\nimport { Subscribable } from './subscribable'\nimport type {\n MutateOptions,\n MutationObserverBaseResult,\n MutationObserverResult,\n MutationObserverOptions,\n} from './types'\nimport { shallowEqualObjects } from './utils'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\ninterface NotifyOptions {\n listeners?: boolean\n onError?: boolean\n onSuccess?: boolean\n}\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = unknown,\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 private client: QueryClient\n private currentResult!: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n >\n private currentMutation?: Mutation<TData, TError, TVariables, TContext>\n private 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 }\n\n protected onUnsubscribe(): void {\n if (!this.listeners.length) {\n this.currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.updateResult()\n\n // Determine which callbacks to trigger\n const notifyOptions: NotifyOptions = {\n listeners: true,\n }\n\n if (action.type === 'success') {\n notifyOptions.onSuccess = true\n } else if (action.type === 'error') {\n notifyOptions.onError = true\n }\n\n this.notify(notifyOptions)\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({ listeners: true })\n }\n\n mutate(\n variables?: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\n ): Promise<TData> {\n this.mutateOptions = options\n\n if (this.currentMutation) {\n this.currentMutation.removeObserver(this)\n }\n\n this.currentMutation = this.client.getMutationCache().build(this.client, {\n ...this.options,\n variables:\n typeof variables !== 'undefined' ? variables : this.options.variables,\n })\n\n this.currentMutation.addObserver(this)\n\n return this.currentMutation.execute()\n }\n\n private updateResult(): void {\n const state = this.currentMutation\n ? this.currentMutation.state\n : getDefaultState<TData, TError, TVariables, TContext>()\n\n const result: MutationObserverBaseResult<\n TData,\n TError,\n TVariables,\n TContext\n > = {\n ...state,\n isLoading: state.status === 'loading',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n }\n\n this.currentResult = result as MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n >\n }\n\n private notify(options: NotifyOptions) {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.mutateOptions && this.hasListeners()) {\n if (options.onSuccess) {\n this.mutateOptions.onSuccess?.(\n this.currentResult.data!,\n this.currentResult.variables!,\n this.currentResult.context!,\n )\n this.mutateOptions.onSettled?.(\n this.currentResult.data!,\n null,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n } else if (options.onError) {\n this.mutateOptions.onError?.(\n this.currentResult.error!,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n this.mutateOptions.onSettled?.(\n undefined,\n this.currentResult.error,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n if (options.listeners) {\n this.listeners.forEach((listener) => {\n listener(this.currentResult)\n })\n }\n })\n }\n}\n"],"names":["MutationObserver","Subscribable","constructor","client","options","setOptions","bindMethods","updateResult","mutate","bind","reset","prevOptions","defaultMutationOptions","shallowEqualObjects","getMutationCache","notify","type","mutation","currentMutation","observer","onUnsubscribe","listeners","length","removeObserver","onMutationUpdate","action","notifyOptions","onSuccess","onError","getCurrentResult","currentResult","undefined","variables","mutateOptions","build","addObserver","execute","state","getDefaultState","result","isLoading","status","isSuccess","isError","isIdle","notifyManager","batch","hasListeners","data","context","onSettled","error","forEach","listener"],"mappings":";;;;;AAyBA;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AAA;SADF,CAAA,CAAA;AAGD,OAAA;KAnCH,CAAA,CAAA;AAqCD,GAAA;;AAzKD;;;;"}
|
|
1
|
+
{"version":3,"file":"mutationObserver.mjs","sources":["../../src/mutationObserver.ts"],"sourcesContent":["import type { Action, Mutation } from './mutation'\nimport { getDefaultState } from './mutation'\nimport { notifyManager } from './notifyManager'\nimport type { QueryClient } from './queryClient'\nimport { Subscribable } from './subscribable'\nimport type {\n MutateOptions,\n MutationObserverBaseResult,\n MutationObserverResult,\n MutationObserverOptions,\n} from './types'\nimport { shallowEqualObjects } from './utils'\n\n// TYPES\n\ntype MutationObserverListener<TData, TError, TVariables, TContext> = (\n result: MutationObserverResult<TData, TError, TVariables, TContext>,\n) => void\n\ninterface NotifyOptions {\n listeners?: boolean\n onError?: boolean\n onSuccess?: boolean\n}\n\n// CLASS\n\nexport class MutationObserver<\n TData = unknown,\n TError = unknown,\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 private client: QueryClient\n private currentResult!: MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n >\n private currentMutation?: Mutation<TData, TError, TVariables, TContext>\n private 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.listeners.length) {\n this.currentMutation?.removeObserver(this)\n }\n }\n\n onMutationUpdate(action: Action<TData, TError, TVariables, TContext>): void {\n this.updateResult()\n\n // Determine which callbacks to trigger\n const notifyOptions: NotifyOptions = {\n listeners: true,\n }\n\n if (action.type === 'success') {\n notifyOptions.onSuccess = true\n } else if (action.type === 'error') {\n notifyOptions.onError = true\n }\n\n this.notify(notifyOptions)\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({ listeners: true })\n }\n\n mutate(\n variables?: TVariables,\n options?: MutateOptions<TData, TError, TVariables, TContext>,\n ): Promise<TData> {\n this.mutateOptions = options\n\n if (this.currentMutation) {\n this.currentMutation.removeObserver(this)\n }\n\n this.currentMutation = this.client.getMutationCache().build(this.client, {\n ...this.options,\n variables:\n typeof variables !== 'undefined' ? variables : this.options.variables,\n })\n\n this.currentMutation.addObserver(this)\n\n return this.currentMutation.execute()\n }\n\n private updateResult(): void {\n const state = this.currentMutation\n ? this.currentMutation.state\n : getDefaultState<TData, TError, TVariables, TContext>()\n\n const result: MutationObserverBaseResult<\n TData,\n TError,\n TVariables,\n TContext\n > = {\n ...state,\n isLoading: state.status === 'loading',\n isSuccess: state.status === 'success',\n isError: state.status === 'error',\n isIdle: state.status === 'idle',\n mutate: this.mutate,\n reset: this.reset,\n }\n\n this.currentResult = result as MutationObserverResult<\n TData,\n TError,\n TVariables,\n TContext\n >\n }\n\n private notify(options: NotifyOptions) {\n notifyManager.batch(() => {\n // First trigger the mutate callbacks\n if (this.mutateOptions && this.hasListeners()) {\n if (options.onSuccess) {\n this.mutateOptions.onSuccess?.(\n this.currentResult.data!,\n this.currentResult.variables!,\n this.currentResult.context!,\n )\n this.mutateOptions.onSettled?.(\n this.currentResult.data!,\n null,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n } else if (options.onError) {\n this.mutateOptions.onError?.(\n this.currentResult.error!,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n this.mutateOptions.onSettled?.(\n undefined,\n this.currentResult.error,\n this.currentResult.variables!,\n this.currentResult.context,\n )\n }\n }\n\n // Then trigger the listeners\n if (options.listeners) {\n this.listeners.forEach((listener) => {\n listener(this.currentResult)\n })\n }\n })\n }\n}\n"],"names":["MutationObserver","Subscribable","constructor","client","options","setOptions","bindMethods","updateResult","mutate","bind","reset","prevOptions","defaultMutationOptions","shallowEqualObjects","getMutationCache","notify","type","mutation","currentMutation","observer","onUnsubscribe","listeners","length","removeObserver","onMutationUpdate","action","notifyOptions","onSuccess","onError","getCurrentResult","currentResult","undefined","variables","mutateOptions","build","addObserver","execute","state","getDefaultState","result","isLoading","status","isSuccess","isError","isIdle","notifyManager","batch","hasListeners","data","context","onSettled","error","forEach","listener"],"mappings":";;;;;AAyBA;AAEO,MAAMA,gBAAN,SAKGC,YALH,CAOL;AAaAC,EAAAA,WAAW,CACTC,MADS,EAETC,OAFS,EAGT;AACA,IAAA,KAAA,EAAA,CAAA;IAEA,IAAKD,CAAAA,MAAL,GAAcA,MAAd,CAAA;IACA,IAAKE,CAAAA,UAAL,CAAgBD,OAAhB,CAAA,CAAA;AACA,IAAA,IAAA,CAAKE,WAAL,EAAA,CAAA;AACA,IAAA,IAAA,CAAKC,YAAL,EAAA,CAAA;AACD,GAAA;;AAESD,EAAAA,WAAW,GAAS;IAC5B,IAAKE,CAAAA,MAAL,GAAc,IAAKA,CAAAA,MAAL,CAAYC,IAAZ,CAAiB,IAAjB,CAAd,CAAA;IACA,IAAKC,CAAAA,KAAL,GAAa,IAAKA,CAAAA,KAAL,CAAWD,IAAX,CAAgB,IAAhB,CAAb,CAAA;AACD,GAAA;;EAEDJ,UAAU,CACRD,OADQ,EAER;AAAA,IAAA,IAAA,qBAAA,CAAA;;IACA,MAAMO,WAAW,GAAG,IAAA,CAAKP,OAAzB,CAAA;IACA,IAAKA,CAAAA,OAAL,GAAe,IAAK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,CAAA;;AAC1B,UAAA,CAAA,sBAAA,GAAA,CAAA,oBAAA,GAAA,IAAA,CAAKK,aAAL,EAAmBL,OAAnB,KACE,IAAA,GAAA,KAAA,CAAA,GAAA,sBAAA,CAAA,IAAA,CAAA,oBAAA,EAAA,IAAA,CAAKE,aAAL,CAAmBqB,KADrB,EAEE,IAAA,CAAKrB,aAAL,CAAmBE,SAFrB,EAGE,IAAKF,CAAAA,aAAL,CAAmBmB,OAHrB,CAAA,CAAA;AAKA,UAAA,CAAA,sBAAA,GAAA,CAAA,oBAAA,GAAA,IAAA,CAAKhB,aAAL,EAAmBiB,SAAnB,uEACEnB,SADF,EAEE,KAAKD,aAAL,CAAmBqB,KAFrB,EAGE,IAAA,CAAKrB,aAAL,CAAmBE,SAHrB,EAIE,IAAKF,CAAAA,aAAL,CAAmBmB,OAJrB,CAAA,CAAA;AAMD,SAAA;AACF,OA5BuB;;;MA+BxB,IAAI7C,OAAO,CAACiB,SAAZ,EAAuB;AACrB,QAAA,IAAA,CAAKA,SAAL,CAAe+B,OAAf,CAAwBC,QAAD,IAAc;UACnCA,QAAQ,CAAC,IAAKvB,CAAAA,aAAN,CAAR,CAAA;SADF,CAAA,CAAA;AAGD,OAAA;KAnCH,CAAA,CAAA;AAqCD,GAAA;;AA1KD;;;;"}
|
|
@@ -1389,18 +1389,23 @@
|
|
|
1389
1389
|
class Mutation extends Removable {
|
|
1390
1390
|
constructor(config) {
|
|
1391
1391
|
super();
|
|
1392
|
-
this.
|
|
1393
|
-
...config.options
|
|
1394
|
-
};
|
|
1392
|
+
this.defaultOptions = config.defaultOptions;
|
|
1395
1393
|
this.mutationId = config.mutationId;
|
|
1396
1394
|
this.mutationCache = config.mutationCache;
|
|
1397
1395
|
this.logger = config.logger || defaultLogger;
|
|
1398
1396
|
this.observers = [];
|
|
1399
1397
|
this.state = config.state || getDefaultState();
|
|
1400
|
-
this.
|
|
1398
|
+
this.setOptions(config.options);
|
|
1401
1399
|
this.scheduleGc();
|
|
1402
1400
|
}
|
|
1403
1401
|
|
|
1402
|
+
setOptions(options) {
|
|
1403
|
+
this.options = { ...this.defaultOptions,
|
|
1404
|
+
...options
|
|
1405
|
+
};
|
|
1406
|
+
this.updateCacheTime(this.options.cacheTime);
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1404
1409
|
get meta() {
|
|
1405
1410
|
return this.options.meta;
|
|
1406
1411
|
}
|
|
@@ -2974,6 +2979,8 @@
|
|
|
2974
2979
|
}
|
|
2975
2980
|
|
|
2976
2981
|
setOptions(options) {
|
|
2982
|
+
var _this$currentMutation;
|
|
2983
|
+
|
|
2977
2984
|
const prevOptions = this.options;
|
|
2978
2985
|
this.options = this.client.defaultMutationOptions(options);
|
|
2979
2986
|
|
|
@@ -2984,13 +2991,15 @@
|
|
|
2984
2991
|
observer: this
|
|
2985
2992
|
});
|
|
2986
2993
|
}
|
|
2994
|
+
|
|
2995
|
+
(_this$currentMutation = this.currentMutation) == null ? void 0 : _this$currentMutation.setOptions(this.options);
|
|
2987
2996
|
}
|
|
2988
2997
|
|
|
2989
2998
|
onUnsubscribe() {
|
|
2990
2999
|
if (!this.listeners.length) {
|
|
2991
|
-
var _this$
|
|
3000
|
+
var _this$currentMutation2;
|
|
2992
3001
|
|
|
2993
|
-
(_this$
|
|
3002
|
+
(_this$currentMutation2 = this.currentMutation) == null ? void 0 : _this$currentMutation2.removeObserver(this);
|
|
2994
3003
|
}
|
|
2995
3004
|
}
|
|
2996
3005
|
|