question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot stop polling by setting refreshInterval 0

See original GitHub issue

SWR version 0.3.1

I have a demo reproducing this issue here.

In this demo I have a flag variable controlling whether swr should poll by conditionally setting refreshInterval value.

{
  refreshInterval: shouldPoll ? 1000 : 0
}

In my swr config I also have an onSuccess callback in which I change my flag value

{
  refreshInterval: shouldPoll ? 1000 : 0,
  onSuccess() {
    setShouldPoll(false);
  }
}

When the flag is set to false as the first fetch finishes, the polling stops normally as expected. Everything is fine so far.

However, if I set the flag value in the callback not of the first fetch but the revalidation fetches, polling is not stopping.

{
  refreshInterval: intValue < 2 ? 1000 : 0, // intValue is initially 0
  onSuccess() {
    setIntValue(value => value + 1); 
  }
}

After the first fetch, intValue is set to 1, and polling is supposed to continue. After the 2nd fetch, intValue is set to 2, and polling is supposed to stop because refreshInterval is now 0. But the polling continued.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
SevenOutmancommented, Sep 7, 2020

I think it’s the naming of onSuccess options that is tricky. I understand it as ‘when fetches finish’, while it’s actually not.

A workaround can be moving my ‘when fetches finish’ logic from onSuccess into a customer fetcher.

async () => {
  await generalFetcher();
  setIntValue(value => value + 1); 
},
{
  refreshInterval: intValue < 2 ? 1000 : 0, // intValue is initially 0
  // onSuccess() {
  //   setIntValue(value => value + 1); 
  // }
}
0reactions
promer94commented, Sep 8, 2020

A simplified revalidation steps

  1. revalidate start
  2. newData = await CONCURRENT_PROMISES[key]
    the fetcher has finished and we have newData
  3. eventsRef.current.emit('onSuccess', newData, key, config) onSucuess called
  4. newData is used to update stateRef.
  5. revalidate is over

So onSuccess is called after we successfully get the newData.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js - SWR hook question about dedupeInterval and ...
Yes, if the user opens your page and does nothing but reading content during 20 seconds, and you have set the refreshInterval to...
Read more >
Change the period override setting or refresh interval for the ...
The Period override always reverts to Auto when the dashboard is closed or the browser is refreshed. Different settings for Period override can't...
Read more >
Settings for minimizing periodic WAN traffic - Windows Client
The background retry quit time. When a program runs a periodic search for domain controllers and cannot find a domain controller, the value...
Read more >
API - SWR
Disabled by default: refreshInterval = 0; If set to a number, polling interval in milliseconds; If set to a function, the function will...
Read more >
Queries - Apollo GraphQL Docs
Note that if you set pollInterval to 0 , the query does not poll. You can also start and stop polling dynamically with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found