Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 1 addition & 20 deletions src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export interface QueryState<TResult, TError> {
isLoading: boolean
isStale: boolean
isSuccess: boolean
markedForGarbageCollection: boolean
status: QueryStatus
throwInErrorBoundary?: boolean
updatedAt: number
Expand All @@ -61,7 +60,6 @@ export interface FetchMoreOptions {
enum ActionType {
Failed = 'Failed',
MarkStale = 'MarkStale',
MarkGC = 'MarkGC',
Fetch = 'Fetch',
Success = 'Success',
Error = 'Error',
Expand All @@ -76,10 +74,6 @@ interface MarkStaleAction {
type: ActionType.MarkStale
}

interface MarkGCAction {
type: ActionType.MarkGC
}

interface FetchAction {
type: ActionType.Fetch
}
Expand All @@ -105,7 +99,6 @@ type Action<TResult, TError> =
| ErrorAction<TError>
| FailedAction
| FetchAction
| MarkGCAction
| MarkStaleAction
| SetStateAction<TResult, TError>
| SuccessAction<TResult>
Expand Down Expand Up @@ -220,14 +213,9 @@ export class Query<TResult, TError> {
return
}

this.dispatch({ type: ActionType.MarkGC })

this.cacheTimeout = setTimeout(
() => {
this.queryCache.removeQueries(
d =>
d.state.markedForGarbageCollection && d.queryHash === this.queryHash
)
this.clear()
},
typeof this.state.data === 'undefined' &&
this.state.status !== QueryStatus.Error
Expand Down Expand Up @@ -612,7 +600,6 @@ function getDefaultState<TResult, TError>(
isFetching: initialStatus === QueryStatus.Loading,
failureCount: 0,
isStale,
markedForGarbageCollection: false,
data: initialData,
updatedAt: hasInitialData ? Date.now() : 0,
}
Expand All @@ -633,12 +620,6 @@ export function queryReducer<TResult, TError>(
...state,
isStale: true,
}
case ActionType.MarkGC: {
return {
...state,
markedForGarbageCollection: true,
}
}
case ActionType.Fetch:
const status =
typeof state.data !== 'undefined'
Expand Down
8 changes: 6 additions & 2 deletions src/react/tests/useInfiniteQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ describe('useInfiniteQuery', () => {
})

it('should compute canFetchMore correctly for falsy getFetchMore return value', async () => {
const key = queryKey()

function Page() {
const fetchCountRef = React.useRef(0)
const {
Expand All @@ -215,7 +217,7 @@ describe('useInfiniteQuery', () => {
canFetchMore,
refetch,
} = useInfiniteQuery<Result, Error, string>(
'items',
key,
(_key, nextId = 0) => fetchItems(nextId, fetchCountRef.current++),
{
getFetchMore: (_lastGroup, _allGroups) => undefined,
Expand Down Expand Up @@ -374,6 +376,8 @@ describe('useInfiniteQuery', () => {
})

it('should compute canFetchMore correctly for falsy getFetchMore return value using initialData', async () => {
const key = queryKey()

function Page() {
const fetchCountRef = React.useRef(0)
const {
Expand All @@ -386,7 +390,7 @@ describe('useInfiniteQuery', () => {
canFetchMore,
refetch,
} = useInfiniteQuery<Result, Error, string>(
'items',
key,
(_key, nextId = 0) => fetchItems(nextId, fetchCountRef.current++),
{
initialData: [initialItems(0)],
Expand Down
36 changes: 17 additions & 19 deletions src/react/tests/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -537,18 +537,16 @@ describe('useQuery', () => {
const key = queryKey()

const queryFn = jest.fn()
queryFn.mockImplementation(() => sleep(10).then(() => 'data'))
queryFn.mockImplementation(() => 'data')

const prefetchQueryFn = jest.fn()
prefetchQueryFn.mockImplementation(() => sleep(16).then(() => 'not yet...'))
prefetchQueryFn.mockImplementation(() => 'not yet...')

await act(() =>
queryCache.prefetchQuery(key, prefetchQueryFn, {
staleTime: 10,
})
)
await queryCache.prefetchQuery(key, prefetchQueryFn, {
staleTime: 10,
})

await act(() => sleep(20))
await sleep(100)

function Page() {
const query = useQuery(key, queryFn)
Expand All @@ -560,9 +558,9 @@ describe('useQuery', () => {
)
}

const rendered = render(<Page />)
render(<Page />)

await waitFor(() => rendered.getByText('data'))
await sleep(100)

expect(prefetchQueryFn).toHaveBeenCalledTimes(1)
expect(queryFn).toHaveBeenCalledTimes(1)
Expand All @@ -572,16 +570,16 @@ describe('useQuery', () => {
const key = queryKey()

const queryFn = jest.fn()
queryFn.mockImplementation(() => sleep(10).then(() => 'data'))
queryFn.mockImplementation(() => 'data')

const prefetchQueryFn = jest.fn()
prefetchQueryFn.mockImplementation(() => sleep(16).then(() => 'not yet...'))
prefetchQueryFn.mockImplementation(() => 'not yet...')

await act(() =>
queryCache.prefetchQuery(key, prefetchQueryFn, {
staleTime: 1000,
})
)
await queryCache.prefetchQuery(key, prefetchQueryFn, {
staleTime: 1000,
})

sleep(100)

function Page() {
const query = useQuery(key, queryFn)
Expand All @@ -593,9 +591,9 @@ describe('useQuery', () => {
)
}

const rendered = render(<Page />)
render(<Page />)

await waitFor(() => rendered.getByText('data'))
sleep(100)

expect(prefetchQueryFn).toHaveBeenCalledTimes(1)
expect(queryFn).toHaveBeenCalledTimes(0)
Expand Down