-
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
fix: not clear promise of revalidate after mutating #1139
fix: not clear promise of revalidate after mutating #1139
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 7e07e30:
|
// be called subsequently. | ||
delete CONCURRENT_PROMISES[key] | ||
delete CONCURRENT_PROMISES_TS[key] | ||
|
||
// enter the revalidation stage | ||
// update existing SWR Hooks' state | ||
const updaters = CACHE_REVALIDATORS[key] |
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.
It might be batter if we delete the promise conditionally
const updaters = CACHE_REVALIDATORS[key] | |
const updaters = CACHE_REVALIDATORS[key] | |
if (updaters && updaters.length > 0) { | |
const promises = [] | |
for (let i = 0; i < updaters.length; ++i) { | |
promises.push( | |
updaters[i](!!shouldRevalidate, data, error, undefined, i > 0) | |
) | |
} | |
// return new updated value | |
return Promise.all(promises).then(() => { | |
if (error) throw error | |
return cache.get(key) | |
}) | |
} else { | |
// clear dedupingInterval of an unmounted key | |
delete CONCURRENT_PROMISES[key] | |
delete CONCURRENT_PROMISES_TS[key] | |
} |
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.
It might be batter if we delete the promise conditionally
Thanks for your suggestions.
I deleted the promise conditionally at first. I think the conditional statement should be !!shouldRevalidate && (updaters && updaters.length > 0)
if deleting it conditionally. This conditional statement is little of complicated and hard to understand.
However, Deleting the promise after mutating is easier to understand. I would like to modify my comment to express it clearly.
I think the root cause of the original issue might not be the dedupingInterval ? import { mutate } from 'swr'
mutate('key') When using the global mutate
Lines 391 to 402 in f787624
The dedupingInterval will be ignored by these conditions. |
You are right. But The Some backend post api doesn't return the data, so RD should use fetch('/api/user/edit', { method: 'post', name })
mutate('/api/user') and then they want to fetch new data if they need. |
|
c0d73da
to
800a882
Compare
@promer94 Thanks for your review. It's better to keep I made a new commit, PTAL. |
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.
I still have some concerns about clearing the deduplicatedInterval in mutate function. I am not sure this is the best way to solve the problem. We might need more discussions about this.
// `updaters` would trigger revalidate if `updaters.length > 0`. | ||
// So clear promise of revalidate if `!updaters.length && shouldRevalidate` | ||
delete CONCURRENT_PROMISES[key] | ||
delete CONCURRENT_PROMISES_TS[key] | ||
} |
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.
if hook is unmounted
mutate('key') // This will not clear the deduplicatedInterval
mutate("key", () => undefined) // This will clear the deduplicatedInterval
mutate("key", () => Promise.resolve(undefined)) // This will also clear the deduplicatedInterval
Line 107 in f787624
if (typeof _data === 'undefined') return trigger(_key, shouldRevalidate) |
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.
if hook is unmounted, the above three cases will not clear the deduplicatedInterval because updaters && updaters.length
is true.
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.
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.
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! I think this is a pretty neat trick to work around that special case. I'll look into it deeper to see if there's any other problems, since this is not a perfect solution.
Fix #828.
See added test case for detail.