Skip to content

Fix refetchInterval infinite loop - #404

Merged
tannerlinsley merged 4 commits into
TanStack:masterfrom
cherniavskii:fix/refetchInterval-infinite-loop
May 1, 2020
Merged

Fix refetchInterval infinite loop#404
tannerlinsley merged 4 commits into
TanStack:masterfrom
cherniavskii:fix/refetchInterval-infinite-loop

Conversation

@cherniavskii

Copy link
Copy Markdown
Contributor

Use queryRef to avoid infinite loop in refetch interval effect.

Fixes #399

Comment thread src/useBaseQuery.js Outdated
const query = queryRef.current
if (
config.refetchInterval &&
(!query.refetchInterval || config.refetchInterval < query.refetchInterval)

@cherniavskii cherniavskii Apr 25, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think query.refetchInterval name is confusing and should be renamed to query.refetchIntervalId, since it actually stores interval ID.

Then config.refetchInterval < query.refetchInterval comparison doesn't make sense to me.

As well as !query.refetchInterval check, since query.refetchInterval will be always undefined due to cleaning function returned by useEffect.

@tannerlinsley thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the idea was that if a user passed different intervals for the same query but on different instances, then the shortest interval time would override the longer one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should work as you described now!

@cherniavskii

Copy link
Copy Markdown
Contributor Author

Wanted to add a unit test fot this case, but it involves dealing with timers, which can be tricky and I did't want to invest my time into this right now.
So I've created an issue - #405
Please add "help wanted" label to it :)

Comment thread src/queryCache.js
if (query) {
Object.assign(query, { queryVariables, queryFn })
Object.assign(query.config, config)
query.config = { ...query.config, ...config }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tannerlinsley

Object.assign was causing a bug. Consider this situation:

  1. first instance of useQuery creates a new query and sets query.config pointer to config object
  2. second instance of useQuery with different config object takes query from cache
  3. Object.assign mutates first instance query.config object

Now as I think about it, maybe we should copy query config in the first step instead of referencing it?
Either way, it should be good now.

But that was super tough to debug :D

@tannerlinsley

Copy link
Copy Markdown
Member

Yes!! This was a total doozy

@tannerlinsley
tannerlinsley merged commit 43e223a into TanStack:master May 1, 2020
@cherniavskii
cherniavskii deleted the fix/refetchInterval-infinite-loop branch May 1, 2020 11:31
@nx-cloud

nx-cloud Bot commented Jun 26, 2025

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit cc9fd67

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded <1s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 2s View ↗

☁️ Nx Cloud last updated this comment at 2026-04-01 13:57:28 UTC

GEREGUR pushed a commit to GEREGUR/query that referenced this pull request Feb 17, 2026
* use react-pacer

* use callbacks for throttling

---------

Co-authored-by: Kevin Van Cott <kevinvandy656@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants