Skip to content

Commit

Permalink
Polling skipPollOnFocusLost behavior changed to skip if any subscript…
Browse files Browse the repository at this point in the history
…ion has the setting and tests changed to better reflect this behavior and intent
  • Loading branch information
riqts committed Jan 24, 2024
1 parent e676ee8 commit 4ae5a9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/toolkit/src/query/core/buildMiddleware/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ export const buildPollingHandler: InternalHandlerBuilder = ({
subscribers[key].pollingInterval!,
lowestPollingInterval
)
skipPollOnFocusLost = subscribers[key].skipPollOnFocusLost
skipPollOnFocusLost =
subscribers[key].skipPollOnFocusLost || skipPollOnFocusLost
}
}

Expand Down
20 changes: 15 additions & 5 deletions packages/toolkit/src/query/tests/polling.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('polling tests', () => {
storeRef.store.dispatch(api.util.resetApiState())
})

it('respects skipPollOnFocusLost of the most recent mounted subscription', async () => {
it('respects skipPollOnFocusLost if at least one subscription has it', async () => {
storeRef.store.dispatch(
getPosts.initiate(3, {
subscriptionOptions: {
Expand All @@ -169,7 +169,7 @@ describe('polling tests', () => {
)

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

storeRef.store.dispatch(
getPosts.initiate(3, {
Expand All @@ -178,13 +178,23 @@ describe('polling tests', () => {
})
)

storeRef.store.dispatch(
getPosts.initiate(3, {
subscriptionOptions: {
pollingInterval: 20,
skipPollOnFocusLost: false,
},
subscribe: true,
})
)

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

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

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

it('replaces skipPollOnFocusLost when the subscription options are updated', async () => {
Expand Down

0 comments on commit 4ae5a9b

Please sign in to comment.