1
- import equal from '@wry/equality' ;
1
+ import { equal } from '@wry/equality' ;
2
2
import { makeObservable , observable , action } from 'mobx' ;
3
3
import {
4
4
addDisposer ,
@@ -15,7 +15,7 @@ import {
15
15
recordPatches ,
16
16
unprotect ,
17
17
} from 'mobx-state-tree' ;
18
- import { MutationReturnType , QueryReturnType } from './create' ;
18
+ import { MutationReturnType } from './create' ;
19
19
import { merge } from './merge' ;
20
20
import { QueryClient , EndpointType } from './QueryClient' ;
21
21
@@ -35,7 +35,6 @@ type QueryHookOptions = {
35
35
36
36
type NotifyOptions = {
37
37
onMutate ?: boolean ;
38
- onQueryMore ?: boolean ;
39
38
} ;
40
39
41
40
type OnResponseOptions = {
@@ -114,7 +113,7 @@ function subscribe(target: any, options: any) {
114
113
115
114
export function onMutate < T extends Instance < MutationReturnType > > (
116
115
target : T ,
117
- callback : ( data : T [ 'data' ] , self : T ) => void
116
+ callback : ( data : T [ 'data' ] , self : T ) => void ,
118
117
) {
119
118
subscribe ( target , {
120
119
onMutate : ( data : any , self : any ) => {
@@ -126,20 +125,6 @@ export function onMutate<T extends Instance<MutationReturnType>>(
126
125
} ) ;
127
126
}
128
127
129
- export function onQueryMore < T extends Instance < QueryReturnType > > (
130
- target : T ,
131
- callback : ( data : T [ 'data' ] , self : T ) => void
132
- ) {
133
- subscribe ( target , {
134
- onQueryMore : ( data : any , self : any ) => {
135
- const root = getRoot ( self ) ;
136
- unprotect ( root ) ;
137
- callback ( data , self ) ;
138
- protect ( root ) ;
139
- } ,
140
- } ) ;
141
- }
142
-
143
128
export class MstQueryHandler {
144
129
isLoading = false ;
145
130
isRefetching = false ;
@@ -148,9 +133,9 @@ export class MstQueryHandler {
148
133
error : any = null ;
149
134
queryObservers = [ ] as any [ ] ;
150
135
151
- result : any ;
152
136
options : {
153
137
endpoint : EndpointType ;
138
+ onQueryMore ?: ( options : any ) => void ;
154
139
meta ?: { [ key : string ] : any } ;
155
140
} ;
156
141
@@ -184,7 +169,6 @@ export class MstQueryHandler {
184
169
error : observable ,
185
170
hydrate : action . bound ,
186
171
setData : action . bound ,
187
- setResult : action . bound ,
188
172
setError : action . bound ,
189
173
run : action . bound ,
190
174
query : action . bound ,
@@ -224,9 +208,10 @@ export class MstQueryHandler {
224
208
meta : options . meta ?? { } ,
225
209
signal : this . abortController . signal ,
226
210
setData : this . model . setData ,
211
+ query : this . model ,
227
212
} ;
228
213
229
- return endpoint ( opts , this . model ) . then ( ( result : any ) => {
214
+ return endpoint ( opts ) . then ( ( result : any ) => {
230
215
if ( abortController ?. signal . aborted || this . isDisposed ) {
231
216
throw new DisposedError ( ) ;
232
217
}
@@ -285,7 +270,7 @@ export class MstQueryHandler {
285
270
query ( options : any = { } ) : Promise < ( ) => any > {
286
271
return this . run ( options ) . then (
287
272
( result ) => this . onSuccess ( result ) ,
288
- ( err ) => this . onError ( err )
273
+ ( err ) => this . onError ( err ) ,
289
274
) ;
290
275
}
291
276
@@ -299,7 +284,7 @@ export class MstQueryHandler {
299
284
}
300
285
return this . run ( options ) . then (
301
286
( result ) => this . onSuccess ( result , { updateRecorder } ) ,
302
- ( err ) => this . onError ( err , { updateRecorder } )
287
+ ( err ) => this . onError ( err , { updateRecorder } ) ,
303
288
) ;
304
289
}
305
290
@@ -312,7 +297,7 @@ export class MstQueryHandler {
312
297
313
298
return this . run ( options ) . then (
314
299
( result ) => this . onSuccess ( result , { shouldUpdate : false } ) ,
315
- ( err ) => this . onError ( err , { shouldUpdate : false } )
300
+ ( err ) => this . onError ( err , { shouldUpdate : false } ) ,
316
301
) ;
317
302
}
318
303
@@ -325,7 +310,7 @@ export class MstQueryHandler {
325
310
326
311
return this . run ( options ) . then (
327
312
( result ) => this . onSuccess ( result ) ,
328
- ( err ) => this . onError ( err )
313
+ ( err ) => this . onError ( err ) ,
329
314
) ;
330
315
}
331
316
@@ -352,8 +337,6 @@ export class MstQueryHandler {
352
337
this . markedAsStale = false ;
353
338
}
354
339
355
- this . setResult ( result ) ;
356
-
357
340
let data ;
358
341
if ( shouldUpdate ) {
359
342
data = this . setData ( result ) ;
@@ -376,7 +359,12 @@ export class MstQueryHandler {
376
359
377
360
if ( this . isFetchingMore ) {
378
361
this . isFetchingMore = false ;
379
- this . notify ( { onQueryMore : true } , data , this . model ) ;
362
+ this . options . onQueryMore ?.( {
363
+ data,
364
+ pagination : this . model . variables . pagination ,
365
+ request : this . model . variables . request ,
366
+ query : this . model ,
367
+ } ) ;
380
368
}
381
369
382
370
if ( ! this . isFetched ) {
@@ -458,18 +446,10 @@ export class MstQueryHandler {
458
446
}
459
447
}
460
448
461
- setResult ( result : any ) {
462
- this . result = result ;
463
- }
464
-
465
449
setError ( error : any ) {
466
450
this . error = error ;
467
451
}
468
452
469
- setOptions ( options : any ) {
470
- this . options = { ...this . options , ...options } ;
471
- }
472
-
473
453
setVariables ( variables : any ) {
474
454
let request = variables . request ?? EmptyRequest ;
475
455
let pagination = variables . pagination ?? EmptyPagination ;
@@ -509,7 +489,7 @@ export class MstQueryHandler {
509
489
this . model . data = merge (
510
490
data ,
511
491
this . type . properties . data ,
512
- this . queryClient . config . env
492
+ this . queryClient . config . env ,
513
493
) ;
514
494
}
515
495
} ) ;
0 commit comments