diff --git a/packages/toolkit/src/query/core/apiState.ts b/packages/toolkit/src/query/core/apiState.ts index 79b248ea35..71ca4856e4 100644 --- a/packages/toolkit/src/query/core/apiState.ts +++ b/packages/toolkit/src/query/core/apiState.ts @@ -90,7 +90,7 @@ export type SubscriptionOptions = { * * Note: requires [`setupListeners`](./setupListeners) to have been called. */ - skipPollOnFocusLost?: boolean + skipPollingIfUnfocused?: boolean /** * Defaults to `false`. This setting allows you to control whether RTK Query will try to refetch all subscribed queries after regaining a network connection. * diff --git a/packages/toolkit/src/query/core/buildMiddleware/polling.ts b/packages/toolkit/src/query/core/buildMiddleware/polling.ts index beb5666bbc..2870d43443 100644 --- a/packages/toolkit/src/query/core/buildMiddleware/polling.ts +++ b/packages/toolkit/src/query/core/buildMiddleware/polling.ts @@ -60,7 +60,7 @@ export const buildPollingHandler: InternalHandlerBuilder = ({ if (!querySubState || querySubState.status === QueryStatus.uninitialized) return - const { lowestPollingInterval, skipPollOnFocusLost } = + const { lowestPollingInterval, skipPollingIfUnfocused } = findLowestPollingInterval(subscriptions) if (!Number.isFinite(lowestPollingInterval)) return @@ -77,7 +77,7 @@ export const buildPollingHandler: InternalHandlerBuilder = ({ nextPollTimestamp, pollingInterval: lowestPollingInterval, timeout: setTimeout(() => { - if (state.config.focused || !skipPollOnFocusLost) { + if (state.config.focused || !skipPollingIfUnfocused) { api.dispatch(refetchQuery(querySubState, queryCacheKey)) } startNextPoll({ queryCacheKey }, api) @@ -127,7 +127,7 @@ export const buildPollingHandler: InternalHandlerBuilder = ({ } function findLowestPollingInterval(subscribers: Subscribers = {}) { - let skipPollOnFocusLost: boolean | undefined = false + let skipPollingIfUnfocused: boolean | undefined = false let lowestPollingInterval = Number.POSITIVE_INFINITY for (let key in subscribers) { if (!!subscribers[key].pollingInterval) { @@ -135,14 +135,14 @@ export const buildPollingHandler: InternalHandlerBuilder = ({ subscribers[key].pollingInterval!, lowestPollingInterval ) - skipPollOnFocusLost = - subscribers[key].skipPollOnFocusLost || skipPollOnFocusLost + skipPollingIfUnfocused = + subscribers[key].skipPollingIfUnfocused || skipPollingIfUnfocused } } return { lowestPollingInterval, - skipPollOnFocusLost, + skipPollingIfUnfocused, } } diff --git a/packages/toolkit/src/query/react/buildHooks.ts b/packages/toolkit/src/query/react/buildHooks.ts index 369637f5b4..a17ba0ae4f 100644 --- a/packages/toolkit/src/query/react/buildHooks.ts +++ b/packages/toolkit/src/query/react/buildHooks.ts @@ -672,7 +672,7 @@ export function buildHooks({ refetchOnMountOrArgChange, skip = false, pollingInterval = 0, - skipPollOnFocusLost = false, + skipPollingIfUnfocused = false, } = {} ) => { const { initiate } = api.endpoints[name] as ApiEndpointQuery< @@ -716,7 +716,7 @@ export function buildHooks({ refetchOnReconnect, refetchOnFocus, pollingInterval, - skipPollOnFocusLost, + skipPollingIfUnfocused, }) const lastRenderHadSubscription = useRef(false) @@ -817,7 +817,7 @@ export function buildHooks({ refetchOnReconnect, refetchOnFocus, pollingInterval = 0, - skipPollOnFocusLost = false, + skipPollingIfUnfocused = false, } = {}) => { const { initiate } = api.endpoints[name] as ApiEndpointQuery< QueryDefinition, @@ -832,7 +832,7 @@ export function buildHooks({ refetchOnReconnect, refetchOnFocus, pollingInterval, - skipPollOnFocusLost, + skipPollingIfUnfocused, }) usePossiblyImmediateEffect(() => { diff --git a/packages/toolkit/src/query/tests/polling.test.tsx b/packages/toolkit/src/query/tests/polling.test.tsx index 297d4eaff3..1e5deb1056 100644 --- a/packages/toolkit/src/query/tests/polling.test.tsx +++ b/packages/toolkit/src/query/tests/polling.test.tsx @@ -123,11 +123,14 @@ describe('polling tests', () => { expect(mockBaseQuery.mock.calls.length).toBeGreaterThanOrEqual(2) }) - it('respects skipPollOnFocusLost', async () => { + it('respects skipPollingIfUnfocused', async () => { mockBaseQuery.mockClear() storeRef.store.dispatch( getPosts.initiate(2, { - subscriptionOptions: { pollingInterval: 10, skipPollOnFocusLost: true }, + subscriptionOptions: { + pollingInterval: 10, + skipPollingIfUnfocused: true, + }, subscribe: true, }) ) @@ -140,7 +143,7 @@ describe('polling tests', () => { getPosts.initiate(2, { subscriptionOptions: { pollingInterval: 10, - skipPollOnFocusLost: false, + skipPollingIfUnfocused: false, }, subscribe: true, }) @@ -157,12 +160,12 @@ describe('polling tests', () => { storeRef.store.dispatch(api.util.resetApiState()) }) - it('respects skipPollOnFocusLost if at least one subscription has it', async () => { + it('respects skipPollingIfUnfocused if at least one subscription has it', async () => { storeRef.store.dispatch( getPosts.initiate(3, { subscriptionOptions: { pollingInterval: 10, - skipPollOnFocusLost: false, + skipPollingIfUnfocused: false, }, subscribe: true, }) @@ -173,7 +176,10 @@ describe('polling tests', () => { storeRef.store.dispatch( getPosts.initiate(3, { - subscriptionOptions: { pollingInterval: 15, skipPollOnFocusLost: true }, + subscriptionOptions: { + pollingInterval: 15, + skipPollingIfUnfocused: true, + }, subscribe: true, }) ) @@ -182,7 +188,7 @@ describe('polling tests', () => { getPosts.initiate(3, { subscriptionOptions: { pollingInterval: 20, - skipPollOnFocusLost: false, + skipPollingIfUnfocused: false, }, subscribe: true, }) @@ -197,13 +203,13 @@ describe('polling tests', () => { expect(callsWithSkip).toBe(callsWithoutSkip + 1) }) - it('replaces skipPollOnFocusLost when the subscription options are updated', async () => { + it('replaces skipPollingIfUnfocused when the subscription options are updated', async () => { const { requestId, queryCacheKey, ...subscription } = storeRef.store.dispatch( getPosts.initiate(1, { subscriptionOptions: { pollingInterval: 10, - skipPollOnFocusLost: false, + skipPollingIfUnfocused: false, }, subscribe: true, }) @@ -213,15 +219,15 @@ describe('polling tests', () => { await delay(1) expect(Object.keys(getSubs())).toHaveLength(1) - expect(getSubs()[requestId].skipPollOnFocusLost).toBe(false) + expect(getSubs()[requestId].skipPollingIfUnfocused).toBe(false) subscription.updateSubscriptionOptions({ pollingInterval: 20, - skipPollOnFocusLost: true, + skipPollingIfUnfocused: true, }) await delay(1) expect(Object.keys(getSubs())).toHaveLength(1) - expect(getSubs()[requestId].skipPollOnFocusLost).toBe(true) + expect(getSubs()[requestId].skipPollingIfUnfocused).toBe(true) }) })