Skip to content

Commit

Permalink
Update skipPollOnFocusLost to use listenerMiddleware
Browse files Browse the repository at this point in the history
Added documentation to apiState, tests to support listenerMiddleware usage and polling.ts updated to match
  • Loading branch information
riqts committed Jan 24, 2024
1 parent 823c3b7 commit 65b448f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
2 changes: 2 additions & 0 deletions packages/toolkit/src/query/core/apiState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export type SubscriptionOptions = {
* Defaults to 'false'. This setting allows you to control whether RTK Query will continue polling if the window is not focused.
*
* If pollingInterval is not set or set to 0, this **will not be evaluated** until pollingInterval is greater than 0.
*
* Note: requires [`setupListeners`](./setupListeners) to have been called.
*/
skipPollOnFocusLost?: boolean
/**
Expand Down
6 changes: 4 additions & 2 deletions packages/toolkit/src/query/core/buildMiddleware/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
nextPollTimestamp,
pollingInterval: lowestPollingInterval,
timeout: setTimeout(() => {
if (document.hasFocus() || !skipPollOnFocusLost) {
if (state.config.focused || !skipPollOnFocusLost) {
api.dispatch(refetchQuery(querySubState, queryCacheKey))
}
startNextPoll({ queryCacheKey }, api)
Expand Down Expand Up @@ -135,7 +135,9 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
subscribers[key].pollingInterval!,
lowestPollingInterval
)
skipPollOnFocusLost = subscribers[key].skipPollOnFocusLost
// if (!skipPollOnFocusLost) {
skipPollOnFocusLost = subscribers[key].skipPollOnFocusLost
// }
}
}

Expand Down
48 changes: 36 additions & 12 deletions packages/toolkit/src/query/tests/polling.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { createApi } from '@reduxjs/toolkit/query'
import { delay } from 'msw'
import { setupApiStore } from './helpers'
import type { SubscriptionSelectors } from '../core/buildMiddleware/types'
import { createListenerMiddleware } from '@reduxjs/toolkit'


const mockBaseQuery = vi
.fn()
Expand Down Expand Up @@ -125,18 +127,27 @@ describe('polling tests', () => {

it('respects skipPollOnFocusLost', async () => {
mockBaseQuery.mockClear()
storeRef.store.dispatch(
getPosts.initiate(1, {
const listenerMiddleware = createListenerMiddleware()
const storeListenerRef = setupApiStore(api, undefined, {
middleware: {
concat: [listenerMiddleware.middleware],
},
withoutTestLifecycles: true,
})

storeListenerRef.store.dispatch(
getPosts.initiate(2, {
subscriptionOptions: { pollingInterval: 10, skipPollOnFocusLost: true },
subscribe: true,
})
)
storeListenerRef.store.dispatch(api.internalActions?.onFocusLost())

await delay(20)
await delay(50)
const callsWithSkip = mockBaseQuery.mock.calls.length

storeRef.store.dispatch(
getPosts.initiate(1, {
storeListenerRef.store.dispatch(
getPosts.initiate(2, {
subscriptionOptions: {
pollingInterval: 10,
skipPollOnFocusLost: false,
Expand All @@ -145,17 +156,28 @@ describe('polling tests', () => {
})
)

await delay(30)
storeListenerRef.store.dispatch(api.internalActions?.onFocus())

await delay(50)
const callsWithoutSkip = mockBaseQuery.mock.calls.length
console.log(callsWithSkip, callsWithoutSkip)

expect(callsWithSkip).toBe(1)
expect(callsWithoutSkip).toBeGreaterThan(2)

storeListenerRef.store.dispatch(api.util.resetApiState())
})

it('replaces skipPollOnFocusLost with most recent mount', async () => {
storeRef.store.dispatch(
getPosts.initiate(1, {
it('respects skipPollOnFocusLost if any subscription is true', async () => {
const listenerMiddleware = createListenerMiddleware()
const storeListenerRef = setupApiStore(api, undefined, {
middleware: {
concat: [listenerMiddleware.middleware],
},
withoutTestLifecycles: true,
})

storeListenerRef.store.dispatch(
getPosts.initiate(3, {
subscriptionOptions: {
pollingInterval: 10,
skipPollOnFocusLost: false,
Expand All @@ -167,13 +189,15 @@ describe('polling tests', () => {
await delay(50)
const callsWithSkip = mockBaseQuery.mock.calls.length

storeRef.store.dispatch(
getPosts.initiate(1, {
storeListenerRef.store.dispatch(
getPosts.initiate(3, {
subscriptionOptions: { pollingInterval: 15, skipPollOnFocusLost: true },
subscribe: true,
})
)

storeListenerRef.store.dispatch(api.internalActions?.onFocusLost())

await delay(50)
const callsWithoutSkip = mockBaseQuery.mock.calls.length

Expand Down

0 comments on commit 65b448f

Please sign in to comment.