Skip to content

Commit 380a049

Browse files
authored
refactor: remove query status bools (#1009)
1 parent 065ffe0 commit 380a049

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

src/core/query.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
Console,
44
Updater,
55
functionalUpdate,
6-
getStatusProps,
76
isCancelable,
87
isCancelledError,
98
isDocumentVisible,
@@ -32,13 +31,9 @@ export interface QueryState<TResult, TError> {
3231
data?: TResult
3332
error: TError | null
3433
failureCount: number
35-
isError: boolean
3634
isFetching: boolean
3735
isFetchingMore: IsFetchingMoreValue
38-
isIdle: boolean
3936
isInitialData: boolean
40-
isLoading: boolean
41-
isSuccess: boolean
4237
status: QueryStatus
4338
throwInErrorBoundary?: boolean
4439
updateCount: number
@@ -231,7 +226,8 @@ export class Query<TResult, TError> {
231226

232227
isStaleByTime(staleTime = 0): boolean {
233228
return (
234-
!this.state.isSuccess || this.state.updatedAt + staleTime <= Date.now()
229+
this.state.status !== QueryStatus.Success ||
230+
this.state.updatedAt + staleTime <= Date.now()
235231
)
236232
}
237233

@@ -578,28 +574,27 @@ function hasMorePages<TResult, TError>(
578574
function getDefaultState<TResult, TError>(
579575
config: ResolvedQueryConfig<TResult, TError>
580576
): QueryState<TResult, TError> {
581-
const initialData =
577+
const data =
582578
typeof config.initialData === 'function'
583579
? (config.initialData as InitialDataFunction<TResult>)()
584580
: config.initialData
585581

586-
const hasInitialData = typeof initialData !== 'undefined'
587-
588-
const initialStatus = hasInitialData
589-
? QueryStatus.Success
590-
: config.enabled
591-
? QueryStatus.Loading
592-
: QueryStatus.Idle
582+
const status =
583+
typeof data !== 'undefined'
584+
? QueryStatus.Success
585+
: config.enabled
586+
? QueryStatus.Loading
587+
: QueryStatus.Idle
593588

594589
return {
595-
...getStatusProps(initialStatus),
596-
canFetchMore: hasMorePages(config, initialData),
597-
data: initialData,
590+
canFetchMore: hasMorePages(config, data),
591+
data,
598592
error: null,
599593
failureCount: 0,
600-
isFetching: initialStatus === QueryStatus.Loading,
594+
isFetching: status === QueryStatus.Loading,
601595
isFetchingMore: false,
602596
isInitialData: true,
597+
status,
603598
updateCount: 0,
604599
updatedAt: Date.now(),
605600
}
@@ -616,39 +611,38 @@ export function queryReducer<TResult, TError>(
616611
failureCount: state.failureCount + 1,
617612
}
618613
case ActionType.Fetch:
619-
const status =
620-
typeof state.data !== 'undefined'
621-
? QueryStatus.Success
622-
: QueryStatus.Loading
623614
return {
624615
...state,
625-
...getStatusProps(status),
616+
failureCount: 0,
626617
isFetching: true,
627618
isFetchingMore: action.isFetchingMore || false,
628-
failureCount: 0,
619+
status:
620+
typeof state.data !== 'undefined'
621+
? QueryStatus.Success
622+
: QueryStatus.Loading,
629623
}
630624
case ActionType.Success:
631625
return {
632626
...state,
633-
...getStatusProps(QueryStatus.Success),
634627
canFetchMore: action.canFetchMore,
635628
data: action.data,
636629
error: null,
637630
failureCount: 0,
638631
isFetching: false,
639632
isFetchingMore: false,
640633
isInitialData: false,
634+
status: QueryStatus.Success,
641635
updateCount: state.updateCount + 1,
642636
updatedAt: action.updatedAt ?? Date.now(),
643637
}
644638
case ActionType.Error:
645639
return {
646640
...state,
647-
...getStatusProps(QueryStatus.Error),
648641
error: action.error,
649642
failureCount: state.failureCount + 1,
650643
isFetching: false,
651644
isFetchingMore: false,
645+
status: QueryStatus.Error,
652646
throwInErrorBoundary: true,
653647
updateCount: state.updateCount + 1,
654648
}

src/core/queryObserver.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
isValidTimeout,
66
noop,
77
} from './utils'
8-
import type { QueryResult, ResolvedQueryConfig } from './types'
8+
import { QueryResult, ResolvedQueryConfig, QueryStatus } from './types'
99
import type { Query, Action, FetchMoreOptions, RefetchOptions } from './query'
1010

1111
export type UpdateListener<TResult, TError> = (
@@ -216,8 +216,9 @@ export class QueryObserver<TResult, TError> {
216216
// Keep previous data if needed
217217
if (
218218
config.keepPreviousData &&
219-
(state.isIdle || state.isLoading) &&
220-
previousQueryResult?.isSuccess
219+
(state.status === QueryStatus.Idle ||
220+
state.status === QueryStatus.Loading) &&
221+
previousQueryResult?.status === QueryStatus.Success
221222
) {
222223
data = previousQueryResult.data
223224
updatedAt = previousQueryResult.updatedAt

src/core/tests/queryCache.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ describe('queryCache', () => {
543543

544544
expect(query.state).toMatchObject({
545545
data: 'data',
546-
isLoading: false,
546+
status: 'success',
547547
updateCount: 1,
548548
})
549549
})
@@ -577,7 +577,7 @@ describe('queryCache', () => {
577577
expect(cancel).toHaveBeenCalled()
578578
expect(query.state).toMatchObject({
579579
data: undefined,
580-
isLoading: false,
580+
status: 'error',
581581
updateCount: 1,
582582
})
583583
})

0 commit comments

Comments
 (0)