Skip to content

Commit 65b448f

Browse files
committed
Update skipPollOnFocusLost to use listenerMiddleware
Added documentation to apiState, tests to support listenerMiddleware usage and polling.ts updated to match
1 parent 823c3b7 commit 65b448f

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

packages/toolkit/src/query/core/apiState.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ export type SubscriptionOptions = {
8787
* Defaults to 'false'. This setting allows you to control whether RTK Query will continue polling if the window is not focused.
8888
*
8989
* If pollingInterval is not set or set to 0, this **will not be evaluated** until pollingInterval is greater than 0.
90+
*
91+
* Note: requires [`setupListeners`](./setupListeners) to have been called.
9092
*/
9193
skipPollOnFocusLost?: boolean
9294
/**

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
7777
nextPollTimestamp,
7878
pollingInterval: lowestPollingInterval,
7979
timeout: setTimeout(() => {
80-
if (document.hasFocus() || !skipPollOnFocusLost) {
80+
if (state.config.focused || !skipPollOnFocusLost) {
8181
api.dispatch(refetchQuery(querySubState, queryCacheKey))
8282
}
8383
startNextPoll({ queryCacheKey }, api)
@@ -135,7 +135,9 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
135135
subscribers[key].pollingInterval!,
136136
lowestPollingInterval
137137
)
138-
skipPollOnFocusLost = subscribers[key].skipPollOnFocusLost
138+
// if (!skipPollOnFocusLost) {
139+
skipPollOnFocusLost = subscribers[key].skipPollOnFocusLost
140+
// }
139141
}
140142
}
141143

packages/toolkit/src/query/tests/polling.test.tsx

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { createApi } from '@reduxjs/toolkit/query'
22
import { delay } from 'msw'
33
import { setupApiStore } from './helpers'
44
import type { SubscriptionSelectors } from '../core/buildMiddleware/types'
5+
import { createListenerMiddleware } from '@reduxjs/toolkit'
6+
57

68
const mockBaseQuery = vi
79
.fn()
@@ -125,18 +127,27 @@ describe('polling tests', () => {
125127

126128
it('respects skipPollOnFocusLost', async () => {
127129
mockBaseQuery.mockClear()
128-
storeRef.store.dispatch(
129-
getPosts.initiate(1, {
130+
const listenerMiddleware = createListenerMiddleware()
131+
const storeListenerRef = setupApiStore(api, undefined, {
132+
middleware: {
133+
concat: [listenerMiddleware.middleware],
134+
},
135+
withoutTestLifecycles: true,
136+
})
137+
138+
storeListenerRef.store.dispatch(
139+
getPosts.initiate(2, {
130140
subscriptionOptions: { pollingInterval: 10, skipPollOnFocusLost: true },
131141
subscribe: true,
132142
})
133143
)
144+
storeListenerRef.store.dispatch(api.internalActions?.onFocusLost())
134145

135-
await delay(20)
146+
await delay(50)
136147
const callsWithSkip = mockBaseQuery.mock.calls.length
137148

138-
storeRef.store.dispatch(
139-
getPosts.initiate(1, {
149+
storeListenerRef.store.dispatch(
150+
getPosts.initiate(2, {
140151
subscriptionOptions: {
141152
pollingInterval: 10,
142153
skipPollOnFocusLost: false,
@@ -145,17 +156,28 @@ describe('polling tests', () => {
145156
})
146157
)
147158

148-
await delay(30)
159+
storeListenerRef.store.dispatch(api.internalActions?.onFocus())
160+
161+
await delay(50)
149162
const callsWithoutSkip = mockBaseQuery.mock.calls.length
150-
console.log(callsWithSkip, callsWithoutSkip)
151163

152164
expect(callsWithSkip).toBe(1)
153165
expect(callsWithoutSkip).toBeGreaterThan(2)
166+
167+
storeListenerRef.store.dispatch(api.util.resetApiState())
154168
})
155169

156-
it('replaces skipPollOnFocusLost with most recent mount', async () => {
157-
storeRef.store.dispatch(
158-
getPosts.initiate(1, {
170+
it('respects skipPollOnFocusLost if any subscription is true', async () => {
171+
const listenerMiddleware = createListenerMiddleware()
172+
const storeListenerRef = setupApiStore(api, undefined, {
173+
middleware: {
174+
concat: [listenerMiddleware.middleware],
175+
},
176+
withoutTestLifecycles: true,
177+
})
178+
179+
storeListenerRef.store.dispatch(
180+
getPosts.initiate(3, {
159181
subscriptionOptions: {
160182
pollingInterval: 10,
161183
skipPollOnFocusLost: false,
@@ -167,13 +189,15 @@ describe('polling tests', () => {
167189
await delay(50)
168190
const callsWithSkip = mockBaseQuery.mock.calls.length
169191

170-
storeRef.store.dispatch(
171-
getPosts.initiate(1, {
192+
storeListenerRef.store.dispatch(
193+
getPosts.initiate(3, {
172194
subscriptionOptions: { pollingInterval: 15, skipPollOnFocusLost: true },
173195
subscribe: true,
174196
})
175197
)
176198

199+
storeListenerRef.store.dispatch(api.internalActions?.onFocusLost())
200+
177201
await delay(50)
178202
const callsWithoutSkip = mockBaseQuery.mock.calls.length
179203

0 commit comments

Comments
 (0)