33 MutationObserver ,
44 MutationObserverOptions ,
55 MutationObserverResult ,
6- MutationOptions ,
76 MutationObserverBaseResult ,
87 MutationStatus ,
8+ MutateOptions ,
99} from '@tanstack/query-core' ;
1010import { action , makeObservable , observable } from 'mobx' ;
1111import { lazyObserve } from 'yummies/mobx' ;
@@ -42,37 +42,51 @@ export class Mutation<
4242 TData = unknown ,
4343 TVariables = void ,
4444 TError = DefaultError ,
45- TContext = unknown ,
45+ TOnMutateResult = unknown ,
4646 >
4747 extends Destroyable
4848 implements
4949 Disposable ,
5050 Pick <
51- MutationObserverBaseResult < TData , TError , TVariables , TContext > ,
51+ MutationObserverBaseResult < TData , TError , TVariables , TOnMutateResult > ,
5252 ( typeof originalMutationProperties ) [ number ]
5353 >
5454{
5555 protected queryClient : AnyQueryClient ;
5656
57- mutationOptions : MutationObserverOptions < TData , TError , TVariables , TContext > ;
58- mutationObserver : MutationObserver < TData , TError , TVariables , TContext > ;
57+ mutationOptions : MutationObserverOptions <
58+ TData ,
59+ TError ,
60+ TVariables ,
61+ TOnMutateResult
62+ > ;
63+ mutationObserver : MutationObserver <
64+ TData ,
65+ TError ,
66+ TVariables ,
67+ TOnMutateResult
68+ > ;
5969
60- result : MutationObserverResult < TData , TError , TVariables , TContext > ;
70+ result : MutationObserverResult < TData , TError , TVariables , TOnMutateResult > ;
6171
6272 protected features : MutationFeatures ;
6373
6474 protected settledListeners : MutationSettledListener <
6575 TData ,
6676 TError ,
6777 TVariables ,
68- TContext
78+ TOnMutateResult
6979 > [ ] ;
7080 protected errorListeners : MutationErrorListener <
7181 TError ,
7282 TVariables ,
73- TContext
83+ TOnMutateResult
84+ > [ ] ;
85+ protected doneListeners : MutationDoneListener <
86+ TData ,
87+ TVariables ,
88+ TOnMutateResult
7489 > [ ] ;
75- protected doneListeners : MutationDoneListener < TData , TVariables , TContext > [ ] ;
7690
7791 private _observerSubscription ?: VoidFunction ;
7892 private hooks ?: QueryClientHooks ;
@@ -120,14 +134,19 @@ export class Mutation<
120134 */
121135 status ! : MutationStatus ;
122136
123- context ! : TContext | undefined ;
137+ context ! : TOnMutateResult | undefined ;
124138 failureCount ! : number ;
125139 failureReason ! : TError | null ;
126140 isPaused ! : boolean ;
127141 submittedAt ! : number ;
128142
129143 constructor (
130- protected config : MutationConfig < TData , TVariables , TError , TContext > ,
144+ protected config : MutationConfig <
145+ TData ,
146+ TVariables ,
147+ TError ,
148+ TOnMutateResult
149+ > ,
131150 ) {
132151 super ( config . abortSignal ) ;
133152
@@ -166,15 +185,9 @@ export class Mutation<
166185 this . start = this . start . bind ( this ) ;
167186
168187 originalMutationProperties . forEach ( ( property ) => {
169- if ( property === 'error' && this . features . transformError ) {
170- Object . defineProperty ( this , property , {
171- get : ( ) => this . features . transformError ! ( this . result [ property ] ) ,
172- } ) ;
173- } else {
174- Object . defineProperty ( this , property , {
175- get : ( ) => this . result [ property ] ,
176- } ) ;
177- }
188+ Object . defineProperty ( this , property , {
189+ get : ( ) => this . result [ property ] ,
190+ } ) ;
178191 } ) ;
179192
180193 makeObservable ( this ) ;
@@ -185,7 +198,7 @@ export class Mutation<
185198 TData ,
186199 TError ,
187200 TVariables ,
188- TContext
201+ TOnMutateResult
189202 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
190203 // @ts -expect-error
191204 > ( queryClient , {
@@ -273,7 +286,7 @@ export class Mutation<
273286
274287 async mutate (
275288 variables : TVariables ,
276- options ?: MutationOptions < TData , TError , TVariables , TContext > ,
289+ options ?: MutateOptions < TData , TError , TVariables , TOnMutateResult > ,
277290 ) {
278291 if ( this . features . lazy ) {
279292 let error : any ;
@@ -299,7 +312,7 @@ export class Mutation<
299312
300313 async start (
301314 variables : TVariables ,
302- options ?: MutationOptions < TData , TError , TVariables , TContext > ,
315+ options ?: MutateOptions < TData , TError , TVariables , TOnMutateResult > ,
303316 ) {
304317 return await this . mutate ( variables , options ) ;
305318 }
@@ -308,10 +321,14 @@ export class Mutation<
308321 * Modify this result so it matches the tanstack query result.
309322 */
310323 private updateResult (
311- result : MutationObserverResult < TData , TError , TVariables , TContext > ,
324+ result : MutationObserverResult < TData , TError , TVariables , TOnMutateResult > ,
312325 ) {
313326 this . result = result || { } ;
314327
328+ if ( this . features . transformError && this . result . error ) {
329+ this . result . error = this . features . transformError ( this . result . error ) ;
330+ }
331+
315332 if ( result . isSuccess && ! result . error ) {
316333 this . doneListeners . forEach ( ( fn ) =>
317334 fn ( result . data ! , result . variables ! , result . context ) ,
@@ -330,16 +347,25 @@ export class Mutation<
330347 }
331348
332349 onSettled (
333- listener : MutationSettledListener < TData , TError , TVariables , TContext > ,
350+ listener : MutationSettledListener <
351+ TData ,
352+ TError ,
353+ TVariables ,
354+ TOnMutateResult
355+ > ,
334356 ) : void {
335357 this . settledListeners . push ( listener ) ;
336358 }
337359
338- onDone ( listener : MutationDoneListener < TData , TVariables , TContext > ) : void {
360+ onDone (
361+ listener : MutationDoneListener < TData , TVariables , TOnMutateResult > ,
362+ ) : void {
339363 this . doneListeners . push ( listener ) ;
340364 }
341365
342- onError ( listener : MutationErrorListener < TError , TVariables , TContext > ) : void {
366+ onError (
367+ listener : MutationErrorListener < TError , TVariables , TOnMutateResult > ,
368+ ) : void {
343369 this . errorListeners . push ( listener ) ;
344370 }
345371
@@ -370,5 +396,5 @@ export class MobxMutation<
370396 TData = unknown ,
371397 TVariables = void ,
372398 TError = DefaultError ,
373- TContext = unknown ,
374- > extends Mutation < TData , TVariables , TError , TContext > { }
399+ TOnMutateResult = unknown ,
400+ > extends Mutation < TData , TVariables , TError , TOnMutateResult > { }
0 commit comments