99 isDocumentVisible ,
1010 isOnline ,
1111 isServer ,
12+ isValidTimeout ,
1213 noop ,
1314 replaceEqualDeep ,
1415 sleep ,
@@ -26,14 +27,6 @@ import { QueryObserver, UpdateListener } from './queryObserver'
2627
2728// TYPES
2829
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-
3730export interface QueryState < TResult , TError > {
3831 canFetchMore ?: boolean
3932 data ?: TResult
@@ -65,11 +58,11 @@ export interface RefetchOptions {
6558 throwOnError ?: boolean
6659}
6760
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 ,
7366}
7467
7568interface FailedAction {
@@ -114,17 +107,19 @@ export class Query<TResult, TError> {
114107 private cancelFetch ?: ( ) => void
115108 private continueFetch ?: ( ) => void
116109 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 !
125120 this . observers = [ ]
126- this . state = getDefaultState ( init . config )
127- this . cacheTime = init . config . cacheTime !
121+ this . state = getDefaultState ( config )
122+ this . cacheTime = config . cacheTime !
128123 this . scheduleGc ( )
129124 }
130125
@@ -140,7 +135,7 @@ export class Query<TResult, TError> {
140135 observer . onQueryUpdate ( action )
141136 } )
142137
143- this . notifyGlobalListeners ( this )
138+ this . queryCache . notifyGlobalListeners ( this )
144139 }
145140
146141 private scheduleGc ( ) : void {
@@ -150,7 +145,7 @@ export class Query<TResult, TError> {
150145
151146 this . clearGcTimeout ( )
152147
153- if ( this . cacheTime === Infinity || this . observers . length > 0 ) {
148+ if ( this . observers . length > 0 || ! isValidTimeout ( this . cacheTime ) ) {
154149 return
155150 }
156151
@@ -234,16 +229,16 @@ export class Query<TResult, TError> {
234229 onInteraction ( type : 'focus' | 'online' ) : void {
235230 // Execute the first observer which is enabled,
236231 // stale and wants to refetch on this interaction.
237- const observer = this . observers . find (
232+ const staleObserver = this . observers . find (
238233 observer =>
239234 observer . isStale ( ) &&
240235 observer . config . enabled &&
241236 ( ( observer . config . refetchOnWindowFocus && type === 'focus' ) ||
242237 ( observer . config . refetchOnReconnect && type === 'online' ) )
243238 )
244239
245- if ( observer ) {
246- observer . fetch ( ) . catch ( noop )
240+ if ( staleObserver ) {
241+ staleObserver . fetch ( ) . catch ( noop )
247242 }
248243
249244 // Continue any paused fetch
@@ -280,9 +275,9 @@ export class Query<TResult, TError> {
280275 if ( this . isTransportCancelable ) {
281276 this . cancel ( )
282277 }
283- }
284278
285- this . scheduleGc ( )
279+ this . scheduleGc ( )
280+ }
286281 }
287282
288283 async refetch (
0 commit comments