-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: different refetchIntervals per instance use the min
- Loading branch information
1 parent
b6314ab
commit b9168b8
Showing
2 changed files
with
46 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -210,18 +210,6 @@ export function makeQueryCache({ frozen = isServer, defaultConfig } = {}) { | |
}, | ||
} | ||
|
||
if (!isServer) { | ||
query.cancelInterval() | ||
if (config.refetchInterval) { | ||
query.currentRefetchInterval = config.refetchInterval | ||
query.refetchIntervalId = setInterval(() => { | ||
if (isDocumentVisible() || config.refetchIntervalInBackground) { | ||
query.fetch() | ||
} | ||
}, config.refetchInterval) | ||
} | ||
} | ||
|
||
return query | ||
} | ||
|
||
|
@@ -344,19 +332,17 @@ export function makeQueryCache({ frozen = isServer, defaultConfig } = {}) { | |
query.cancel = () => { | ||
query.cancelled = cancelledError | ||
|
||
query.cancelInterval() | ||
|
||
if (query.cancelPromises) { | ||
query.cancelPromises() | ||
} | ||
|
||
delete query.promise | ||
} | ||
|
||
query.cancelInterval = () => { | ||
clearInterval(query.refetchIntervalId) | ||
delete query.refetchIntervalId | ||
delete query.currentRefetchInterval | ||
query.clearIntervals = () => { | ||
query.instances.forEach(instance => { | ||
instance.clearInterval() | ||
}) | ||
} | ||
|
||
query.setState = updater => | ||
|
@@ -373,6 +359,7 @@ export function makeQueryCache({ frozen = isServer, defaultConfig } = {}) { | |
query.clear = () => { | ||
clearTimeout(query.staleTimeout) | ||
clearTimeout(query.cacheTimeout) | ||
query.clearIntervals() | ||
query.cancel() | ||
query.dispatch = noop | ||
delete queryCache.queries[query.queryHash] | ||
|
@@ -388,8 +375,40 @@ export function makeQueryCache({ frozen = isServer, defaultConfig } = {}) { | |
|
||
query.heal() | ||
|
||
instance.clearInterval = () => { | ||
clearInterval(instance.refetchIntervalId) | ||
delete instance.refetchIntervalId | ||
} | ||
|
||
instance.updateConfig = config => { | ||
const oldConfig = instance.config | ||
|
||
// Update the config | ||
instance.config = config | ||
|
||
if (!isServer) { | ||
if (oldConfig?.refetchInterval === config.refetchInterval) { | ||
return | ||
} | ||
|
||
query.clearIntervals() | ||
|
||
const minInterval = Math.min( | ||
This comment has been minimized.
Sorry, something went wrong. |
||
...query.instances.map(d => d.config.refetchInterval || Infinity) | ||
) | ||
|
||
if ( | ||
!instance.refetchIntervalId && | ||
minInterval > 0 && | ||
minInterval < Infinity | ||
) { | ||
instance.refetchIntervalId = setInterval(() => { | ||
if (isDocumentVisible() || config.refetchIntervalInBackground) { | ||
This comment has been minimized.
Sorry, something went wrong.
relayrlukaszmakuch
|
||
query.fetch() | ||
} | ||
}, minInterval) | ||
} | ||
} | ||
} | ||
|
||
instance.run = async () => { | ||
|
@@ -416,6 +435,7 @@ export function makeQueryCache({ frozen = isServer, defaultConfig } = {}) { | |
query.instances = query.instances.filter(d => d.id !== instance.id) | ||
|
||
if (!query.instances.length) { | ||
query.clearIntervals() | ||
query.cancel() | ||
|
||
if (!isServer) { | ||
|
This value will be calculated and used only when an instance's config updates.
Imagine we have two components:
Component A requests data every 1 second.
Component B requests data every 5 seconds.
1 second wins. It's the interval.
Then we remove Component B.
But because Component A's config hasn't changed, the interval is not updated, and it's still 1 second.
In other words, reducing the frequency of auto refetching doesn't work.
Here's a test case that help to find this and similar issues: