1- import { getStatusProps , isServer , isDocumentVisible } from './utils'
1+ import {
2+ getStatusProps ,
3+ isServer ,
4+ isDocumentVisible ,
5+ isValidTimeout ,
6+ } from './utils'
27import type { QueryResult , QueryObserverConfig } from './types'
38import type { Query , Action , FetchMoreOptions , RefetchOptions } from './query'
49import type { QueryCache } from './queryCache'
@@ -90,9 +95,7 @@ export class QueryObserver<TResult, TError> {
9095 // Update refetch interval if needed
9196 if (
9297 config . enabled !== prevConfig . enabled ||
93- config . refetchInterval !== prevConfig . refetchInterval ||
94- config . refetchIntervalInBackground !==
95- prevConfig . refetchIntervalInBackground
98+ config . refetchInterval !== prevConfig . refetchInterval
9699 ) {
97100 this . updateRefetchInterval ( )
98101 }
@@ -163,16 +166,15 @@ export class QueryObserver<TResult, TError> {
163166
164167 this . clearStaleTimeout ( )
165168
166- const staleTime = this . config . staleTime || 0
167169 const { isStale, updatedAt } = this . currentResult
168170
169- if ( isStale || staleTime === Infinity ) {
171+ if ( isStale || ! isValidTimeout ( this . config . staleTime ) ) {
170172 return
171173 }
172174
173175 const timeElapsed = Date . now ( ) - updatedAt
174- const timeUntilStale = staleTime - timeElapsed
175- const timeout = Math . max ( timeUntilStale , 0 )
176+ const timeUntilStale = this . config . staleTime - timeElapsed
177+ const timeout = Math . max ( timeUntilStale , 1 )
176178
177179 this . staleTimeoutId = setTimeout ( ( ) => {
178180 this . updateIsStale ( )
@@ -186,12 +188,7 @@ export class QueryObserver<TResult, TError> {
186188
187189 this . clearRefetchInterval ( )
188190
189- if (
190- ! this . config . enabled ||
191- ! this . config . refetchInterval ||
192- this . config . refetchInterval < 0 ||
193- this . config . refetchInterval === Infinity
194- ) {
191+ if ( ! this . config . enabled || ! isValidTimeout ( this . config . refetchInterval ) ) {
195192 return
196193 }
197194
@@ -309,6 +306,8 @@ export class QueryObserver<TResult, TError> {
309306 }
310307
311308 onQueryUpdate ( action : Action < TResult , TError > ) : void {
309+ const { type } = action
310+
312311 // Store current result and get new result
313312 const prevResult = this . currentResult
314313 this . updateResult ( )
@@ -317,11 +316,11 @@ export class QueryObserver<TResult, TError> {
317316
318317 // We need to check the action because the state could have
319318 // transitioned from success to success in case of `setQueryData`.
320- if ( action . type === 'Success' && currentResult . isSuccess ) {
319+ if ( type === 2 ) {
321320 config . onSuccess ?.( currentResult . data ! )
322321 config . onSettled ?.( currentResult . data ! , null )
323322 this . updateTimers ( )
324- } else if ( action . type === 'Error' && currentResult . isError ) {
323+ } else if ( type === 3 ) {
325324 config . onError ?.( currentResult . error ! )
326325 config . onSettled ?.( undefined , currentResult . error ! )
327326 this . updateTimers ( )
0 commit comments