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
78 changes: 43 additions & 35 deletions src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,6 @@ export class Query<TResult, TError> {
}, this.cacheTime)
}

async refetch(
options?: RefetchOptions,
config?: QueryConfig<TResult, TError>
): Promise<TResult | undefined> {
try {
return await this.fetch(undefined, config)
} catch (error) {
if (options?.throwOnError === true) {
throw error
}
return undefined
}
}

cancel(): void {
this.cancelFetch?.()
}
Expand Down Expand Up @@ -243,7 +229,7 @@ export class Query<TResult, TError> {
)
}

onWindowFocus(): void {
async onWindowFocus(): Promise<void> {
if (
this.observers.some(
observer =>
Expand All @@ -252,12 +238,17 @@ export class Query<TResult, TError> {
observer.config.refetchOnWindowFocus
)
) {
this.fetch()
try {
await this.fetch()
} catch {
// ignore
}
}

this.continue()
}

onOnline(): void {
async onOnline(): Promise<void> {
if (
this.observers.some(
observer =>
Expand All @@ -266,8 +257,13 @@ export class Query<TResult, TError> {
observer.config.refetchOnReconnect
)
) {
this.fetch()
try {
await this.fetch()
} catch {
// ignore
}
}

this.continue()
}

Expand Down Expand Up @@ -306,6 +302,35 @@ export class Query<TResult, TError> {
this.scheduleGc()
}

async refetch(
options?: RefetchOptions,
config?: QueryConfig<TResult, TError>
): Promise<TResult | undefined> {
try {
return await this.fetch(undefined, config)
} catch (error) {
if (options?.throwOnError === true) {
throw error
}
}
}

async fetchMore(
fetchMoreVariable?: unknown,
options?: FetchMoreOptions,
config?: QueryConfig<TResult, TError>
): Promise<TResult | undefined> {
return this.fetch(
{
fetchMore: {
fetchMoreVariable,
previous: options?.previous || false,
},
},
config
)
}

async fetch(
options?: FetchOptions,
config?: QueryConfig<TResult, TError>
Expand Down Expand Up @@ -548,22 +573,6 @@ export class Query<TResult, TError> {
run()
})
}

fetchMore(
fetchMoreVariable?: unknown,
options?: FetchMoreOptions,
config?: QueryConfig<TResult, TError>
): Promise<TResult | undefined> {
return this.fetch(
{
fetchMore: {
fetchMoreVariable,
previous: options?.previous || false,
},
},
config
)
}
}

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

function getDefaultState<TResult, TError>(
Expand Down
4 changes: 2 additions & 2 deletions src/core/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ export class QueryObserver<TResult, TError> {
async fetch(): Promise<TResult | undefined> {
try {
return await this.currentQuery.fetch(undefined, this.config)
} catch (error) {
return undefined
} catch {
// ignore
}
}

Expand Down