Skip to content

Commit d8828a1

Browse files
committed
Fix polling not stopping on dismount
1 parent 3119514 commit d8828a1

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

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)