Skip to content

Commit

Permalink
feat: Add focusPolling option to disable refetching if tab is inactive
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-kumawat committed Nov 29, 2023
1 parent 89ec18e commit b920b5a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/core/ObservableQuery.ts
Expand Up @@ -776,7 +776,10 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`,

const maybeFetch = () => {
if (this.pollingInfo) {
if (!isNetworkRequestInFlight(this.queryInfo.networkStatus)) {
if (
!isNetworkRequestInFlight(this.queryInfo.networkStatus) &&
(!this.options.focusPolling || document.hasFocus())
) {
this.reobserve(
{
// Most fetchPolicy options don't make sense to use in a polling context, as
Expand Down
6 changes: 6 additions & 0 deletions src/core/watchQueryOptions.ts
Expand Up @@ -176,6 +176,12 @@ export interface WatchQueryOptions<

/** {@inheritDoc @apollo/client!QueryOptions#canonizeResults:member} */
canonizeResults?: boolean;

/**
* Specifies if refetching of query should be enabled if window is focused, ie,
* `document.hasFocused()` returns `true`.
*/
focusPolling?: boolean;
}

export interface NextFetchPolicyContext<
Expand Down

0 comments on commit b920b5a

Please sign in to comment.