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 }
@@ -144,14 +147,6 @@ export class QueryObserver<TResult, TError> {
144147 }
145148 }
146149
147- private updateIsStale ( ) : void {
148- const isStale = this . currentQuery . isStaleByTime ( this . config . staleTime )
149- if ( isStale !== this . currentResult . isStale ) {
150- this . updateResult ( )
151- this . notify ( )
152- }
153- }
154-
155150 private notify ( ) : void {
156151 this . updateListener ?.( this . currentResult )
157152 }
@@ -163,19 +158,21 @@ export class QueryObserver<TResult, TError> {
163158
164159 this . clearStaleTimeout ( )
165160
166- const staleTime = this . config . staleTime || 0
167161 const { isStale, updatedAt } = this . currentResult
168162
169- if ( isStale || staleTime === Infinity ) {
163+ if ( isStale || ! isValidTimeout ( this . config . staleTime ) ) {
170164 return
171165 }
172166
173167 const timeElapsed = Date . now ( ) - updatedAt
174- const timeUntilStale = staleTime - timeElapsed + 1
168+ const timeUntilStale = this . config . staleTime - timeElapsed + 1
175169 const timeout = Math . max ( timeUntilStale , 0 )
176170
177171 this . staleTimeoutId = setTimeout ( ( ) => {
178- this . updateIsStale ( )
172+ if ( ! this . currentResult . isStale ) {
173+ this . currentResult = { ...this . currentResult , isStale : true }
174+ this . notify ( )
175+ }
179176 } , timeout )
180177 }
181178
@@ -186,12 +183,7 @@ export class QueryObserver<TResult, TError> {
186183
187184 this . clearRefetchInterval ( )
188185
189- if (
190- ! this . config . enabled ||
191- ! this . config . refetchInterval ||
192- this . config . refetchInterval < 0 ||
193- this . config . refetchInterval === Infinity
194- ) {
186+ if ( ! this . config . enabled || ! isValidTimeout ( this . config . refetchInterval ) ) {
195187 return
196188 }
197189
@@ -309,6 +301,8 @@ export class QueryObserver<TResult, TError> {
309301 }
310302
311303 onQueryUpdate ( action : Action < TResult , TError > ) : void {
304+ const { type } = action
305+
312306 // Store current result and get new result
313307 const prevResult = this . currentResult
314308 this . updateResult ( )
@@ -317,11 +311,11 @@ export class QueryObserver<TResult, TError> {
317311
318312 // We need to check the action because the state could have
319313 // transitioned from success to success in case of `setQueryData`.
320- if ( action . type === 'Success' && currentResult . isSuccess ) {
314+ if ( type === 2 ) {
321315 config . onSuccess ?.( currentResult . data ! )
322316 config . onSettled ?.( currentResult . data ! , null )
323317 this . updateTimers ( )
324- } else if ( action . type === 'Error' && currentResult . isError ) {
318+ } else if ( type === 3 ) {
325319 config . onError ?.( currentResult . error ! )
326320 config . onSettled ?.( undefined , currentResult . error ! )
327321 this . updateTimers ( )
0 commit comments