11import { useEffect , useRef } from 'react' ;
22import { useSelector } from 'react-redux' ;
33import { getCompletedOnboarding } from '../ducks/metamask/metamask' ;
4+ import { useSyncEqualityCheck } from './useSyncEqualityCheck' ;
45
56type 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
7770export default useMultiPolling ;
0 commit comments