From b9168b83f1c10763d29d730a6f3527bcb930ebad Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Thu, 18 Jun 2020 16:45:22 -0600 Subject: [PATCH] fix: different refetchIntervals per instance use the min --- examples/auto-refetching/pages/index.js | 9 +++- src/queryCache.js | 56 +++++++++++++++++-------- 2 files changed, 46 insertions(+), 19 deletions(-) diff --git a/examples/auto-refetching/pages/index.js b/examples/auto-refetching/pages/index.js index 1169011a5f..6ef3797669 100755 --- a/examples/auto-refetching/pages/index.js +++ b/examples/auto-refetching/pages/index.js @@ -5,7 +5,7 @@ import axios from 'axios' import { useQuery, useMutation, queryCache } from 'react-query' -export default () => { +function App() { const [intervalMs, setIntervalMs] = React.useState(1000) const [value, setValue] = React.useState('') @@ -91,3 +91,10 @@ export default () => { ) } + +export default () => ( + <> + + + +) diff --git a/src/queryCache.js b/src/queryCache.js index 14e69b9ba8..ec544e0e1b 100644 --- a/src/queryCache.js +++ b/src/queryCache.js @@ -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,8 +332,6 @@ export function makeQueryCache({ frozen = isServer, defaultConfig } = {}) { query.cancel = () => { query.cancelled = cancelledError - query.cancelInterval() - if (query.cancelPromises) { query.cancelPromises() } @@ -353,10 +339,10 @@ export function makeQueryCache({ frozen = isServer, defaultConfig } = {}) { 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( + ...query.instances.map(d => d.config.refetchInterval || Infinity) + ) + + if ( + !instance.refetchIntervalId && + minInterval > 0 && + minInterval < Infinity + ) { + instance.refetchIntervalId = setInterval(() => { + if (isDocumentVisible() || config.refetchIntervalInBackground) { + 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) {