8
8
isValidTimeout ,
9
9
noop ,
10
10
replaceData ,
11
- resolveEnabled ,
12
- resolveStaleTime ,
11
+ resolveValueOrFunction ,
13
12
shallowEqualObjects ,
14
13
timeUntilStale ,
15
14
} from './utils'
@@ -157,8 +156,10 @@ export class QueryObserver<
157
156
this . options . enabled !== undefined &&
158
157
typeof this . options . enabled !== 'boolean' &&
159
158
typeof this . options . enabled !== 'function' &&
160
- typeof resolveEnabled ( this . options . enabled , this . #currentQuery) !==
161
- 'boolean'
159
+ typeof resolveValueOrFunction (
160
+ this . options . enabled ,
161
+ this . #currentQuery,
162
+ ) !== 'boolean'
162
163
) {
163
164
throw new Error (
164
165
'Expected enabled to be a boolean or a callback that returns a boolean' ,
@@ -201,10 +202,10 @@ export class QueryObserver<
201
202
if (
202
203
mounted &&
203
204
( this . #currentQuery !== prevQuery ||
204
- resolveEnabled ( this . options . enabled , this . #currentQuery) !==
205
- resolveEnabled ( prevOptions . enabled , this . #currentQuery) ||
206
- resolveStaleTime ( this . options . staleTime , this . #currentQuery) !==
207
- resolveStaleTime ( prevOptions . staleTime , this . #currentQuery) )
205
+ resolveValueOrFunction ( this . options . enabled , this . #currentQuery) !==
206
+ resolveValueOrFunction ( prevOptions . enabled , this . #currentQuery) ||
207
+ resolveValueOrFunction ( this . options . staleTime , this . #currentQuery) !==
208
+ resolveValueOrFunction ( prevOptions . staleTime , this . #currentQuery) )
208
209
) {
209
210
this . #updateStaleTimeout( )
210
211
}
@@ -215,8 +216,8 @@ export class QueryObserver<
215
216
if (
216
217
mounted &&
217
218
( this . #currentQuery !== prevQuery ||
218
- resolveEnabled ( this . options . enabled , this . #currentQuery) !==
219
- resolveEnabled ( prevOptions . enabled , this . #currentQuery) ||
219
+ resolveValueOrFunction ( this . options . enabled , this . #currentQuery) !==
220
+ resolveValueOrFunction ( prevOptions . enabled , this . #currentQuery) ||
220
221
nextRefetchInterval !== this . #currentRefetchInterval)
221
222
) {
222
223
this . #updateRefetchInterval( nextRefetchInterval )
@@ -344,7 +345,7 @@ export class QueryObserver<
344
345
345
346
#updateStaleTimeout( ) : void {
346
347
this . #clearStaleTimeout( )
347
- const staleTime = resolveStaleTime (
348
+ const staleTime = resolveValueOrFunction (
348
349
this . options . staleTime ,
349
350
this . #currentQuery,
350
351
)
@@ -368,9 +369,10 @@ export class QueryObserver<
368
369
369
370
#computeRefetchInterval( ) {
370
371
return (
371
- ( typeof this . options . refetchInterval === 'function'
372
- ? this . options . refetchInterval ( this . #currentQuery)
373
- : this . options . refetchInterval ) ?? false
372
+ resolveValueOrFunction (
373
+ this . options . refetchInterval ,
374
+ this . #currentQuery,
375
+ ) ?? false
374
376
)
375
377
}
376
378
@@ -381,7 +383,8 @@ export class QueryObserver<
381
383
382
384
if (
383
385
isServer ||
384
- resolveEnabled ( this . options . enabled , this . #currentQuery) === false ||
386
+ resolveValueOrFunction ( this . options . enabled , this . #currentQuery) ===
387
+ false ||
385
388
! isValidTimeout ( this . #currentRefetchInterval) ||
386
389
this . #currentRefetchInterval === 0
387
390
) {
@@ -489,15 +492,11 @@ export class QueryObserver<
489
492
skipSelect = true
490
493
} else {
491
494
// compute placeholderData
492
- placeholderData =
493
- typeof options . placeholderData === 'function'
494
- ? (
495
- options . placeholderData as unknown as PlaceholderDataFunction < TQueryData >
496
- ) (
497
- this . #lastQueryWithDefinedData?. state . data ,
498
- this . #lastQueryWithDefinedData as any ,
499
- )
500
- : options . placeholderData
495
+ placeholderData = resolveValueOrFunction (
496
+ options . placeholderData ,
497
+ this . #lastQueryWithDefinedData?. state . data ,
498
+ this . #lastQueryWithDefinedData as any ,
499
+ )
501
500
}
502
501
503
502
if ( placeholderData !== undefined ) {
@@ -660,9 +659,7 @@ export class QueryObserver<
660
659
661
660
const { notifyOnChangeProps } = this . options
662
661
const notifyOnChangePropsValue =
663
- typeof notifyOnChangeProps === 'function'
664
- ? notifyOnChangeProps ( )
665
- : notifyOnChangeProps
662
+ resolveValueOrFunction ( notifyOnChangeProps )
666
663
667
664
if (
668
665
notifyOnChangePropsValue === 'all' ||
@@ -740,7 +737,7 @@ function shouldLoadOnMount(
740
737
options : QueryObserverOptions < any , any , any , any > ,
741
738
) : boolean {
742
739
return (
743
- resolveEnabled ( options . enabled , query ) !== false &&
740
+ resolveValueOrFunction ( options . enabled , query ) !== false &&
744
741
query . state . data === undefined &&
745
742
! ( query . state . status === 'error' && options . retryOnMount === false )
746
743
)
@@ -764,8 +761,8 @@ function shouldFetchOn(
764
761
( typeof options ) [ 'refetchOnWindowFocus' ] &
765
762
( typeof options ) [ 'refetchOnReconnect' ] ,
766
763
) {
767
- if ( resolveEnabled ( options . enabled , query ) !== false ) {
768
- const value = typeof field === 'function' ? field ( query ) : field
764
+ if ( resolveValueOrFunction ( options . enabled , query ) !== false ) {
765
+ const value = resolveValueOrFunction ( field , query )
769
766
770
767
return value === 'always' || ( value !== false && isStale ( query , options ) )
771
768
}
@@ -780,7 +777,7 @@ function shouldFetchOptionally(
780
777
) : boolean {
781
778
return (
782
779
( query !== prevQuery ||
783
- resolveEnabled ( prevOptions . enabled , query ) === false ) &&
780
+ resolveValueOrFunction ( prevOptions . enabled , query ) === false ) &&
784
781
( ! options . suspense || query . state . status !== 'error' ) &&
785
782
isStale ( query , options )
786
783
)
@@ -791,8 +788,8 @@ function isStale(
791
788
options : QueryObserverOptions < any , any , any , any , any > ,
792
789
) : boolean {
793
790
return (
794
- resolveEnabled ( options . enabled , query ) !== false &&
795
- query . isStaleByTime ( resolveStaleTime ( options . staleTime , query ) )
791
+ resolveValueOrFunction ( options . enabled , query ) !== false &&
792
+ query . isStaleByTime ( resolveValueOrFunction ( options . staleTime , query ) )
796
793
)
797
794
}
798
795
0 commit comments