Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Focus aware query polling #856

Merged
merged 5 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ All notable changes to this project will be documented in this file. This projec

- Add scope support for redirects (`@comet/cms-api` and `@comet/cms-admin`)

### @comet/admin

#### Changes

- Add `useFocusAwarePolling` hook that can be used in combination with `useQuery` and only fetches when the current browser tab is focused
johnnyomair marked this conversation as resolved.
Show resolved Hide resolved

## 3.0.0

_Oct 17, 2022_
Expand Down
56 changes: 56 additions & 0 deletions packages/admin/admin/src/apollo/useFocusAwarePolling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { ObservableQuery, OperationVariables, QueryHookOptions } from "@apollo/client";
import { useEffect } from "react";

type FocusAwarePollingHookOptions<TData = any, TVariables = OperationVariables> = Pick<
ObservableQuery<TData, TVariables>,
"refetch" | "startPolling" | "stopPolling"
> &
Pick<QueryHookOptions<TData, TVariables>, "pollInterval" | "skip">;

function useFocusAwarePolling<TData = any, TVariables = OperationVariables>({
johnnyomair marked this conversation as resolved.
Show resolved Hide resolved
pollInterval,
skip,
refetch,
startPolling,
stopPolling,
}: FocusAwarePollingHookOptions<TData, TVariables>): 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 };
1 change: 1 addition & 0 deletions packages/admin/admin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { useFocusAwarePolling } from "./apollo/useFocusAwarePolling";
export { AppHeader, AppHeaderClassKey } from "./appHeader/AppHeader";
export { AppHeaderButton, AppHeaderButtonProps } from "./appHeader/button/AppHeaderButton";
export { AppHeaderButtonClassKey } from "./appHeader/button/AppHeaderButton.styles";
Expand Down
13 changes: 10 additions & 3 deletions packages/admin/cms-admin/src/builds/BuildEntry.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { gql, useQuery } from "@apollo/client";
import { LocalErrorScopeApolloContext } from "@comet/admin";
import { LocalErrorScopeApolloContext, useFocusAwarePolling } from "@comet/admin";
import { SsgRunning, SsgStandby } from "@comet/admin-icons";
import { List, ListItem, Typography } from "@mui/material";
import { styled } from "@mui/material/styles";
Expand Down Expand Up @@ -104,13 +104,20 @@ const BuildStatusPopperContent: React.FunctionComponent<{ data: GQLBuildStatusQu
};

export function BuildEntry(): React.ReactElement {
const { data, error } = useQuery<GQLBuildStatusQuery>(buildStatusQuery, {
pollInterval: process.env.NODE_ENV === "production" ? 10000 : undefined,
const { data, error, refetch, startPolling, stopPolling } = useQuery<GQLBuildStatusQuery>(buildStatusQuery, {
skip: process.env.NODE_ENV === "development",
fetchPolicy: "network-only",
context: LocalErrorScopeApolloContext,
});

useFocusAwarePolling({
pollInterval: process.env.NODE_ENV === "production" ? 10000 : undefined,
skip: process.env.NODE_ENV === "development",
refetch,
startPolling,
stopPolling,
});

const running = data?.builds[0]?.status === "active" || data?.builds[0]?.status === "pending";

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { gql, useQuery } from "@apollo/client";
import { Toolbar, ToolbarActions, ToolbarFillSpace } from "@comet/admin";
import { Toolbar, ToolbarActions, ToolbarFillSpace, useFocusAwarePolling } from "@comet/admin";
import { ArrowRight, Close, Delete } from "@comet/admin-icons";
import { Button, Dialog, DialogActions, DialogContent, DialogTitle, IconButton, MenuItem, Select } from "@mui/material";
import { styled } from "@mui/material/styles";
Expand Down Expand Up @@ -82,13 +82,20 @@ export default function PageTreeSelectDialog({ value, onChange, open, onClose, d
const classes = useStyles();

// Fetch data
const { data } = useQuery<GQLPagesQuery, GQLPagesQueryVariables>(pagesQuery, {
const { data, refetch, startPolling, stopPolling } = useQuery<GQLPagesQuery, GQLPagesQueryVariables>(pagesQuery, {
variables: {
contentScope: scope,
category,
},
skip: !open,
});

useFocusAwarePolling({
pollInterval: process.env.NODE_ENV === "development" ? undefined : 10000,
skip: !open,
refetch,
startPolling,
stopPolling,
});

// Exclude all archived pages from selectables, except if the selected page itself is archived
Expand Down
21 changes: 19 additions & 2 deletions packages/admin/cms-admin/src/pages/pagesPage/PagesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { useQuery } from "@apollo/client";
import { MainContent, messages, Stack, StackPage, StackSwitch, Toolbar, ToolbarActions, useEditDialog, useStoredState } from "@comet/admin";
import {
MainContent,
messages,
Stack,
StackPage,
StackSwitch,
Toolbar,
ToolbarActions,
useEditDialog,
useFocusAwarePolling,
useStoredState,
} from "@comet/admin";
import { Add } from "@comet/admin-icons";
import { Box, Button, CircularProgress, FormControlLabel, Paper, Switch } from "@mui/material";
import withStyles from "@mui/styles/withStyles";
Expand Down Expand Up @@ -53,12 +64,18 @@ export function PagesPage({
};
}, [setRedirectPathAfterChange, path]);

const { loading, data } = useQuery<GQLPagesQuery, GQLPagesQueryVariables>(pagesQuery, {
const { loading, data, refetch, startPolling, stopPolling } = useQuery<GQLPagesQuery, GQLPagesQueryVariables>(pagesQuery, {
variables: {
contentScope: scope,
category,
},
});

useFocusAwarePolling({
pollInterval: process.env.NODE_ENV === "development" ? undefined : 10000,
refetch,
startPolling,
stopPolling,
});

const [EditDialog, editDialogSelection, editDialogApi] = useEditDialog();
Expand Down