1
- import { useQuery , useQueryClient } from "@tanstack/react-query" ;
1
+ import { useQueryClient } from "@tanstack/react-query" ;
2
2
import { useEffect , useRef } from "react" ;
3
3
4
4
import babylon from "@/infrastructure/babylon" ;
@@ -9,54 +9,52 @@ import { usePendingOperationsService } from "../services/usePendingOperationsSer
9
9
import { BABY_DELEGATIONS_KEY } from "./useDelegations" ;
10
10
import { BABY_UNBONDING_DELEGATIONS_KEY } from "./useUnbondingDelegations" ;
11
11
12
- const BABYLON_CURRENT_EPOCH_KEY = "BABYLON_CURRENT_EPOCH" ;
13
-
14
12
export function useEpochPolling ( address ?: string ) {
15
13
const queryClient = useQueryClient ( ) ;
16
14
const previousEpochRef = useRef < number | undefined > ( undefined ) ;
17
15
const { cleanupAllPendingOperationsFromStorage } =
18
16
usePendingOperationsService ( ) ;
19
-
20
- const { data : currentEpoch } = useQuery < number , Error > ( {
21
- queryKey : [ BABYLON_CURRENT_EPOCH_KEY ] ,
22
- queryFn : async ( ) => {
23
- const client = await babylon . client ( ) ;
24
- const { currentEpoch } = await client . baby . getCurrentEpoch ( ) ;
25
- return currentEpoch ;
26
- } ,
27
- refetchInterval : ONE_MINUTE ,
28
- // Only poll if we have an address (user is connected)
29
- enabled : Boolean ( address ) ,
30
- } ) ;
17
+ const cleanupRef = useRef ( cleanupAllPendingOperationsFromStorage ) ;
18
+ cleanupRef . current = cleanupAllPendingOperationsFromStorage ;
31
19
32
20
useEffect ( ( ) => {
33
- if ( currentEpoch === undefined ) return ;
34
-
35
- if ( previousEpochRef . current === undefined ) {
36
- previousEpochRef . current = currentEpoch ;
37
- return ;
38
- }
39
-
40
- if ( currentEpoch !== previousEpochRef . current ) {
41
- // Clean up all pending operations from localStorage (everything from
42
- // previous epochs is finalized)
43
- cleanupAllPendingOperationsFromStorage ( ) ;
44
-
45
- // Invalidate all delegation queries since epoch change affects entire
46
- // blockchain state
47
- queryClient . invalidateQueries ( {
48
- queryKey : [ BABY_DELEGATIONS_KEY ] ,
49
- } ) ;
50
- // Also invalidate unbonding delegations since they're epoch-dependent
51
- queryClient . invalidateQueries ( {
52
- queryKey : [ BABY_UNBONDING_DELEGATIONS_KEY ] ,
53
- } ) ;
54
- previousEpochRef . current = currentEpoch ;
55
- }
56
- } , [
57
- currentEpoch ,
58
- queryClient ,
59
- address ,
60
- cleanupAllPendingOperationsFromStorage ,
61
- ] ) ;
21
+ if ( ! address ) return ;
22
+
23
+ let cancelled = false ;
24
+
25
+ const checkEpoch = async ( ) => {
26
+ try {
27
+ const client = await babylon . client ( ) ;
28
+ const { currentEpoch } = await client . baby . getCurrentEpoch ( ) ;
29
+
30
+ if ( previousEpochRef . current === undefined ) {
31
+ previousEpochRef . current = currentEpoch ;
32
+ return ;
33
+ }
34
+
35
+ if ( ! cancelled && currentEpoch !== previousEpochRef . current ) {
36
+ cleanupRef . current ?.( ) ;
37
+
38
+ queryClient . invalidateQueries ( {
39
+ queryKey : [ BABY_DELEGATIONS_KEY ] ,
40
+ refetchType : "active" ,
41
+ } ) ;
42
+ queryClient . invalidateQueries ( {
43
+ queryKey : [ BABY_UNBONDING_DELEGATIONS_KEY ] ,
44
+ refetchType : "active" ,
45
+ } ) ;
46
+ previousEpochRef . current = currentEpoch ;
47
+ }
48
+ } catch {
49
+ // ignore transient errors
50
+ }
51
+ } ;
52
+
53
+ checkEpoch ( ) ;
54
+ const id = setInterval ( checkEpoch , ONE_MINUTE ) ;
55
+ return ( ) => {
56
+ cancelled = true ;
57
+ clearInterval ( id ) ;
58
+ } ;
59
+ } , [ address , queryClient ] ) ;
62
60
}
0 commit comments