Skip to content

Commit bda455b

Browse files
committed
Fix polling not stopped on dismount
1 parent d6b0e6d commit bda455b

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

ui/hooks/useCurrencyRatePolling.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ const useNativeCurrencies = (isPollingEnabled: boolean) => {
4040
? enabledChainIds
4141
: chainIds;
4242

43-
const isMounted = useRef(false);
44-
useEffect(() => {
45-
isMounted.current = true;
46-
return () => {
47-
isMounted.current = false;
48-
};
49-
});
43+
const isMounted = useRef(true);
5044

5145
useEffect(() => {
5246
// Use validated currency tickers
@@ -76,6 +70,10 @@ const useNativeCurrencies = (isPollingEnabled: boolean) => {
7670
}
7771
};
7872
fetchNativeCurrencies();
73+
74+
return () => {
75+
isMounted.current = false;
76+
};
7977
}, [
8078
chainIds,
8179
isPollingEnabled,
@@ -96,8 +94,6 @@ const useCurrencyRatePolling = () => {
9694
// usePolling is a custom hook that is invoked synchronously.
9795
usePolling({
9896
startPolling: currencyRateStartPolling,
99-
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31879
100-
// eslint-disable-next-line @typescript-eslint/no-misused-promises
10197
stopPollingByPollingToken: currencyRateStopPollingByPollingToken,
10298
input: nativeCurrencies,
10399
enabled,

ui/hooks/usePolling.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ const usePolling = <PollingInput>(
1919
JSON.stringify(usePollingOptions.input) !==
2020
prevPollingInputStringified.current;
2121

22+
const isMounted = useRef(true);
23+
2224
useEffect(() => {
23-
let isMounted = false;
2425
if (usePollingOptions.enabled === false || !hasPollingInputChanged) {
2526
return () => {
2627
// noop
2728
};
2829
}
2930

30-
isMounted = true;
31-
3231
const cleanup = () => {
3332
if (pollTokenRef.current) {
3433
usePollingOptions.stopPollingByPollingToken(pollTokenRef.current);
@@ -41,10 +40,8 @@ const usePolling = <PollingInput>(
4140
.startPolling(usePollingOptions.input)
4241
.then((pollToken) => {
4342
pollTokenRef.current = pollToken;
44-
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31880
45-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
46-
cleanupRef.current = usePollingOptions.callback?.(pollToken) || null;
47-
if (!isMounted) {
43+
cleanupRef.current = usePollingOptions.callback?.(pollToken) ?? null;
44+
if (!isMounted.current) {
4845
cleanup();
4946
}
5047
});
@@ -55,11 +52,15 @@ const usePolling = <PollingInput>(
5552

5653
// Return a cleanup function to stop polling when the component unmounts
5754
return () => {
58-
isMounted = false;
55+
isMounted.current = false;
5956
prevPollingInputStringified.current = null;
6057
cleanup();
6158
};
62-
}, [hasPollingInputChanged, usePollingOptions.enabled]);
59+
}, [
60+
usePollingOptions.input,
61+
hasPollingInputChanged,
62+
usePollingOptions.enabled,
63+
]);
6364
};
6465

6566
export default usePolling;

0 commit comments

Comments
 (0)