-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: clear refresh timer on effects change and check it on starting new timer #853
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
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit a8d3549:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good catch! Indeed there's an async process inside tick
so it's not safe to guarantee that cancel is safe. But I don't understand why you need an object to bind the latest timer value to the closure, I suppose just timer
would work too (by checking if it's null
on line 688)?
@shuding yeah re-test it, it works well! thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much 😊
It seems i am a little late 😂. I just find a solution // Read the latest refreshInterval and make sure the key is not changed
if (configRef.current.refreshInterval && key === keyRef.current) {
timer = setTimeout(tick, configRef.current.refreshInterval)
} |
@promer94 that's a very good point! but the effects clean up can due to several change, we cannot guarantee the |
Another problem is that the actual refreshInterval is the We should mention this in the docs |
Seems some interval tests are failing |
3898e64
to
a8d3549
Compare
Fix #852
Description
swr/src/use-swr.ts
Lines 687 to 697 in 00c5847
when timer is been cancelled, but to the timer procedure, it cannot know if the last timer is cancelled. so it started a new timer again.
steps:
start timer > revalidate > timer is cancelled due to effects > but still start a new timer (with legacy
revalidate
).Changes
mark timer as null when cancel it and the cancel state is kept as condition. then swr can avoid to start a new timer again.