Skip to content

Commit 445ae1f

Browse files
authored
fix: ignore errors from background fetches (#953)
1 parent 5c14392 commit 445ae1f

File tree

2 files changed

+45
-37
lines changed

2 files changed

+45
-37
lines changed

src/core/query.ts

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,6 @@ export class Query<TResult, TError> {
157157
}, this.cacheTime)
158158
}
159159

160-
async refetch(
161-
options?: RefetchOptions,
162-
config?: QueryConfig<TResult, TError>
163-
): Promise<TResult | undefined> {
164-
try {
165-
return await this.fetch(undefined, config)
166-
} catch (error) {
167-
if (options?.throwOnError === true) {
168-
throw error
169-
}
170-
return undefined
171-
}
172-
}
173-
174160
cancel(): void {
175161
this.cancelFetch?.()
176162
}
@@ -243,7 +229,7 @@ export class Query<TResult, TError> {
243229
)
244230
}
245231

246-
onWindowFocus(): void {
232+
async onWindowFocus(): Promise<void> {
247233
if (
248234
this.observers.some(
249235
observer =>
@@ -252,12 +238,17 @@ export class Query<TResult, TError> {
252238
observer.config.refetchOnWindowFocus
253239
)
254240
) {
255-
this.fetch()
241+
try {
242+
await this.fetch()
243+
} catch {
244+
// ignore
245+
}
256246
}
247+
257248
this.continue()
258249
}
259250

260-
onOnline(): void {
251+
async onOnline(): Promise<void> {
261252
if (
262253
this.observers.some(
263254
observer =>
@@ -266,8 +257,13 @@ export class Query<TResult, TError> {
266257
observer.config.refetchOnReconnect
267258
)
268259
) {
269-
this.fetch()
260+
try {
261+
await this.fetch()
262+
} catch {
263+
// ignore
264+
}
270265
}
266+
271267
this.continue()
272268
}
273269

@@ -306,6 +302,35 @@ export class Query<TResult, TError> {
306302
this.scheduleGc()
307303
}
308304

305+
async refetch(
306+
options?: RefetchOptions,
307+
config?: QueryConfig<TResult, TError>
308+
): Promise<TResult | undefined> {
309+
try {
310+
return await this.fetch(undefined, config)
311+
} catch (error) {
312+
if (options?.throwOnError === true) {
313+
throw error
314+
}
315+
}
316+
}
317+
318+
async fetchMore(
319+
fetchMoreVariable?: unknown,
320+
options?: FetchMoreOptions,
321+
config?: QueryConfig<TResult, TError>
322+
): Promise<TResult | undefined> {
323+
return this.fetch(
324+
{
325+
fetchMore: {
326+
fetchMoreVariable,
327+
previous: options?.previous || false,
328+
},
329+
},
330+
config
331+
)
332+
}
333+
309334
async fetch(
310335
options?: FetchOptions,
311336
config?: QueryConfig<TResult, TError>
@@ -548,22 +573,6 @@ export class Query<TResult, TError> {
548573
run()
549574
})
550575
}
551-
552-
fetchMore(
553-
fetchMoreVariable?: unknown,
554-
options?: FetchMoreOptions,
555-
config?: QueryConfig<TResult, TError>
556-
): Promise<TResult | undefined> {
557-
return this.fetch(
558-
{
559-
fetchMore: {
560-
fetchMoreVariable,
561-
previous: options?.previous || false,
562-
},
563-
},
564-
config
565-
)
566-
}
567576
}
568577

569578
function getLastPage<TResult>(pages: TResult[], previous?: boolean): TResult {
@@ -578,7 +587,6 @@ function hasMorePages<TResult, TError>(
578587
if (config.infinite && config.getFetchMore && Array.isArray(pages)) {
579588
return Boolean(config.getFetchMore(getLastPage(pages, previous), pages))
580589
}
581-
return undefined
582590
}
583591

584592
function getDefaultState<TResult, TError>(

src/core/queryObserver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ export class QueryObserver<TResult, TError> {
122122
async fetch(): Promise<TResult | undefined> {
123123
try {
124124
return await this.currentQuery.fetch(undefined, this.config)
125-
} catch (error) {
126-
return undefined
125+
} catch {
126+
// ignore
127127
}
128128
}
129129

0 commit comments

Comments
 (0)