Skip to content

Commit 4e762db

Browse files
committed
refactor: move refetch logic into observer
1 parent bc4a001 commit 4e762db

File tree

4 files changed

+50
-46
lines changed

4 files changed

+50
-46
lines changed

src/core/infiniteQueryBehavior.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function infiniteQueryBehavior<
99
return {
1010
onFetch: context => {
1111
context.queryFn = () => {
12-
const fetchMore = context.fetchOptions?.meta.fetchMore
12+
const fetchMore = context.fetchOptions?.meta?.fetchMore
1313
const pageParam = fetchMore?.pageParam
1414
const isFetchingNextPage = fetchMore?.direction === 'forward'
1515
const isFetchingPreviousPage = fetchMore?.direction === 'backward'

src/core/query.ts

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ interface QueryConfig<TData, TError, TQueryFnData> {
3434
export interface QueryState<TData = unknown, TError = unknown> {
3535
data: TData | undefined
3636
dataUpdateCount: number
37+
dataUpdatedAt: number
3738
error: TError | null
3839
errorUpdateCount: number
39-
failureCount: number
40+
errorUpdatedAt: number
41+
fetchFailureCount: number
4042
fetchMeta: any
4143
isFetching: boolean
4244
isInvalidated: boolean
4345
isPaused: boolean
4446
status: QueryStatus
45-
updatedAt: number
4647
}
4748

4849
export interface FetchContext<TData, TError, TQueryFnData> {
@@ -232,51 +233,35 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
232233
isStale(): boolean {
233234
return (
234235
this.state.isInvalidated ||
235-
!this.state.updatedAt ||
236+
!this.state.dataUpdatedAt ||
236237
this.observers.some(observer => observer.getCurrentResult().isStale)
237238
)
238239
}
239240

240241
isStaleByTime(staleTime = 0): boolean {
241242
return (
242243
this.state.isInvalidated ||
243-
!this.state.updatedAt ||
244-
!timeUntilStale(this.state.updatedAt, staleTime)
244+
!this.state.dataUpdatedAt ||
245+
!timeUntilStale(this.state.dataUpdatedAt, staleTime)
245246
)
246247
}
247248

248249
onFocus(): void {
249-
this.onExternalEvent('focus')
250-
}
250+
const observer = this.observers.find(x => x.willFetchOnWindowFocus())
251251

252-
onOnline(): void {
253-
this.onExternalEvent('online')
252+
if (observer) {
253+
observer.refetch()
254+
}
255+
256+
// Continue fetch if currently paused
257+
this.retryer?.continue()
254258
}
255259

256-
private onExternalEvent(type: 'focus' | 'online'): void {
257-
// Execute the first observer that wants to fetch on this event
258-
const fetchObserver = this.observers.find(observer => {
259-
const {
260-
enabled,
261-
refetchOnWindowFocus,
262-
refetchOnReconnect,
263-
} = observer.options
264-
265-
const { isStale } = observer.getCurrentResult()
266-
267-
return (
268-
enabled !== false &&
269-
((type === 'focus' &&
270-
(refetchOnWindowFocus === 'always' ||
271-
(refetchOnWindowFocus !== false && isStale))) ||
272-
(type === 'online' &&
273-
(refetchOnReconnect === 'always' ||
274-
(refetchOnReconnect !== false && isStale))))
275-
)
276-
})
260+
onOnline(): void {
261+
const observer = this.observers.find(x => x.willFetchOnReconnect())
277262

278-
if (fetchObserver) {
279-
fetchObserver.refetch()
263+
if (observer) {
264+
observer.refetch()
280265
}
281266

282267
// Continue fetch if currently paused
@@ -331,7 +316,7 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
331316
fetchOptions?: FetchOptions
332317
): Promise<TData> {
333318
if (this.state.isFetching)
334-
if (this.state.updatedAt && fetchOptions?.cancelRefetch) {
319+
if (this.state.dataUpdatedAt && fetchOptions?.cancelRefetch) {
335320
// Silently cancel current fetch if the user wants to cancel refetches
336321
this.cancel({ silent: true })
337322
} else if (this.promise) {
@@ -450,15 +435,16 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
450435
return {
451436
data,
452437
dataUpdateCount: 0,
438+
dataUpdatedAt: hasData ? Date.now() : 0,
453439
error: null,
454440
errorUpdateCount: 0,
455-
failureCount: 0,
441+
errorUpdatedAt: 0,
442+
fetchFailureCount: 0,
456443
fetchMeta: undefined,
457444
isFetching: false,
458445
isInvalidated: false,
459446
isPaused: false,
460447
status: hasData ? 'success' : 'idle',
461-
updatedAt: hasData ? Date.now() : 0,
462448
}
463449
}
464450

@@ -470,7 +456,7 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
470456
case 'failed':
471457
return {
472458
...state,
473-
failureCount: state.failureCount + 1,
459+
fetchFailureCount: state.fetchFailureCount + 1,
474460
}
475461
case 'pause':
476462
return {
@@ -485,7 +471,7 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
485471
case 'fetch':
486472
return {
487473
...state,
488-
failureCount: 0,
474+
fetchFailureCount: 0,
489475
fetchMeta: action.meta,
490476
isFetching: true,
491477
isPaused: false,
@@ -496,21 +482,21 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
496482
...state,
497483
data: action.data,
498484
dataUpdateCount: state.dataUpdateCount + 1,
485+
dataUpdatedAt: action.updatedAt ?? Date.now(),
499486
error: null,
500-
failureCount: 0,
487+
fetchFailureCount: 0,
501488
isFetching: false,
502489
isInvalidated: false,
503490
isPaused: false,
504491
status: 'success',
505-
updatedAt: action.updatedAt ?? Date.now(),
506492
}
507493
case 'error':
508494
const error = action.error as unknown
509495

510496
if (isCancelledError(error) && error.revert) {
511497
return {
512498
...state,
513-
failureCount: 0,
499+
fetchFailureCount: 0,
514500
isFetching: false,
515501
isPaused: false,
516502
status: state.status === 'loading' ? 'idle' : state.status,
@@ -521,7 +507,8 @@ export class Query<TData = unknown, TError = unknown, TQueryFnData = TData> {
521507
...state,
522508
error: error as TError,
523509
errorUpdateCount: state.errorUpdateCount + 1,
524-
failureCount: state.failureCount + 1,
510+
errorUpdatedAt: Date.now(),
511+
fetchFailureCount: state.fetchFailureCount + 1,
525512
isFetching: false,
526513
isPaused: false,
527514
status: 'error',

src/core/queryObserver.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,28 @@ export class QueryObserver<
9494
willFetchOnMount(): boolean {
9595
return (
9696
this.options.enabled !== false &&
97-
(!this.currentQuery.state.updatedAt ||
97+
(!this.currentQuery.state.dataUpdatedAt ||
9898
this.options.refetchOnMount === 'always' ||
9999
(this.options.refetchOnMount !== false && this.isStale()))
100100
)
101101
}
102102

103+
willFetchOnReconnect(): boolean {
104+
return (
105+
this.options.enabled !== false &&
106+
(this.options.refetchOnReconnect === 'always' ||
107+
(this.options.refetchOnReconnect !== false && this.isStale()))
108+
)
109+
}
110+
111+
willFetchOnWindowFocus(): boolean {
112+
return (
113+
this.options.enabled !== false &&
114+
(this.options.refetchOnWindowFocus === 'always' ||
115+
(this.options.refetchOnWindowFocus !== false && this.isStale()))
116+
)
117+
}
118+
103119
private willFetchOptionally(): boolean {
104120
return this.options.enabled !== false && this.isStale()
105121
}
@@ -304,10 +320,11 @@ export class QueryObserver<
304320
willFetch?: boolean
305321
): QueryObserverResult<TData, TError> {
306322
const { state } = this.currentQuery
307-
let { status, isFetching, updatedAt } = state
323+
let { isFetching, status } = state
308324
let isPreviousData = false
309325
let isPlaceholderData = false
310326
let data: TData | undefined
327+
let updatedAt = state.dataUpdatedAt
311328

312329
// Optimistically set status to loading if we will start fetching
313330
if (willFetch) {
@@ -366,7 +383,7 @@ export class QueryObserver<
366383
...getStatusProps(status),
367384
data,
368385
error: state.error,
369-
failureCount: state.failureCount,
386+
failureCount: state.fetchFailureCount,
370387
isFetched: state.dataUpdateCount > 0,
371388
isFetchedAfterMount: state.dataUpdateCount > this.initialDataUpdateCount,
372389
isFetching,

src/hydration/hydration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export function hydrate(
154154

155155
// Do not hydrate if an existing query exists with newer data
156156
if (query) {
157-
if (query.state.updatedAt < dehydratedQuery.state.updatedAt) {
157+
if (query.state.dataUpdatedAt < dehydratedQuery.state.dataUpdatedAt) {
158158
query.setState(dehydratedQuery.state)
159159
}
160160
return

0 commit comments

Comments
 (0)