@@ -53,35 +53,38 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
53
53
{ queryCacheKey } : QuerySubstateIdentifier ,
54
54
api : SubMiddlewareApi
55
55
) {
56
- const state = api . getState ( ) [ reducerPath ]
57
- const querySubState = state . queries [ queryCacheKey ]
58
- const subscriptions = internalState . currentSubscriptions [ queryCacheKey ]
56
+ const state = api . getState ( ) [ reducerPath ] ;
57
+ const querySubState = state . queries [ queryCacheKey ] ;
58
+ const subscriptions = internalState . currentSubscriptions [ queryCacheKey ] ;
59
59
60
60
if ( ! querySubState || querySubState . status === QueryStatus . uninitialized )
61
- return
61
+ return ;
62
62
63
- const lowestPollingInterval = findLowestPollingInterval ( subscriptions )
64
- if ( ! Number . isFinite ( lowestPollingInterval ) ) return
63
+ const { lowestPollingInterval, skipPollOnFocusLost } = findLowestPollingInterval ( subscriptions ) ;
64
+ if ( ! Number . isFinite ( lowestPollingInterval ) ) return ;
65
65
66
- const currentPoll = currentPolls [ queryCacheKey ]
66
+ const currentPoll = currentPolls [ queryCacheKey ] ;
67
67
68
68
if ( currentPoll ?. timeout ) {
69
- clearTimeout ( currentPoll . timeout )
70
- currentPoll . timeout = undefined
69
+ clearTimeout ( currentPoll . timeout ) ;
70
+ currentPoll . timeout = undefined ;
71
71
}
72
72
73
- const nextPollTimestamp = Date . now ( ) + lowestPollingInterval
73
+ const nextPollTimestamp = Date . now ( ) + lowestPollingInterval ;
74
74
75
- const currentInterval : typeof currentPolls [ number ] = ( currentPolls [
76
- queryCacheKey
77
- ] = {
75
+ // Always update the polling interval
76
+ currentPolls [ queryCacheKey ] = {
78
77
nextPollTimestamp,
79
78
pollingInterval : lowestPollingInterval ,
80
79
timeout : setTimeout ( ( ) => {
81
- currentInterval ! . timeout = undefined
82
- api . dispatch ( refetchQuery ( querySubState , queryCacheKey ) )
80
+ // Conditionally dispatch the query
81
+ if ( document . hasFocus ( ) || ! skipPollOnFocusLost ) {
82
+ api . dispatch ( refetchQuery ( querySubState , queryCacheKey ) ) ;
83
+ }
84
+ // Regardless of dispatch, set up the next poll
85
+ startNextPoll ( { queryCacheKey } , api ) ;
83
86
} , lowestPollingInterval ) ,
84
- } )
87
+ } ;
85
88
}
86
89
87
90
function updatePollingInterval (
@@ -96,7 +99,7 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
96
99
return
97
100
}
98
101
99
- const lowestPollingInterval = findLowestPollingInterval ( subscriptions )
102
+ const { lowestPollingInterval } = findLowestPollingInterval ( subscriptions )
100
103
101
104
if ( ! Number . isFinite ( lowestPollingInterval ) ) {
102
105
cleanupPollForKey ( queryCacheKey )
@@ -126,17 +129,23 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
126
129
}
127
130
128
131
function findLowestPollingInterval ( subscribers : Subscribers = { } ) {
132
+ let skipPollOnFocusLost : boolean | undefined = false
129
133
let lowestPollingInterval = Number . POSITIVE_INFINITY
130
134
for ( let key in subscribers ) {
131
135
if ( ! ! subscribers [ key ] . pollingInterval ) {
132
136
lowestPollingInterval = Math . min (
133
137
subscribers [ key ] . pollingInterval ! ,
134
138
lowestPollingInterval
135
139
)
140
+ skipPollOnFocusLost = subscribers [ key ] . skipPollOnFocusLost
136
141
}
137
142
}
138
143
139
- return lowestPollingInterval
144
+ return {
145
+ lowestPollingInterval,
146
+ skipPollOnFocusLost,
147
+ }
140
148
}
149
+
141
150
return handler
142
151
}
0 commit comments