-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot stop polling by setting refreshInterval 0 #632
Comments
useIsomorphicLayoutEffect(() => {
let timer = null
const tick = async () => {
if (
!stateRef.current.error &&
(config.refreshWhenHidden || isDocumentVisible()) &&
(config.refreshWhenOffline || isOnline())
) {
// onSuccess called but revalidate has not finished yet, refreshInterval will trigger the clean-up function
await revalidate({ dedupe: true })
}
if (config.refreshInterval) {
// timer changes after the clear-up function
timer = setTimeout(tick, config.refreshInterval)
}
}
if (config.refreshInterval) {
timer = setTimeout(tick, config.refreshInterval)
}
return () => {
if (timer) clearTimeout(timer)
}
}, [
config.refreshInterval,
config.refreshWhenHidden,
config.refreshWhenOffline,
revalidate
])
|
@promer94 Is this expected behavior or an issue to be fixed? |
I think it's the naming of A workaround can be moving my 'when fetches finish' logic from async () => {
await generalFetcher();
setIntValue(value => value + 1);
},
{
refreshInterval: intValue < 2 ? 1000 : 0, // intValue is initially 0
// onSuccess() {
// setIntValue(value => value + 1);
// }
} |
Lines 362 to 371 in ba18429
onSuccess is actually 'when fetcher finish'.
|
@SevenOutman But you mentioned in your comment that "onSuccess called but revalidate has not finished yet" |
A simplified revalidation steps
So |
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.In my swr config I also have an
onSuccess
callback in which I change my flag valueWhen 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.
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.
The text was updated successfully, but these errors were encountered: