Skip to content

Commit f577186

Browse files
committed
Refactor polling hooks to use new hook
1 parent a21f02b commit f577186

File tree

2 files changed

+45
-59
lines changed

2 files changed

+45
-59
lines changed

ui/hooks/useMultiPolling.ts

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect, useRef } from 'react';
22
import { useSelector } from 'react-redux';
33
import { getCompletedOnboarding } from '../ducks/metamask/metamask';
4+
import { useSyncEqualityCheck } from './useSyncEqualityCheck';
45

56
type UseMultiPollingOptions<PollingInput> = {
67
startPolling: (input: PollingInput) => Promise<string>;
@@ -16,28 +17,27 @@ const useMultiPolling = <PollingInput>(
1617
) => {
1718
const completedOnboarding = useSelector(getCompletedOnboarding);
1819
const pollingTokens = useRef<Map<string, string>>(new Map());
20+
const pollingInputs = useSyncEqualityCheck(usePollingOptions.input);
1921

20-
const prevPollingInputStringified = useRef<string | null>(null);
21-
const hasPollingInputChanged =
22-
JSON.stringify(usePollingOptions.input) !==
23-
prevPollingInputStringified.current;
24-
25-
const isMounted = useRef(false);
22+
const isMounted = useRef(true);
2623
useEffect(() => {
27-
isMounted.current = true;
2824
return () => {
2925
isMounted.current = false;
26+
// Stop all polling on unmount - access refs at cleanup time
27+
for (const token of pollingTokens.current.values()) {
28+
usePollingOptions.stopPollingByPollingToken(token);
29+
}
3030
};
3131
}, []);
3232

3333
useEffect(() => {
34-
if (!completedOnboarding || !hasPollingInputChanged) {
35-
// don't start polling if no selected account, or onboarding is incomplete, or polling inputs haven't changed
36-
return () => undefined;
34+
if (!completedOnboarding) {
35+
// don't start polling if onboarding is incomplete
36+
return;
3737
}
3838

3939
// start new polls
40-
for (const input of usePollingOptions.input) {
40+
for (const input of pollingInputs) {
4141
const key = JSON.stringify(input);
4242
if (!pollingTokens.current.has(key)) {
4343
usePollingOptions.startPolling(input).then((token) => {
@@ -50,28 +50,21 @@ const useMultiPolling = <PollingInput>(
5050

5151
// stop existing polls
5252
for (const [inputKey, token] of pollingTokens.current.entries()) {
53-
const exists = usePollingOptions.input.some(
54-
(i) => inputKey === JSON.stringify(i),
53+
const exists = pollingInputs.some(
54+
(i: PollingInput) => inputKey === JSON.stringify(i),
5555
);
5656

5757
if (!exists) {
5858
usePollingOptions.stopPollingByPollingToken(token);
5959
pollingTokens.current.delete(inputKey);
6060
}
6161
}
62-
63-
prevPollingInputStringified.current = JSON.stringify(
64-
usePollingOptions.input,
65-
);
66-
67-
// stop all polling on dismount
68-
return () => {
69-
for (const token of pollingTokens.current.values()) {
70-
usePollingOptions.stopPollingByPollingToken(token);
71-
}
72-
prevPollingInputStringified.current = null;
73-
};
74-
}, [usePollingOptions, hasPollingInputChanged, completedOnboarding]);
62+
}, [
63+
pollingInputs,
64+
usePollingOptions.startPolling,
65+
usePollingOptions.stopPollingByPollingToken,
66+
completedOnboarding,
67+
]);
7568
};
7669

7770
export default useMultiPolling;

ui/hooks/usePolling.ts

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect, useRef } from 'react';
2+
import { useSyncEqualityCheck } from './useSyncEqualityCheck';
23

34
type UsePollingOptions<PollingInput> = {
45
callback?: (pollingToken: string) => (pollingToken: string) => void;
@@ -13,58 +14,50 @@ const usePolling = <PollingInput>(
1314
) => {
1415
const pollTokenRef = useRef<null | string>(null);
1516
const cleanupRef = useRef<null | ((pollingToken: string) => void)>(null);
17+
const pollingInput = useSyncEqualityCheck(usePollingOptions.input);
1618

17-
const prevPollingInputStringified = useRef<string | null>(null);
18-
const hasPollingInputChanged =
19-
JSON.stringify(usePollingOptions.input) !==
20-
prevPollingInputStringified.current;
21-
22-
const isMounted = useRef(false);
19+
const isMounted = useRef(true);
2320
useEffect(() => {
24-
isMounted.current = true;
2521
return () => {
2622
isMounted.current = false;
2723
};
2824
}, []);
2925

3026
useEffect(() => {
31-
if (usePollingOptions.enabled === false || !hasPollingInputChanged) {
32-
return () => {
33-
// noop
34-
};
35-
}
36-
3727
const cleanup = () => {
3828
if (pollTokenRef.current) {
3929
usePollingOptions.stopPollingByPollingToken(pollTokenRef.current);
4030
cleanupRef.current?.(pollTokenRef.current);
31+
pollTokenRef.current = null;
32+
cleanupRef.current = null;
4133
}
4234
};
4335

44-
// Start polling when the component mounts
45-
usePollingOptions
46-
.startPolling(usePollingOptions.input)
47-
.then((pollToken) => {
48-
pollTokenRef.current = pollToken;
49-
cleanupRef.current = usePollingOptions.callback?.(pollToken) ?? null;
50-
if (!isMounted.current) {
51-
cleanup();
52-
}
53-
});
36+
if (usePollingOptions.enabled === false) {
37+
// Stop polling if it was previously enabled
38+
if (pollTokenRef.current) {
39+
cleanup();
40+
}
41+
return () => {
42+
// noop
43+
};
44+
}
5445

55-
prevPollingInputStringified.current = JSON.stringify(
56-
usePollingOptions.input,
57-
);
46+
// Start polling when the component mounts
47+
usePollingOptions.startPolling(pollingInput).then((pollToken) => {
48+
pollTokenRef.current = pollToken;
49+
cleanupRef.current = usePollingOptions.callback?.(pollToken) ?? null;
50+
if (!isMounted.current) {
51+
cleanup();
52+
}
53+
});
5854

59-
// Return a cleanup function to stop polling when the component unmounts
60-
return () => {
61-
prevPollingInputStringified.current = null;
62-
cleanup();
63-
};
55+
// Return a cleanup function to stop polling when the component unmounts or dependencies change
56+
return cleanup;
6457
}, [
65-
usePollingOptions.input,
66-
hasPollingInputChanged,
58+
pollingInput,
6759
usePollingOptions.enabled,
60+
usePollingOptions.stopPollingByPollingToken,
6861
]);
6962
};
7063

0 commit comments

Comments
 (0)