Skip to content

Commit 2842bcf

Browse files
author
tombertrand
committed
Re-enable timeout polling on CPS, market statistics & uptime on visibility change
1 parent 67d0d33 commit 2842bcf

File tree

3 files changed

+58
-20
lines changed

3 files changed

+58
-20
lines changed

src/api/contexts/MarketStatistics.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export interface Context {
3939
isError: boolean;
4040
}
4141

42-
let pollMarketStatisticsInterval: number | undefined;
42+
let pollMarketStatisticsTimeout: number | undefined;
4343

4444
export const MarketStatisticsContext = React.createContext<Context>({
4545
marketStatistics: {
@@ -77,7 +77,7 @@ const Provider: React.FC = ({ children }) => {
7777
const { fiat } = React.useContext(PreferencesContext);
7878

7979
const getMarketStatistics = async (fiat: string) => {
80-
clearTimeout(pollMarketStatisticsInterval);
80+
clearTimeout(pollMarketStatisticsTimeout);
8181

8282
setIsError(false);
8383
setIsLoading(true);
@@ -98,16 +98,27 @@ const Provider: React.FC = ({ children }) => {
9898
setIsInitialLoading(false);
9999
setIsLoading(false);
100100

101-
pollMarketStatisticsInterval = window.setTimeout(() => {
102-
if (document.visibilityState !== 'visible') return;
101+
pollMarketStatisticsTimeout = window.setTimeout(() => {
103102
getMarketStatistics(fiat);
104103
}, 7500);
105104
};
106105

107106
React.useEffect(() => {
107+
function visibilityChange() {
108+
if (document.visibilityState === "visible") {
109+
getMarketStatistics(fiat);
110+
} else {
111+
clearTimeout(pollMarketStatisticsTimeout);
112+
}
113+
}
114+
108115
getMarketStatistics(fiat);
116+
window.addEventListener("visibilitychange", visibilityChange);
109117

110-
return () => clearInterval(pollMarketStatisticsInterval);
118+
return () => {
119+
clearTimeout(pollMarketStatisticsTimeout);
120+
window.removeEventListener("visibilitychange", visibilityChange);
121+
};
111122
// eslint-disable-next-line react-hooks/exhaustive-deps
112123
}, []);
113124

src/api/hooks/use-confirmations-per-second.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ export interface UseUptimeReturn {
44
confirmationsPerSecond: string | undefined;
55
}
66

7-
let confirmationsPerSecondInterval: number | undefined;
7+
let confirmationsPerSecondTimeout: number | undefined;
88

99
const useConfirmationsPerSecond = (): UseUptimeReturn => {
1010
const [confirmationsPerSecond, setConfirmationsPerSecond] = React.useState();
1111

1212
const getConfirmationsPerSecond = async () => {
13+
clearTimeout(confirmationsPerSecondTimeout);
1314
try {
1415
const res = await fetch(`/api/confirmations-per-second`);
1516
const { cps } = await res.json();
@@ -19,16 +20,27 @@ const useConfirmationsPerSecond = (): UseUptimeReturn => {
1920
setConfirmationsPerSecond(undefined);
2021
}
2122

22-
confirmationsPerSecondInterval = window.setTimeout(() => {
23-
if (document.visibilityState !== "visible") return;
23+
confirmationsPerSecondTimeout = window.setTimeout(() => {
2424
getConfirmationsPerSecond();
2525
}, 3000);
2626
};
2727

2828
React.useEffect(() => {
29+
function visibilityChange() {
30+
if (document.visibilityState === "visible") {
31+
getConfirmationsPerSecond();
32+
} else {
33+
clearTimeout(confirmationsPerSecondTimeout);
34+
}
35+
}
36+
2937
getConfirmationsPerSecond();
38+
window.addEventListener("visibilitychange", visibilityChange);
3039

31-
return () => clearInterval(confirmationsPerSecondInterval);
40+
return () => {
41+
clearTimeout(confirmationsPerSecondTimeout);
42+
window.removeEventListener("visibilitychange", visibilityChange);
43+
};
3244
// eslint-disable-next-line react-hooks/exhaustive-deps
3345
}, []);
3446

src/api/hooks/use-uptime.tsx

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,45 @@ export interface UseUptimeReturn {
1111
}
1212

1313
const ONE_MINUTE = 1000 * 60;
14+
let uptimeTimeout: number | undefined;
1415

1516
const useUptime = (): UseUptimeReturn => {
1617
const [uptime, setUptime] = React.useState({} as UptimeResponse);
1718
const [isError, setIsError] = React.useState(false);
1819

1920
const getUptime = async () => {
20-
const json = await rpc("uptime");
21+
clearTimeout(uptimeTimeout);
22+
setIsError(false);
2123

22-
!json || json.error ? setIsError(true) : setUptime(json);
23-
};
24+
try {
25+
const json = await rpc("uptime");
2426

25-
React.useEffect(() => {
26-
if (!uptime?.seconds) return;
27+
!json || json.error ? setIsError(true) : setUptime(json);
28+
} catch (err) {
29+
setIsError(true);
30+
}
2731

28-
setTimeout(() => {
29-
if (document.visibilityState !== "visible") return;
30-
setUptime({
31-
seconds: (parseInt(uptime.seconds) + 60).toString(),
32-
});
32+
uptimeTimeout = window.setTimeout(() => {
33+
getUptime();
3334
}, ONE_MINUTE);
34-
}, [uptime]);
35+
};
3536

3637
React.useEffect(() => {
38+
function visibilityChange() {
39+
if (document.visibilityState === "visible") {
40+
getUptime();
41+
} else {
42+
clearTimeout(uptimeTimeout);
43+
}
44+
}
3745
getUptime();
46+
window.addEventListener("visibilitychange", visibilityChange);
47+
48+
return () => {
49+
clearTimeout(uptimeTimeout);
50+
window.removeEventListener("visibilitychange", visibilityChange);
51+
};
52+
// eslint-disable-next-line react-hooks/exhaustive-deps
3853
}, []);
3954

4055
return { uptime, isError };

0 commit comments

Comments
 (0)