-
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.
Merge pull request #856 from vivid-planet/focus-aware-polling
Focus aware query polling
- Loading branch information
Showing
8 changed files
with
215 additions
and
7 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
23 changes: 23 additions & 0 deletions
23
...in-stories/src/docs/hooks/useFocusAwarePolling/useFocusAwarePolling.stories.mdx
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,23 @@ | ||
import { Meta, Story, Canvas } from "@storybook/addon-docs"; | ||
|
||
<Meta title="Docs/Hooks/useFocusAwarePolling" /> | ||
|
||
# useFocusAwarePolling() | ||
|
||
The `useFocusAwarePolling` hook prevents a `useQuery` hook from polling when the current browser tab is not in focus. | ||
Polling will resume once the tab is back in focus. | ||
Additionally, the query will be updated immediately on focus using `refetch`. | ||
|
||
## Basic example | ||
|
||
<Canvas> | ||
<Story id="stories-hooks-usefocusawarepolling--basic-example" /> | ||
</Canvas> | ||
|
||
### Query with `skip` option | ||
|
||
When using the `useQuery` hook with the `skip` option enabled (for instance, to pause polling when a dialog is not open), add the `skip` option to `useFocusAwarePolling`. | ||
|
||
<Canvas> | ||
<Story id="stories-hooks-usefocusawarepolling--with-skip-option" /> | ||
</Canvas> |
100 changes: 100 additions & 0 deletions
100
.../admin/admin-stories/src/docs/hooks/useFocusAwarePolling/useFocusAwarePolling.stories.tsx
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,100 @@ | ||
import { gql, useQuery } from "@apollo/client"; | ||
import { useFocusAwarePolling } from "@comet/admin"; | ||
import { Pause, Play } from "@comet/admin-icons"; | ||
import { Button, Typography } from "@mui/material"; | ||
import { storiesOf } from "@storybook/react"; | ||
import * as React from "react"; | ||
|
||
import { apolloStoryDecorator } from "../../../apollo-story.decorator"; | ||
|
||
storiesOf("stories/hooks/useFocusAwarePolling", module) | ||
.addDecorator(apolloStoryDecorator(`https://api.spacex.land/graphql/`)) | ||
.add("Basic example", () => { | ||
const { data, loading, error, refetch, startPolling, stopPolling } = useQuery( | ||
gql` | ||
query LaunchesPast { | ||
launchesPastResult(limit: 1) { | ||
data { | ||
id | ||
mission_name | ||
launch_date_local | ||
} | ||
} | ||
} | ||
`, | ||
{ | ||
notifyOnNetworkStatusChange: true, // Only necessary to show loading indicator while polling | ||
}, | ||
); | ||
|
||
useFocusAwarePolling({ | ||
pollInterval: 10000, | ||
refetch, | ||
startPolling, | ||
stopPolling, | ||
}); | ||
|
||
return ( | ||
<> | ||
<Typography> | ||
Most recent launch: | ||
{loading | ||
? "Checking..." | ||
: data && ( | ||
<> | ||
{data.launchesPastResult.data[0].mission_name}, {data.launchesPastResult.data[0].launch_date_local} | ||
</> | ||
)} | ||
</Typography> | ||
{error && <Typography color="error">Error: {JSON.stringify(error)}</Typography>} | ||
</> | ||
); | ||
}) | ||
.add("With skip option", () => { | ||
const [paused, setPaused] = React.useState(false); | ||
|
||
const { data, loading, error, refetch, startPolling, stopPolling } = useQuery( | ||
gql` | ||
query LaunchesPast { | ||
launchesPastResult(limit: 1) { | ||
data { | ||
id | ||
mission_name | ||
launch_date_local | ||
} | ||
} | ||
} | ||
`, | ||
{ | ||
notifyOnNetworkStatusChange: true, // Only necessary to show loading indicator while polling | ||
skip: paused, | ||
}, | ||
); | ||
|
||
useFocusAwarePolling({ | ||
pollInterval: 10000, | ||
skip: paused, // <-- Make sure to add skip here too! | ||
refetch, | ||
startPolling, | ||
stopPolling, | ||
}); | ||
|
||
return ( | ||
<> | ||
<Typography> | ||
Most recent launch: | ||
{loading | ||
? "Checking..." | ||
: data && ( | ||
<> | ||
{data.launchesPastResult.data[0].mission_name}, {data.launchesPastResult.data[0].launch_date_local} | ||
</> | ||
)} | ||
</Typography> | ||
<Button onClick={() => setPaused(!paused)} startIcon={paused ? <Play /> : <Pause />}> | ||
{paused ? "Resume" : "Pause"} | ||
</Button> | ||
{error && <Typography color="error">Error: {JSON.stringify(error)}</Typography>} | ||
</> | ||
); | ||
}); |
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,47 @@ | ||
import { ObservableQuery, QueryHookOptions } from "@apollo/client"; | ||
import { useEffect } from "react"; | ||
|
||
type FocusAwarePollingHookOptions = Pick<ObservableQuery, "refetch" | "startPolling" | "stopPolling"> & | ||
Pick<QueryHookOptions, "pollInterval" | "skip">; | ||
|
||
function useFocusAwarePolling({ pollInterval, skip, refetch, startPolling, stopPolling }: FocusAwarePollingHookOptions): void { | ||
useEffect(() => { | ||
if (pollInterval === undefined || skip) { | ||
stopPolling(); | ||
return; | ||
} | ||
|
||
const handleFocus = () => { | ||
refetch(); | ||
|
||
startPolling(pollInterval); | ||
}; | ||
|
||
const handleBlur = () => { | ||
stopPolling(); | ||
}; | ||
|
||
window.addEventListener("focus", handleFocus); | ||
window.addEventListener("blur", handleBlur); | ||
|
||
let timeoutId: number | undefined; | ||
|
||
if (document.hasFocus()) { | ||
// Timeout to prevent duplicate initial requests as useQuery fetches as well | ||
timeoutId = window.setTimeout(() => { | ||
startPolling(pollInterval); | ||
}, pollInterval); | ||
} | ||
|
||
return () => { | ||
window.removeEventListener("focus", handleFocus); | ||
window.removeEventListener("blur", handleBlur); | ||
|
||
if (timeoutId !== undefined) { | ||
window.clearTimeout(timeoutId); | ||
} | ||
}; | ||
}, [pollInterval, skip, refetch, startPolling, stopPolling]); | ||
} | ||
|
||
export { useFocusAwarePolling }; |
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