Skip to content

Commit 2fe5915

Browse files
committed
Updated middleware to handle poll skipping
Changes made to the polling middleware to handle the situation when the page is out of focus. The code now includes a check to see whether to skip polling if the page is out of focus. Restructured the return from 'findLowestPollingInterval' function to include 'skipPollOnFocusLost' flag.
1 parent 6041460 commit 2fe5915

File tree

1 file changed

+27
-18
lines changed
  • packages/toolkit/src/query/core/buildMiddleware

1 file changed

+27
-18
lines changed

packages/toolkit/src/query/core/buildMiddleware/polling.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,38 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
5353
{ queryCacheKey }: QuerySubstateIdentifier,
5454
api: SubMiddlewareApi
5555
) {
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];
5959

6060
if (!querySubState || querySubState.status === QueryStatus.uninitialized)
61-
return
61+
return;
6262

63-
const lowestPollingInterval = findLowestPollingInterval(subscriptions)
64-
if (!Number.isFinite(lowestPollingInterval)) return
63+
const { lowestPollingInterval, skipPollOnFocusLost } = findLowestPollingInterval(subscriptions);
64+
if (!Number.isFinite(lowestPollingInterval)) return;
6565

66-
const currentPoll = currentPolls[queryCacheKey]
66+
const currentPoll = currentPolls[queryCacheKey];
6767

6868
if (currentPoll?.timeout) {
69-
clearTimeout(currentPoll.timeout)
70-
currentPoll.timeout = undefined
69+
clearTimeout(currentPoll.timeout);
70+
currentPoll.timeout = undefined;
7171
}
7272

73-
const nextPollTimestamp = Date.now() + lowestPollingInterval
73+
const nextPollTimestamp = Date.now() + lowestPollingInterval;
7474

75-
const currentInterval: typeof currentPolls[number] = (currentPolls[
76-
queryCacheKey
77-
] = {
75+
// Always update the polling interval
76+
currentPolls[queryCacheKey] = {
7877
nextPollTimestamp,
7978
pollingInterval: lowestPollingInterval,
8079
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);
8386
}, lowestPollingInterval),
84-
})
87+
};
8588
}
8689

8790
function updatePollingInterval(
@@ -96,7 +99,7 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
9699
return
97100
}
98101

99-
const lowestPollingInterval = findLowestPollingInterval(subscriptions)
102+
const { lowestPollingInterval } = findLowestPollingInterval(subscriptions)
100103

101104
if (!Number.isFinite(lowestPollingInterval)) {
102105
cleanupPollForKey(queryCacheKey)
@@ -126,17 +129,23 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
126129
}
127130

128131
function findLowestPollingInterval(subscribers: Subscribers = {}) {
132+
let skipPollOnFocusLost: boolean | undefined = false
129133
let lowestPollingInterval = Number.POSITIVE_INFINITY
130134
for (let key in subscribers) {
131135
if (!!subscribers[key].pollingInterval) {
132136
lowestPollingInterval = Math.min(
133137
subscribers[key].pollingInterval!,
134138
lowestPollingInterval
135139
)
140+
skipPollOnFocusLost = subscribers[key].skipPollOnFocusLost
136141
}
137142
}
138143

139-
return lowestPollingInterval
144+
return {
145+
lowestPollingInterval,
146+
skipPollOnFocusLost,
147+
}
140148
}
149+
141150
return handler
142151
}

0 commit comments

Comments
 (0)