Skip to content

Commit e68f0e4

Browse files
committed
Fix polling not stopping after dismount
1 parent 85f0648 commit e68f0e4

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
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: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ const usePolling = <PollingInput>(
1919
JSON.stringify(usePollingOptions.input) !==
2020
prevPollingInputStringified.current;
2121

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

30-
isMounted = true;
31+
isMounted.current = true;
3132

3233
const cleanup = () => {
3334
if (pollTokenRef.current) {
@@ -41,10 +42,8 @@ const usePolling = <PollingInput>(
4142
.startPolling(usePollingOptions.input)
4243
.then((pollToken) => {
4344
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) {
45+
cleanupRef.current = usePollingOptions.callback?.(pollToken) ?? null;
46+
if (!isMounted.current) {
4847
cleanup();
4948
}
5049
});
@@ -55,11 +54,15 @@ const usePolling = <PollingInput>(
5554

5655
// Return a cleanup function to stop polling when the component unmounts
5756
return () => {
58-
isMounted = false;
57+
isMounted.current = false;
5958
prevPollingInputStringified.current = null;
6059
cleanup();
6160
};
62-
}, [hasPollingInputChanged, usePollingOptions.enabled]);
61+
}, [
62+
usePollingOptions.input,
63+
hasPollingInputChanged,
64+
usePollingOptions.enabled,
65+
]);
6366
};
6467

6568
export default usePolling;

0 commit comments

Comments
 (0)