88 isValidTimeout ,
99 noop ,
1010 replaceData ,
11- resolveEnabled ,
12- resolveStaleTime ,
11+ resolveValueOrFunction ,
1312 shallowEqualObjects ,
1413 timeUntilStale ,
1514} from './utils'
@@ -157,8 +156,10 @@ export class QueryObserver<
157156 this . options . enabled !== undefined &&
158157 typeof this . options . enabled !== 'boolean' &&
159158 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'
162163 ) {
163164 throw new Error (
164165 'Expected enabled to be a boolean or a callback that returns a boolean' ,
@@ -201,10 +202,10 @@ export class QueryObserver<
201202 if (
202203 mounted &&
203204 ( 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) )
208209 ) {
209210 this . #updateStaleTimeout( )
210211 }
@@ -215,8 +216,8 @@ export class QueryObserver<
215216 if (
216217 mounted &&
217218 ( 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) ||
220221 nextRefetchInterval !== this . #currentRefetchInterval)
221222 ) {
222223 this . #updateRefetchInterval( nextRefetchInterval )
@@ -344,7 +345,7 @@ export class QueryObserver<
344345
345346 #updateStaleTimeout( ) : void {
346347 this . #clearStaleTimeout( )
347- const staleTime = resolveStaleTime (
348+ const staleTime = resolveValueOrFunction (
348349 this . options . staleTime ,
349350 this . #currentQuery,
350351 )
@@ -368,9 +369,10 @@ export class QueryObserver<
368369
369370 #computeRefetchInterval( ) {
370371 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
374376 )
375377 }
376378
@@ -381,7 +383,8 @@ export class QueryObserver<
381383
382384 if (
383385 isServer ||
384- resolveEnabled ( this . options . enabled , this . #currentQuery) === false ||
386+ resolveValueOrFunction ( this . options . enabled , this . #currentQuery) ===
387+ false ||
385388 ! isValidTimeout ( this . #currentRefetchInterval) ||
386389 this . #currentRefetchInterval === 0
387390 ) {
@@ -489,15 +492,11 @@ export class QueryObserver<
489492 skipSelect = true
490493 } else {
491494 // 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+ )
501500 }
502501
503502 if ( placeholderData !== undefined ) {
@@ -660,9 +659,7 @@ export class QueryObserver<
660659
661660 const { notifyOnChangeProps } = this . options
662661 const notifyOnChangePropsValue =
663- typeof notifyOnChangeProps === 'function'
664- ? notifyOnChangeProps ( )
665- : notifyOnChangeProps
662+ resolveValueOrFunction ( notifyOnChangeProps )
666663
667664 if (
668665 notifyOnChangePropsValue === 'all' ||
@@ -740,7 +737,7 @@ function shouldLoadOnMount(
740737 options : QueryObserverOptions < any , any , any , any > ,
741738) : boolean {
742739 return (
743- resolveEnabled ( options . enabled , query ) !== false &&
740+ resolveValueOrFunction ( options . enabled , query ) !== false &&
744741 query . state . data === undefined &&
745742 ! ( query . state . status === 'error' && options . retryOnMount === false )
746743 )
@@ -764,8 +761,8 @@ function shouldFetchOn(
764761 ( typeof options ) [ 'refetchOnWindowFocus' ] &
765762 ( typeof options ) [ 'refetchOnReconnect' ] ,
766763) {
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 )
769766
770767 return value === 'always' || ( value !== false && isStale ( query , options ) )
771768 }
@@ -780,7 +777,7 @@ function shouldFetchOptionally(
780777) : boolean {
781778 return (
782779 ( query !== prevQuery ||
783- resolveEnabled ( prevOptions . enabled , query ) === false ) &&
780+ resolveValueOrFunction ( prevOptions . enabled , query ) === false ) &&
784781 ( ! options . suspense || query . state . status !== 'error' ) &&
785782 isStale ( query , options )
786783 )
@@ -791,8 +788,8 @@ function isStale(
791788 options : QueryObserverOptions < any , any , any , any , any > ,
792789) : boolean {
793790 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 ) )
796793 )
797794}
798795
0 commit comments