@@ -42,6 +42,18 @@ export interface MobxQueryDynamicOptions<
4242 enabled ?: boolean ;
4343}
4444
45+ export interface MobxQueryOptions <
46+ TData ,
47+ TError = DefaultError ,
48+ TQueryKey extends QueryKey = QueryKey ,
49+ > extends DefaultedQueryObserverOptions <
50+ TData ,
51+ TError ,
52+ TData ,
53+ TData ,
54+ TQueryKey
55+ > { }
56+
4557export interface MobxQueryConfig <
4658 TData ,
4759 TError = DefaultError ,
@@ -103,13 +115,7 @@ export class MobxQuery<
103115
104116 private _result : QueryObserverResult < TData , TError > ;
105117
106- options : DefaultedQueryObserverOptions <
107- TData ,
108- TError ,
109- TData ,
110- TData ,
111- TQueryKey
112- > ;
118+ options : MobxQueryOptions < TData , TError , TQueryKey > ;
113119 queryObserver : QueryObserver < TData , TError , TData , TData , TQueryKey > ;
114120
115121 isResultRequsted : boolean ;
@@ -140,7 +146,7 @@ export class MobxQuery<
140146 disposer . add ( ( ) => this . dispose ( ) ) ;
141147 }
142148
143- observable . ref ( this , '_result' ) ;
149+ observable . deep ( this , '_result' ) ;
144150 observable . ref ( this , 'isResultRequsted' ) ;
145151 action . bound ( this , 'setData' ) ;
146152 action . bound ( this , 'update' ) ;
@@ -188,15 +194,10 @@ export class MobxQuery<
188194
189195 this . queryObserver = new QueryObserver ( queryClient , this . options ) ;
190196
191- this . updateResult ( ) ;
197+ this . updateResult ( this . queryObserver . getOptimisticResult ( this . options ) ) ;
192198
193199 const subscription = this . queryObserver . subscribe ( this . updateResult ) ;
194200
195- this . abortController . signal . addEventListener ( 'abort' , ( ) => {
196- subscription ( ) ;
197- this . queryObserver . destroy ( ) ;
198- } ) ;
199-
200201 if ( getDynamicOptions ) {
201202 reaction (
202203 ( ) =>
@@ -239,11 +240,14 @@ export class MobxQuery<
239240 this . onError ( onError ) ;
240241 }
241242
242- if ( resetOnDispose ) {
243- this . abortController . signal . addEventListener ( 'abort' , ( ) => {
243+ this . abortController . signal . addEventListener ( 'abort' , ( ) => {
244+ subscription ( ) ;
245+ this . queryObserver . destroy ( ) ;
246+
247+ if ( resetOnDispose ) {
244248 this . reset ( ) ;
245- } ) ;
246- }
249+ }
250+ } ) ;
247251
248252 onInit ?.( this ) ;
249253 }
@@ -264,7 +268,7 @@ export class MobxQuery<
264268 updater : Updater < NoInfer < TData > | undefined , NoInfer < TData > | undefined > ,
265269 options ?: SetDataOptions ,
266270 ) {
267- this . queryClient . setQueryData < TData > (
271+ return this . queryClient . setQueryData < TData > (
268272 this . options . queryKey ,
269273 updater ,
270274 options ,
@@ -295,21 +299,20 @@ export class MobxQuery<
295299 /**
296300 * Modify this result so it matches the tanstack query result.
297301 */
298- private updateResult ( ) {
299- const nextResult = this . queryObserver . getOptimisticResult ( this . options ) ;
300- this . _result = nextResult || { } ;
302+ private updateResult ( result : QueryObserverResult < TData , TError > ) {
303+ this . _result = result ;
301304 }
302305
303306 async reset ( params ?: MobxQueryResetParams ) {
304- await this . queryClient . resetQueries ( {
307+ return await this . queryClient . resetQueries ( {
305308 queryKey : this . options . queryKey ,
306309 exact : true ,
307310 ...params ,
308311 } ) ;
309312 }
310313
311314 async invalidate ( params ?: MobxQueryInvalidateParams ) {
312- await this . queryClient . invalidateQueries ( {
315+ return await this . queryClient . invalidateQueries ( {
313316 exact : true ,
314317 queryKey : this . options . queryKey ,
315318 ...params ,
0 commit comments