@@ -14,12 +14,28 @@ interface PollingState {
1414 maxAttempts : number ;
1515}
1616
17+ const DEFAULT_MAX_ATTEMPTS = 20 ;
18+
1719export function useClientPolling ( ) {
18- const [ pollingState , setPollingState ] = useState < PollingState > ( {
19- isPolling : false ,
20- clientId : null ,
21- attempts : 0 ,
22- maxAttempts : 20 , // 20 minutes max (20 * 60 seconds)
20+ const [ pollingState , setPollingState ] = useState < PollingState > ( ( ) => {
21+ const pendingTransactions = getActivePendingTransactions ( ) ;
22+ const firstPending = pendingTransactions [ 0 ] ;
23+
24+ if ( firstPending ) {
25+ return {
26+ isPolling : true ,
27+ clientId : firstPending . id ,
28+ attempts : firstPending . attempts ,
29+ maxAttempts : DEFAULT_MAX_ATTEMPTS ,
30+ } ;
31+ }
32+
33+ return {
34+ isPolling : false ,
35+ clientId : null ,
36+ attempts : 0 ,
37+ maxAttempts : DEFAULT_MAX_ATTEMPTS ,
38+ } ;
2339 } ) ;
2440
2541 const queryClient = useQueryClient ( ) ;
@@ -31,7 +47,7 @@ export function useClientPolling() {
3147 isPolling : true ,
3248 clientId,
3349 attempts : initialAttempts ,
34- maxAttempts : 20 ,
50+ maxAttempts : DEFAULT_MAX_ATTEMPTS ,
3551 } ) ;
3652 } ,
3753 [ ] ,
@@ -59,15 +75,6 @@ export function useClientPolling() {
5975 }
6076 } , [ ] ) ;
6177
62- useEffect ( ( ) => {
63- const pendingTransactions = getActivePendingTransactions ( ) ;
64-
65- if ( pendingTransactions . length > 0 ) {
66- const firstPending = pendingTransactions [ 0 ] ;
67- startPolling ( firstPending . id , firstPending . attempts ) ;
68- }
69- } , [ startPolling ] ) ;
70-
7178 useEffect ( ( ) => {
7279 if ( ! pollingState . isPolling || ! pollingState . clientId ) {
7380 return ;
@@ -89,33 +96,45 @@ export function useClientPolling() {
8996 return ;
9097 }
9198
92- const newAttempts = pollingState . attempts + 1 ;
93- setPollingState ( ( prev ) => ( {
94- ...prev ,
95- attempts : newAttempts ,
96- } ) ) ;
99+ let shouldStop = false ;
100+ setPollingState ( ( prev ) => {
101+ const newAttempts = prev . attempts + 1 ;
97102
98- if ( pollingState . clientId ) {
99- updateTransactionAttempts ( pollingState . clientId , newAttempts ) ;
100- }
103+ if ( prev . clientId ) {
104+ updateTransactionAttempts ( prev . clientId , newAttempts ) ;
105+ }
106+
107+ shouldStop = newAttempts >= prev . maxAttempts ;
101108
102- if ( newAttempts >= pollingState . maxAttempts ) {
109+ return {
110+ ...prev ,
111+ attempts : newAttempts ,
112+ } ;
113+ } ) ;
114+
115+ if ( shouldStop ) {
103116 stopPolling ( false ) ;
104117 }
105118 } catch ( error ) {
106119 console . error ( "Error polling client availability:" , error ) ;
107120
108- const newAttempts = pollingState . attempts + 1 ;
109- setPollingState ( ( prev ) => ( {
110- ...prev ,
111- attempts : newAttempts ,
112- } ) ) ;
121+ let shouldStop = false ;
122+ setPollingState ( ( prev ) => {
123+ const newAttempts = prev . attempts + 1 ;
113124
114- if ( pollingState . clientId ) {
115- updateTransactionAttempts ( pollingState . clientId , newAttempts ) ;
116- }
125+ if ( prev . clientId ) {
126+ updateTransactionAttempts ( prev . clientId , newAttempts ) ;
127+ }
128+
129+ shouldStop = newAttempts >= prev . maxAttempts ;
130+
131+ return {
132+ ...prev ,
133+ attempts : newAttempts ,
134+ } ;
135+ } ) ;
117136
118- if ( newAttempts >= pollingState . maxAttempts ) {
137+ if ( shouldStop ) {
119138 stopPolling ( false ) ;
120139 }
121140 }
0 commit comments