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,19 @@ export class QueryObserver<TResult, TError> {
163158
164159 this . clearStaleTimeout ( )
165160
166- const staleTime = this . config . staleTime || 0
167- const { isStale, updatedAt } = this . currentResult
168-
169- if ( isStale || staleTime === Infinity ) {
161+ if ( this . currentResult . isStale || ! isValidTimeout ( this . config . staleTime ) ) {
170162 return
171163 }
172164
173- const timeElapsed = Date . now ( ) - updatedAt
174- const timeUntilStale = staleTime - timeElapsed + 1
165+ const timeElapsed = Date . now ( ) - this . currentResult . updatedAt
166+ const timeUntilStale = this . config . staleTime - timeElapsed + 1
175167 const timeout = Math . max ( timeUntilStale , 0 )
176168
177169 this . staleTimeoutId = setTimeout ( ( ) => {
178- this . updateIsStale ( )
170+ if ( ! this . currentResult . isStale ) {
171+ this . currentResult = { ...this . currentResult , isStale : true }
172+ this . notify ( )
173+ }
179174 } , timeout )
180175 }
181176
@@ -186,12 +181,7 @@ export class QueryObserver<TResult, TError> {
186181
187182 this . clearRefetchInterval ( )
188183
189- if (
190- ! this . config . enabled ||
191- ! this . config . refetchInterval ||
192- this . config . refetchInterval < 0 ||
193- this . config . refetchInterval === Infinity
194- ) {
184+ if ( ! this . config . enabled || ! isValidTimeout ( this . config . refetchInterval ) ) {
195185 return
196186 }
197187
@@ -309,6 +299,8 @@ export class QueryObserver<TResult, TError> {
309299 }
310300
311301 onQueryUpdate ( action : Action < TResult , TError > ) : void {
302+ const { type } = action
303+
312304 // Store current result and get new result
313305 const prevResult = this . currentResult
314306 this . updateResult ( )
@@ -317,11 +309,11 @@ export class QueryObserver<TResult, TError> {
317309
318310 // We need to check the action because the state could have
319311 // transitioned from success to success in case of `setQueryData`.
320- if ( action . type === 'Success' && currentResult . isSuccess ) {
312+ if ( type === 2 ) {
321313 config . onSuccess ?.( currentResult . data ! )
322314 config . onSettled ?.( currentResult . data ! , null )
323315 this . updateTimers ( )
324- } else if ( action . type === 'Error' && currentResult . isError ) {
316+ } else if ( type === 3 ) {
325317 config . onError ?.( currentResult . error ! )
326318 config . onSettled ?.( undefined , currentResult . error ! )
327319 this . updateTimers ( )
0 commit comments