Skip to content

Commit

Permalink
clear stale timeout when removing queries (#271)
Browse files Browse the repository at this point in the history
Closes #270
  • Loading branch information
kentcdodds authored Mar 20, 2020
1 parent df4bd3b commit 6ce2791
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/queryCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export function makeQueryCache() {
const foundQueries = findQueries(predicate, { exact })

foundQueries.forEach(query => {
clearTimeout(query.staleTimeout)
delete cache.queries[query.queryHash]
})

Expand Down Expand Up @@ -230,9 +231,7 @@ export function makeQueryCache() {
return
}
query.staleTimeout = setTimeout(() => {
if (query) {
dispatch({ type: actionMarkStale })
}
dispatch({ type: actionMarkStale })
}, query.config.staleTime)
}

Expand Down
11 changes: 11 additions & 0 deletions src/tests/queryCache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,15 @@ describe('queryCache', () => {
const data = queries.map(query => query.state.data)
expect(data).toEqual(['data1', 'data2'])
})

test('stale timeout dispatch is not called if query is no longer in the query cache', async () => {
const queryKey = 'key'
const fetchData = () => Promise.resolve('data')
await queryCache.prefetchQuery(queryKey, fetchData)
const query = queryCache.getQuery(queryKey)
expect(query.state.isStale).toBe(false)
queryCache.removeQueries(queryKey)
await sleep(50)
expect(query.state.isStale).toBe(false)
})
})

0 comments on commit 6ce2791

Please sign in to comment.