@@ -44,7 +44,6 @@ export interface QueryState<TResult, TError> {
44
44
isFetchingMore : IsFetchingMoreValue
45
45
isIdle : boolean
46
46
isLoading : boolean
47
- isStale : boolean
48
47
isSuccess : boolean
49
48
status : QueryStatus
50
49
throwInErrorBoundary ?: boolean
@@ -66,7 +65,6 @@ export interface RefetchOptions {
66
65
67
66
export enum ActionType {
68
67
Failed = 'Failed' ,
69
- MarkStale = 'MarkStale' ,
70
68
Fetch = 'Fetch' ,
71
69
Success = 'Success' ,
72
70
Error = 'Error' ,
@@ -76,10 +74,6 @@ interface FailedAction {
76
74
type : ActionType . Failed
77
75
}
78
76
79
- interface MarkStaleAction {
80
- type : ActionType . MarkStale
81
- }
82
-
83
77
interface FetchAction {
84
78
type : ActionType . Fetch
85
79
isFetchingMore ?: IsFetchingMoreValue
@@ -89,7 +83,6 @@ interface SuccessAction<TResult> {
89
83
type : ActionType . Success
90
84
data : TResult | undefined
91
85
canFetchMore ?: boolean
92
- isStale : boolean
93
86
}
94
87
95
88
interface ErrorAction < TError > {
@@ -101,7 +94,6 @@ export type Action<TResult, TError> =
101
94
| ErrorAction < TError >
102
95
| FailedAction
103
96
| FetchAction
104
- | MarkStaleAction
105
97
| SuccessAction < TResult >
106
98
107
99
// CLASS
@@ -136,7 +128,6 @@ export class Query<TResult, TError> {
136
128
137
129
activateTimeouts ( ) : void {
138
130
this . enableTimeouts = true
139
- this . rescheduleStaleTimeout ( )
140
131
this . rescheduleGarbageCollection ( )
141
132
}
142
133
@@ -150,45 +141,6 @@ export class Query<TResult, TError> {
150
141
this . notifyGlobalListeners ( this )
151
142
}
152
143
153
- private rescheduleStaleTimeout ( ) : void {
154
- if ( isServer ) {
155
- return
156
- }
157
-
158
- this . clearStaleTimeout ( )
159
-
160
- if (
161
- ! this . enableTimeouts ||
162
- this . state . isStale ||
163
- this . state . status !== QueryStatus . Success ||
164
- this . config . staleTime === Infinity
165
- ) {
166
- return
167
- }
168
-
169
- const staleTime = this . config . staleTime || 0
170
- let timeout = staleTime
171
- if ( this . state . updatedAt ) {
172
- const timeElapsed = Date . now ( ) - this . state . updatedAt
173
- const timeUntilStale = staleTime - timeElapsed
174
- timeout = Math . max ( timeUntilStale , 0 )
175
- }
176
-
177
- this . staleTimeout = setTimeout ( ( ) => {
178
- this . invalidate ( )
179
- } , timeout )
180
- }
181
-
182
- invalidate ( ) : void {
183
- this . clearStaleTimeout ( )
184
-
185
- if ( this . state . isStale ) {
186
- return
187
- }
188
-
189
- this . dispatch ( { type : ActionType . MarkStale } )
190
- }
191
-
192
144
private rescheduleGarbageCollection ( ) : void {
193
145
if ( isServer ) {
194
146
return
@@ -230,7 +182,7 @@ export class Query<TResult, TError> {
230
182
231
183
private clearTimersObservers ( ) : void {
232
184
this . observers . forEach ( observer => {
233
- observer . clearRefetchInterval ( )
185
+ observer . clearTimers ( )
234
186
} )
235
187
}
236
188
@@ -264,20 +216,15 @@ export class Query<TResult, TError> {
264
216
data = prevData
265
217
}
266
218
267
- const isStale = this . config . staleTime === 0
268
-
269
219
// Try to determine if more data can be fetched
270
220
const canFetchMore = hasMorePages ( this . config , data )
271
221
272
222
// Set data and mark it as cached
273
223
this . dispatch ( {
274
224
type : ActionType . Success ,
275
225
data,
276
- isStale,
277
226
canFetchMore,
278
227
} )
279
-
280
- this . rescheduleStaleTimeout ( )
281
228
}
282
229
283
230
clear ( ) : void {
@@ -293,12 +240,22 @@ export class Query<TResult, TError> {
293
240
return this . observers . some ( observer => observer . config . enabled )
294
241
}
295
242
243
+ isStale ( ) : boolean {
244
+ return this . observers . some ( observer => observer . isStale ( ) )
245
+ }
246
+
247
+ isStaleByTime ( staleTime = 0 ) : boolean {
248
+ const { isSuccess, updatedAt } = this . state
249
+ return ! isSuccess || updatedAt + staleTime <= Date . now ( )
250
+ }
251
+
296
252
onWindowFocus ( ) : void {
297
253
if (
298
- this . state . isStale &&
299
254
this . observers . some (
300
255
observer =>
301
- observer . config . enabled && observer . config . refetchOnWindowFocus
256
+ observer . isStale ( ) &&
257
+ observer . config . enabled &&
258
+ observer . config . refetchOnWindowFocus
302
259
)
303
260
) {
304
261
this . fetch ( )
@@ -308,10 +265,11 @@ export class Query<TResult, TError> {
308
265
309
266
onOnline ( ) : void {
310
267
if (
311
- this . state . isStale &&
312
268
this . observers . some (
313
269
observer =>
314
- observer . config . enabled && observer . config . refetchOnReconnect
270
+ observer . isStale ( ) &&
271
+ observer . config . enabled &&
272
+ observer . config . refetchOnReconnect
315
273
)
316
274
) {
317
275
this . fetch ( )
@@ -628,12 +586,6 @@ function getDefaultState<TResult, TError>(
628
586
629
587
const hasInitialData = typeof initialData !== 'undefined'
630
588
631
- const isStale =
632
- ! config . enabled ||
633
- ( typeof config . initialStale === 'function'
634
- ? config . initialStale ( )
635
- : config . initialStale ?? ! hasInitialData )
636
-
637
589
const initialStatus = hasInitialData
638
590
? QueryStatus . Success
639
591
: config . enabled
@@ -647,9 +599,8 @@ function getDefaultState<TResult, TError>(
647
599
isFetching : initialStatus === QueryStatus . Loading ,
648
600
isFetchingMore : false ,
649
601
failureCount : 0 ,
650
- isStale,
651
602
data : initialData ,
652
- updatedAt : hasInitialData ? Date . now ( ) : 0 ,
603
+ updatedAt : Date . now ( ) ,
653
604
canFetchMore : hasMorePages ( config , initialData ) ,
654
605
}
655
606
}
@@ -664,11 +615,6 @@ export function queryReducer<TResult, TError>(
664
615
...state ,
665
616
failureCount : state . failureCount + 1 ,
666
617
}
667
- case ActionType . MarkStale :
668
- return {
669
- ...state ,
670
- isStale : true ,
671
- }
672
618
case ActionType . Fetch :
673
619
const status =
674
620
typeof state . data !== 'undefined'
@@ -687,7 +633,6 @@ export function queryReducer<TResult, TError>(
687
633
...getStatusProps ( QueryStatus . Success ) ,
688
634
data : action . data ,
689
635
error : null ,
690
- isStale : action . isStale ,
691
636
isFetched : true ,
692
637
isFetching : false ,
693
638
isFetchingMore : false ,
@@ -703,7 +648,6 @@ export function queryReducer<TResult, TError>(
703
648
isFetched : true ,
704
649
isFetching : false ,
705
650
isFetchingMore : false ,
706
- isStale : true ,
707
651
failureCount : state . failureCount + 1 ,
708
652
throwInErrorBoundary : true ,
709
653
}
0 commit comments