Skip to content

Commit 8fcd002

Browse files
authored
refactor: remove gc flag (TanStack#825)
1 parent 01c5b29 commit 8fcd002

File tree

3 files changed

+24
-41
lines changed

3 files changed

+24
-41
lines changed

src/core/query.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export interface QueryState<TResult, TError> {
4343
isLoading: boolean
4444
isStale: boolean
4545
isSuccess: boolean
46-
markedForGarbageCollection: boolean
4746
status: QueryStatus
4847
throwInErrorBoundary?: boolean
4948
updatedAt: number
@@ -61,7 +60,6 @@ export interface FetchMoreOptions {
6160
enum ActionType {
6261
Failed = 'Failed',
6362
MarkStale = 'MarkStale',
64-
MarkGC = 'MarkGC',
6563
Fetch = 'Fetch',
6664
Success = 'Success',
6765
Error = 'Error',
@@ -76,10 +74,6 @@ interface MarkStaleAction {
7674
type: ActionType.MarkStale
7775
}
7876

79-
interface MarkGCAction {
80-
type: ActionType.MarkGC
81-
}
82-
8377
interface FetchAction {
8478
type: ActionType.Fetch
8579
}
@@ -105,7 +99,6 @@ type Action<TResult, TError> =
10599
| ErrorAction<TError>
106100
| FailedAction
107101
| FetchAction
108-
| MarkGCAction
109102
| MarkStaleAction
110103
| SetStateAction<TResult, TError>
111104
| SuccessAction<TResult>
@@ -220,14 +213,9 @@ export class Query<TResult, TError> {
220213
return
221214
}
222215

223-
this.dispatch({ type: ActionType.MarkGC })
224-
225216
this.cacheTimeout = setTimeout(
226217
() => {
227-
this.queryCache.removeQueries(
228-
d =>
229-
d.state.markedForGarbageCollection && d.queryHash === this.queryHash
230-
)
218+
this.clear()
231219
},
232220
typeof this.state.data === 'undefined' &&
233221
this.state.status !== QueryStatus.Error
@@ -612,7 +600,6 @@ function getDefaultState<TResult, TError>(
612600
isFetching: initialStatus === QueryStatus.Loading,
613601
failureCount: 0,
614602
isStale,
615-
markedForGarbageCollection: false,
616603
data: initialData,
617604
updatedAt: hasInitialData ? Date.now() : 0,
618605
}
@@ -633,12 +620,6 @@ export function queryReducer<TResult, TError>(
633620
...state,
634621
isStale: true,
635622
}
636-
case ActionType.MarkGC: {
637-
return {
638-
...state,
639-
markedForGarbageCollection: true,
640-
}
641-
}
642623
case ActionType.Fetch:
643624
const status =
644625
typeof state.data !== 'undefined'

src/react/tests/useInfiniteQuery.test.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ describe('useInfiniteQuery', () => {
203203
})
204204

205205
it('should compute canFetchMore correctly for falsy getFetchMore return value', async () => {
206+
const key = queryKey()
207+
206208
function Page() {
207209
const fetchCountRef = React.useRef(0)
208210
const {
@@ -215,7 +217,7 @@ describe('useInfiniteQuery', () => {
215217
canFetchMore,
216218
refetch,
217219
} = useInfiniteQuery<Result, Error, string>(
218-
'items',
220+
key,
219221
(_key, nextId = 0) => fetchItems(nextId, fetchCountRef.current++),
220222
{
221223
getFetchMore: (_lastGroup, _allGroups) => undefined,
@@ -374,6 +376,8 @@ describe('useInfiniteQuery', () => {
374376
})
375377

376378
it('should compute canFetchMore correctly for falsy getFetchMore return value using initialData', async () => {
379+
const key = queryKey()
380+
377381
function Page() {
378382
const fetchCountRef = React.useRef(0)
379383
const {
@@ -386,7 +390,7 @@ describe('useInfiniteQuery', () => {
386390
canFetchMore,
387391
refetch,
388392
} = useInfiniteQuery<Result, Error, string>(
389-
'items',
393+
key,
390394
(_key, nextId = 0) => fetchItems(nextId, fetchCountRef.current++),
391395
{
392396
initialData: [initialItems(0)],

src/react/tests/useQuery.test.tsx

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -537,18 +537,16 @@ describe('useQuery', () => {
537537
const key = queryKey()
538538

539539
const queryFn = jest.fn()
540-
queryFn.mockImplementation(() => sleep(10).then(() => 'data'))
540+
queryFn.mockImplementation(() => 'data')
541541

542542
const prefetchQueryFn = jest.fn()
543-
prefetchQueryFn.mockImplementation(() => sleep(16).then(() => 'not yet...'))
543+
prefetchQueryFn.mockImplementation(() => 'not yet...')
544544

545-
await act(() =>
546-
queryCache.prefetchQuery(key, prefetchQueryFn, {
547-
staleTime: 10,
548-
})
549-
)
545+
await queryCache.prefetchQuery(key, prefetchQueryFn, {
546+
staleTime: 10,
547+
})
550548

551-
await act(() => sleep(20))
549+
await sleep(100)
552550

553551
function Page() {
554552
const query = useQuery(key, queryFn)
@@ -560,9 +558,9 @@ describe('useQuery', () => {
560558
)
561559
}
562560

563-
const rendered = render(<Page />)
561+
render(<Page />)
564562

565-
await waitFor(() => rendered.getByText('data'))
563+
await sleep(100)
566564

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

574572
const queryFn = jest.fn()
575-
queryFn.mockImplementation(() => sleep(10).then(() => 'data'))
573+
queryFn.mockImplementation(() => 'data')
576574

577575
const prefetchQueryFn = jest.fn()
578-
prefetchQueryFn.mockImplementation(() => sleep(16).then(() => 'not yet...'))
576+
prefetchQueryFn.mockImplementation(() => 'not yet...')
579577

580-
await act(() =>
581-
queryCache.prefetchQuery(key, prefetchQueryFn, {
582-
staleTime: 1000,
583-
})
584-
)
578+
await queryCache.prefetchQuery(key, prefetchQueryFn, {
579+
staleTime: 1000,
580+
})
581+
582+
sleep(100)
585583

586584
function Page() {
587585
const query = useQuery(key, queryFn)
@@ -593,9 +591,9 @@ describe('useQuery', () => {
593591
)
594592
}
595593

596-
const rendered = render(<Page />)
594+
render(<Page />)
597595

598-
await waitFor(() => rendered.getByText('data'))
596+
sleep(100)
599597

600598
expect(prefetchQueryFn).toHaveBeenCalledTimes(1)
601599
expect(queryFn).toHaveBeenCalledTimes(0)

0 commit comments

Comments
 (0)