Skip to content

Commit

Permalink
skipPollOnFocused lost to skipPollingIfUnfocused and yarn format
Browse files Browse the repository at this point in the history
  • Loading branch information
riqts committed Jan 24, 2024
1 parent 4ae5a9b commit 8cbf2a1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/core/apiState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
12 changes: 6 additions & 6 deletions packages/toolkit/src/query/core/buildMiddleware/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -127,22 +127,22 @@ 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) {
lowestPollingInterval = Math.min(
subscribers[key].pollingInterval!,
lowestPollingInterval
)
skipPollOnFocusLost =
subscribers[key].skipPollOnFocusLost || skipPollOnFocusLost
skipPollingIfUnfocused =
subscribers[key].skipPollingIfUnfocused || skipPollingIfUnfocused
}
}

return {
lowestPollingInterval,
skipPollOnFocusLost,
skipPollingIfUnfocused,
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
refetchOnMountOrArgChange,
skip = false,
pollingInterval = 0,
skipPollOnFocusLost = false,
skipPollingIfUnfocused = false,
} = {}
) => {
const { initiate } = api.endpoints[name] as ApiEndpointQuery<
Expand Down Expand Up @@ -716,7 +716,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
refetchOnReconnect,
refetchOnFocus,
pollingInterval,
skipPollOnFocusLost,
skipPollingIfUnfocused,
})

const lastRenderHadSubscription = useRef(false)
Expand Down Expand Up @@ -817,7 +817,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
refetchOnReconnect,
refetchOnFocus,
pollingInterval = 0,
skipPollOnFocusLost = false,
skipPollingIfUnfocused = false,
} = {}) => {
const { initiate } = api.endpoints[name] as ApiEndpointQuery<
QueryDefinition<any, any, any, any, any>,
Expand All @@ -832,7 +832,7 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
refetchOnReconnect,
refetchOnFocus,
pollingInterval,
skipPollOnFocusLost,
skipPollingIfUnfocused,
})

usePossiblyImmediateEffect(() => {
Expand Down
30 changes: 18 additions & 12 deletions packages/toolkit/src/query/tests/polling.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
)
Expand All @@ -140,7 +143,7 @@ describe('polling tests', () => {
getPosts.initiate(2, {
subscriptionOptions: {
pollingInterval: 10,
skipPollOnFocusLost: false,
skipPollingIfUnfocused: false,
},
subscribe: true,
})
Expand All @@ -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,
})
Expand All @@ -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,
})
)
Expand All @@ -182,7 +188,7 @@ describe('polling tests', () => {
getPosts.initiate(3, {
subscriptionOptions: {
pollingInterval: 20,
skipPollOnFocusLost: false,
skipPollingIfUnfocused: false,
},
subscribe: true,
})
Expand All @@ -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,
})
Expand All @@ -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)
})
})

0 comments on commit 8cbf2a1

Please sign in to comment.