9
9
isDocumentVisible ,
10
10
isOnline ,
11
11
isServer ,
12
+ isValidTimeout ,
12
13
noop ,
13
14
replaceEqualDeep ,
14
15
sleep ,
@@ -26,14 +27,6 @@ import { QueryObserver, UpdateListener } from './queryObserver'
26
27
27
28
// TYPES
28
29
29
- interface QueryInitConfig < TResult , TError > {
30
- queryCache : QueryCache
31
- queryKey : ArrayQueryKey
32
- queryHash : string
33
- config : QueryConfig < TResult , TError >
34
- notifyGlobalListeners : ( query : Query < TResult , TError > ) => void
35
- }
36
-
37
30
export interface QueryState < TResult , TError > {
38
31
canFetchMore ?: boolean
39
32
data ?: TResult
@@ -65,11 +58,11 @@ export interface RefetchOptions {
65
58
throwOnError ?: boolean
66
59
}
67
60
68
- export enum ActionType {
69
- Failed = 'Failed' ,
70
- Fetch = 'Fetch' ,
71
- Success = 'Success' ,
72
- Error = 'Error' ,
61
+ const enum ActionType {
62
+ Failed ,
63
+ Fetch ,
64
+ Success ,
65
+ Error ,
73
66
}
74
67
75
68
interface FailedAction {
@@ -114,17 +107,19 @@ export class Query<TResult, TError> {
114
107
private cancelFetch ?: ( ) => void
115
108
private continueFetch ?: ( ) => void
116
109
private isTransportCancelable ?: boolean
117
- private notifyGlobalListeners : ( query : Query < TResult , TError > ) => void
118
-
119
- constructor ( init : QueryInitConfig < TResult , TError > ) {
120
- this . config = init . config
121
- this . queryCache = init . queryCache
122
- this . queryKey = init . queryKey
123
- this . queryHash = init . queryHash
124
- this . notifyGlobalListeners = init . notifyGlobalListeners
110
+
111
+ constructor (
112
+ queryKey : ArrayQueryKey ,
113
+ queryHash : string ,
114
+ config : QueryConfig < TResult , TError >
115
+ ) {
116
+ this . config = config
117
+ this . queryKey = queryKey
118
+ this . queryHash = queryHash
119
+ this . queryCache = config . queryCache !
125
120
this . observers = [ ]
126
- this . state = getDefaultState ( init . config )
127
- this . cacheTime = init . config . cacheTime !
121
+ this . state = getDefaultState ( config )
122
+ this . cacheTime = config . cacheTime !
128
123
this . scheduleGc ( )
129
124
}
130
125
@@ -140,7 +135,7 @@ export class Query<TResult, TError> {
140
135
observer . onQueryUpdate ( action )
141
136
} )
142
137
143
- this . notifyGlobalListeners ( this )
138
+ this . queryCache . notifyGlobalListeners ( this )
144
139
}
145
140
146
141
private scheduleGc ( ) : void {
@@ -150,7 +145,7 @@ export class Query<TResult, TError> {
150
145
151
146
this . clearGcTimeout ( )
152
147
153
- if ( this . cacheTime === Infinity || this . observers . length > 0 ) {
148
+ if ( this . observers . length > 0 || ! isValidTimeout ( this . cacheTime ) ) {
154
149
return
155
150
}
156
151
@@ -222,7 +217,7 @@ export class Query<TResult, TError> {
222
217
}
223
218
224
219
isStale ( ) : boolean {
225
- return this . observers . some ( observer => observer . isStale ( ) )
220
+ return this . observers . some ( observer => observer . getCurrentResult ( ) . isStale )
226
221
}
227
222
228
223
isStaleByTime ( staleTime = 0 ) : boolean {
@@ -234,16 +229,16 @@ export class Query<TResult, TError> {
234
229
onInteraction ( type : 'focus' | 'online' ) : void {
235
230
// Execute the first observer which is enabled,
236
231
// stale and wants to refetch on this interaction.
237
- const observer = this . observers . find (
232
+ const staleObserver = this . observers . find (
238
233
observer =>
239
- observer . isStale ( ) &&
234
+ observer . getCurrentResult ( ) . isStale &&
240
235
observer . config . enabled &&
241
236
( ( observer . config . refetchOnWindowFocus && type === 'focus' ) ||
242
237
( observer . config . refetchOnReconnect && type === 'online' ) )
243
238
)
244
239
245
- if ( observer ) {
246
- observer . fetch ( ) . catch ( noop )
240
+ if ( staleObserver ) {
241
+ staleObserver . fetch ( ) . catch ( noop )
247
242
}
248
243
249
244
// Continue any paused fetch
@@ -280,9 +275,9 @@ export class Query<TResult, TError> {
280
275
if ( this . isTransportCancelable ) {
281
276
this . cancel ( )
282
277
}
283
- }
284
278
285
- this . scheduleGc ( )
279
+ this . scheduleGc ( )
280
+ }
286
281
}
287
282
288
283
async refetch (
0 commit comments