Skip to content
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(scheduleGarbageCollection): Do not garbage collect newly cached queries of the same hash #332

Merged
merged 7 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Store when the GC was initiated and compare against updatedAt
  • Loading branch information
kamranayub committed Apr 8, 2020
commit e30cf94654201786b1929d5adf049809584117b1
5 changes: 4 additions & 1 deletion src/queryCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,12 @@ export function makeQueryCache() {
}

query.scheduleGarbageCollection = () => {
const scheduledAt = Date.now()
query.cacheTimeout = setTimeout(
() => {
cache.removeQueries(d => d.queryHash === query.queryHash)
cache.removeQueries(
d => d.state.updatedAt < scheduledAt && d.queryHash === query.queryHash
)
},
typeof query.state.data === 'undefined' &&
query.state.status !== 'error'
Expand Down
3 changes: 1 addition & 2 deletions src/tests/cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const PostsByUser = () => {
return null
}

export const Page = ({ onAfterHide }) => {
export const Page = () => {
const [showContent, setShowContent] = React.useState(false)

return (
Expand All @@ -59,7 +59,6 @@ export const Page = ({ onAfterHide }) => {
data-testid="hideContent"
onClick={() => {
setShowContent(false)
if (onAfterHide) onAfterHide()
}}
>
Close Modal
Expand Down
26 changes: 5 additions & 21 deletions src/tests/cleanup.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import React from 'react'
import { render, fireEvent } from '@testing-library/react'
import { cleanup } from '@testing-library/react/pure'
import { queryCache } from '..'
import { Page } from './cleanup'

describe('query cleanup', () => {
afterEach(() => {
queryCache.clear()
// This will make things pass as it force-flushes the microtask queue twice
// but it's a bandaid over an underlying issue
// await cleanup()
})

test('should prefetchQuery by force for first query', async () => {
await queryCache.prefetchQuery(
['user', 1],
() => Promise.resolve({ userId: 1, username: 'Mock User' }),
{ force: true }
)
test('should set query data on first test', async () => {
queryCache.setQueryData(['user', 1], { userId: 1, username: 'Mock User' })

const { findByTestId, findByText } = render(<Page />)

Expand All @@ -30,17 +22,9 @@ describe('query cleanup', () => {
expect(userProfile).toBeDefined()
})

test('should prefetch by force for both queries', async () => {
await queryCache.prefetchQuery(
['user', 1],
() => Promise.resolve({ userId: 1, username: 'Mock User' }),
{ force: true }
)
await queryCache.prefetchQuery(
['posts', 1],
() => Promise.resolve([{ postId: 1, body: 'World' }]),
{ force: true }
)
test('should not garbage collect new initial data', async () => {
queryCache.setQueryData(['user', 1], { userId: 1, username: 'Mock 2 User' })
queryCache.setQueryData(['posts', 1], [{ postId: 1, body: 'World' }])

const { findByTestId, findByText } = render(<Page />)

Expand Down