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

Fix revalidation logic #34

Merged
merged 1 commit into from
Oct 15, 2021
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
23 changes: 13 additions & 10 deletions src/lib/hooks/use-revalidation.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import {useCallback, useEffect, useMemo, useState} from 'react';
import {useEffect, useMemo, useState} from 'react';
import pThrottle from 'p-throttle';
import {useInterval} from '@chakra-ui/react';
import useWindowFocus from './use-window-focus';
import {useCallbackRef, useInterval} from '@chakra-ui/react';
import useWindowFocus, {UseWindowFocusParameters} from './use-window-focus';

const useRevalidation = (doRevalidation: boolean, revalidate: () => Promise<void>, interval = 3000) => {
const revalidateRef = useCallbackRef(revalidate);
const [shouldRefetchAtInterval, setShouldRefetchAtInterval] = useState(doRevalidation);

const throttledRevalidation = useMemo(() => pThrottle({limit: 1, interval: 1000})(revalidate), [revalidate]);
const throttledRevalidation = useMemo(() => pThrottle({limit: 1, interval})(revalidateRef), [revalidateRef, interval]);

useWindowFocus({
onFocus: useCallback(() => {
const focusParameters: UseWindowFocusParameters = useMemo(() => ({
onFocus: () => {
setShouldRefetchAtInterval(true);
void throttledRevalidation();
}, [throttledRevalidation]),
onBlur: useCallback(() => {
},
onBlur: () => {
setShouldRefetchAtInterval(false);
}, []),
});
},
}), [throttledRevalidation]);

useWindowFocus(focusParameters);

useEffect(() => {
if (doRevalidation) {
Expand Down
7 changes: 6 additions & 1 deletion src/lib/hooks/use-window-focus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {useEffect} from 'react';

const useWindowFocus = ({onFocus, onBlur}: {onFocus: () => void; onBlur: () => void}) => {
export type UseWindowFocusParameters = {
onFocus: () => void;
onBlur: () => void;
};

const useWindowFocus = ({onFocus, onBlur}: UseWindowFocusParameters) => {
useEffect(() => {
window.addEventListener('focus', onFocus);
window.addEventListener('blur', onBlur);
Expand Down
26 changes: 16 additions & 10 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useMemo} from 'react';
import type {AppProps} from 'next/app';
import Head from 'next/head';
import {Box, BoxProps, ChakraProvider, extendTheme} from '@chakra-ui/react';
Expand Down Expand Up @@ -50,17 +50,23 @@ const theme = extendTheme({
const MyApp = ({Component, pageProps}: AppProps & {Component: CustomNextPage<any>}) => {
const state = useStore();

useRevalidation(true, async () => state.apiState.revalidate());
useRevalidation(true, async () => {
await state.apiState.revalidate();
});

const wrapperProps: BoxProps = {};
const wrapperProps: BoxProps = useMemo(() => {
if (Component.useStaticHeight) {
return {
h: '100vh',
display: 'flex',
flexDir: 'column',
pos: 'relative',
overflow: 'hidden',
};
}

if (Component.useStaticHeight) {
wrapperProps.h = '100vh';
wrapperProps.display = 'flex';
wrapperProps.flexDir = 'column';
wrapperProps.pos = 'relative';
wrapperProps.overflow = 'hidden';
}
return {};
}, [Component.useStaticHeight]);

return (
<ChakraProvider theme={theme}>
Expand Down