-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apollo does not support pausing polling when the current browser tab is not in focus (see [feature request](apollographql/apollo-feature-requests#247). Therefore we add our own `useFocusAwareQuery` hook that wraps `useQuery` and pauses/resumes polling upon focus/blur.
- Loading branch information
1 parent
c93998c
commit 2ce2425
Showing
6 changed files
with
66 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { DocumentNode, OperationVariables, QueryHookOptions, QueryResult, TypedDocumentNode, useQuery } from "@apollo/client"; | ||
import { useEffect } from "react"; | ||
|
||
function useFocusAwareQuery<TData = any, TVariables = OperationVariables>( | ||
query: DocumentNode | TypedDocumentNode<TData, TVariables>, | ||
options?: QueryHookOptions<TData, TVariables>, | ||
): QueryResult<TData, TVariables> { | ||
const queryResult = useQuery<TData, TVariables>(query, options); | ||
|
||
const pollInterval = options?.pollInterval; | ||
const { refetch, startPolling, stopPolling } = queryResult; | ||
|
||
useEffect(() => { | ||
if (pollInterval === undefined) { | ||
return; | ||
} | ||
|
||
const handleFocus = () => { | ||
refetch(); | ||
|
||
startPolling(pollInterval); | ||
}; | ||
|
||
const handleBlur = () => { | ||
stopPolling(); | ||
}; | ||
|
||
window.addEventListener("focus", handleFocus); | ||
window.addEventListener("blur", handleBlur); | ||
|
||
return () => { | ||
window.removeEventListener("focus", handleFocus); | ||
window.removeEventListener("blur", handleBlur); | ||
}; | ||
}, [pollInterval, refetch, startPolling, stopPolling]); | ||
|
||
return queryResult; | ||
} | ||
|
||
export { useFocusAwareQuery }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters