Skip to content

Commit

Permalink
fix: allow queries to be fetched with zero cache time and do not hydr…
Browse files Browse the repository at this point in the history
…ate it (#1442)
  • Loading branch information
boschni authored Dec 16, 2020
1 parent 73341ba commit eb301fc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 35 deletions.
16 changes: 13 additions & 3 deletions src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,7 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {

if (isValidTimeout(this.cacheTime)) {
this.gcTimeout = setTimeout(() => {
if (!this.observers.length) {
this.cache.remove(this)
}
this.optionalRemove()
}, this.cacheTime)
}
}
Expand All @@ -180,6 +178,12 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
this.gcTimeout = undefined
}

private optionalRemove() {
if (!this.observers.length && !this.state.isFetching) {
this.cache.remove(this)
}
}

setData(
updater: Updater<TData | undefined, TData>,
options?: SetDataOptions
Expand Down Expand Up @@ -412,6 +416,12 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
// Propagate error
throw error
})
.finally(() => {
// Remove query after fetching if cache time is 0
if (this.cacheTime === 0) {
this.optionalRemove()
}
})

return this.promise
}
Expand Down
36 changes: 36 additions & 0 deletions src/core/tests/queryCache.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,42 @@ describe('queryCache', () => {
expect(second).toBe(first)
})

test('fetchQuery should be able to fetch when cache time is set to 0 and then be removed', async () => {
const testClient = new QueryClient({
defaultOptions: { queries: { cacheTime: 0 } },
})

const key1 = queryKey()

const result = await testClient.fetchQuery(key1, async () => {
await sleep(10)
return 1
})

const result2 = testClient.getQueryData(key1)

expect(result).toEqual(1)
expect(result2).toEqual(undefined)
})

test('fetchQuery should keep a query in cache if cache time is Infinity', async () => {
const testClient = new QueryClient({
defaultOptions: { queries: { cacheTime: Infinity } },
})

const key1 = queryKey()

const result = await testClient.fetchQuery(key1, async () => {
await sleep(10)
return 1
})

const result2 = testClient.getQueryData(key1)

expect(result).toEqual(1)
expect(result2).toEqual(1)
})

test('fetchQuery should not force fetch', async () => {
const key = queryKey()

Expand Down
11 changes: 0 additions & 11 deletions src/hydration/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ interface DehydratedMutation {
}

interface DehydratedQuery {
cacheTime: number
queryHash: string
queryKey: QueryKey
state: QueryState
Expand All @@ -47,14 +46,6 @@ export type ShouldDehydrateMutationFunction = (mutation: Mutation) => boolean

// FUNCTIONS

function serializePositiveNumber(value: number): number {
return value === Infinity ? -1 : value
}

function deserializePositiveNumber(value: number): number {
return value === -1 ? Infinity : value
}

function dehydrateMutation(mutation: Mutation): DehydratedMutation {
return {
mutationKey: mutation.options.mutationKey,
Expand All @@ -68,7 +59,6 @@ function dehydrateMutation(mutation: Mutation): DehydratedMutation {
// in the html-payload, but not consume it on the initial render.
function dehydrateQuery(query: Query): DehydratedQuery {
return {
cacheTime: serializePositiveNumber(query.cacheTime),
state: query.state,
queryKey: query.queryKey,
queryHash: query.queryHash,
Expand Down Expand Up @@ -167,7 +157,6 @@ export function hydrate(
...options?.defaultOptions?.queries,
queryKey: dehydratedQuery.queryKey,
queryHash: dehydratedQuery.queryHash,
cacheTime: deserializePositiveNumber(dehydratedQuery.cacheTime),
},
dehydratedQuery.state
)
Expand Down
23 changes: 2 additions & 21 deletions src/hydration/tests/hydration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('dehydration and rehydration', () => {
hydrationClient.clear()
})

test('should schedule garbage collection, measured from hydration', async () => {
test('should use the cache time from the client', async () => {
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })
await queryClient.prefetchQuery('string', () => fetchData('string'), {
Expand All @@ -80,28 +80,9 @@ describe('dehydration and rehydration', () => {
const hydrationClient = new QueryClient({ queryCache: hydrationCache })
hydrate(hydrationClient, parsed)
expect(hydrationCache.find('string')?.state.data).toBe('string')
await sleep(10)
expect(hydrationCache.find('string')).toBeTruthy()
await sleep(100)
expect(hydrationCache.find('string')).toBeFalsy()

queryClient.clear()
hydrationClient.clear()
})
expect(hydrationCache.find('string')).toBeTruthy()

test('should serialize the cacheTime correctly', async () => {
const queryCache = new QueryCache()
const queryClient = new QueryClient({ queryCache })
await queryClient.prefetchQuery('string', () => fetchData('string'), {
cacheTime: Infinity,
})
const dehydrated = dehydrate(queryClient)
const stringified = JSON.stringify(dehydrated)
const parsed = JSON.parse(stringified)
const hydrationCache = new QueryCache()
const hydrationClient = new QueryClient({ queryCache: hydrationCache })
hydrate(hydrationClient, parsed)
expect(hydrationCache.find('string')?.cacheTime).toBe(Infinity)
queryClient.clear()
hydrationClient.clear()
})
Expand Down

1 comment on commit eb301fc

@vercel
Copy link

@vercel vercel bot commented on eb301fc Dec 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.