-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -64,7 +64,7 @@ const trigger: Trigger = (_key, shouldRevalidate = true) => { | |||
|
||||
const updaters = CACHE_REVALIDATORS[key] | ||||
|
||||
if (key && updaters) { | ||||
if (key && updaters && updaters.length) { | ||||
const currentData = cache.get(key) | ||||
const currentError = cache.get(keyErr) | ||||
const currentIsValidating = cache.get(keyValidating) | ||||
|
@@ -82,6 +82,11 @@ const trigger: Trigger = (_key, shouldRevalidate = true) => { | |||
} | ||||
// return new updated value | ||||
return Promise.all(promises).then(() => cache.get(key)) | ||||
} else if (shouldRevalidate) { | ||||
// `updaters` would trigger revalidate if `updaters.length > 0`. | ||||
// So only clear promise of revalidate if `!updaters.length && shouldRevalidate` | ||||
delete CONCURRENT_PROMISES[key] | ||||
delete CONCURRENT_PROMISES_TS[key] | ||||
} | ||||
return Promise.resolve(cache.get(key)) | ||||
} | ||||
|
@@ -168,7 +173,7 @@ async function mutate<Data = any>( | |||
// enter the revalidation stage | ||||
// update existing SWR Hooks' state | ||||
const updaters = CACHE_REVALIDATORS[key] | ||||
if (updaters) { | ||||
if (updaters && updaters.length) { | ||||
const promises = [] | ||||
for (let i = 0; i < updaters.length; ++i) { | ||||
promises.push( | ||||
|
@@ -180,6 +185,11 @@ async function mutate<Data = any>( | |||
if (error) throw error | ||||
return cache.get(key) | ||||
}) | ||||
} else if (shouldRevalidate) { | ||||
// `updaters` would trigger revalidate if `updaters.length > 0`. | ||||
// So only 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 commentThe reason will be displayed to describe this comment to others. Learn more. if hook is unmounted
Line 107 in f787624
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
// throw error or return data to be used by caller of mutate | ||||
if (error) throw error | ||||
|
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
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.
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.