@@ -19,7 +19,6 @@ export const useConnection = (): UseConnectionReturn => {
1919 > ( null ) ;
2020 const [ queries , setQueries ] = useState < QueryData [ ] > ( [ ] ) ;
2121 const [ mutations , setMutations ] = useState < MutationData [ ] > ( [ ] ) ;
22- const [ reconnectAttempts , setReconnectAttempts ] = useState ( 0 ) ;
2322 // Track artificial states triggered by DevTools
2423 const [ artificialStates , setArtificialStates ] = useState <
2524 Map < string , "loading" | "error" >
@@ -28,7 +27,6 @@ export const useConnection = (): UseConnectionReturn => {
2827 // Connection management
2928 const portRef = useRef < chrome . runtime . Port | null > ( null ) ;
3029 const reconnectTimeoutRef = useRef < NodeJS . Timeout | null > ( null ) ;
31- const heartbeatIntervalRef = useRef < NodeJS . Timeout | null > ( null ) ;
3230
3331 // Send message function
3432 const sendMessage = useCallback ( ( message : unknown ) => {
@@ -58,28 +56,7 @@ export const useConnection = (): UseConnectionReturn => {
5856 } ) ;
5957
6058 port . onMessage . addListener ( ( message ) => {
61- if ( message . type === "CONNECTION_ESTABLISHED" ) {
62- setReconnectAttempts ( 0 ) ;
63-
64- // Start heartbeat
65- if ( heartbeatIntervalRef . current ) {
66- clearInterval ( heartbeatIntervalRef . current ) ;
67- }
68- heartbeatIntervalRef . current = setInterval ( ( ) => {
69- if ( portRef . current ) {
70- try {
71- portRef . current . postMessage ( {
72- type : "PING" ,
73- timestamp : Date . now ( ) ,
74- } ) ;
75- } catch ( error ) {
76- console . warn ( "Failed to send ping:" , error ) ;
77- }
78- }
79- } , 10000 ) ; // Ping every 10 seconds
80- } else if ( message . type === "PONG" ) {
81- // Connection is healthy
82- } else if ( message . type === "INITIAL_STATE" ) {
59+ if ( message . type === "INITIAL_STATE" ) {
8360 // Clear previous data when connecting to a different tab
8461 setQueries ( [ ] ) ;
8562 setMutations ( [ ] ) ;
@@ -110,9 +87,13 @@ export const useConnection = (): UseConnectionReturn => {
11087 switch ( message . subtype ) {
11188 case "QUERY_CLIENT_DETECTED" :
11289 setTanStackQueryDetected ( true ) ;
90+ // Clear artificial states when TanStack Query is detected (page refresh/reload)
91+ setArtificialStates ( new Map ( ) ) ;
11392 break ;
11493 case "QUERY_CLIENT_NOT_FOUND" :
11594 setTanStackQueryDetected ( false ) ;
95+ // Clear artificial states when TanStack Query is not found
96+ setArtificialStates ( new Map ( ) ) ;
11697 break ;
11798 case "QUERY_STATE_UPDATE" :
11899 break ;
@@ -161,50 +142,21 @@ export const useConnection = (): UseConnectionReturn => {
161142 }
162143 }
163144 } ) ;
164-
165- port . onDisconnect . addListener ( ( ) => {
166- portRef . current = null ;
167-
168- // Clear heartbeat
169- if ( heartbeatIntervalRef . current ) {
170- clearInterval ( heartbeatIntervalRef . current ) ;
171- heartbeatIntervalRef . current = null ;
172- }
173-
174- // Attempt reconnection with exponential backoff
175- const attempt = reconnectAttempts + 1 ;
176- setReconnectAttempts ( attempt ) ;
177-
178- if ( attempt <= 5 ) {
179- const delay = Math . min ( 1000 * Math . pow ( 2 , attempt - 1 ) , 10000 ) ; // Max 10s delay
180-
181- reconnectTimeoutRef . current = setTimeout ( ( ) => {
182- connectToBackground ( ) ;
183- } , delay ) ;
184- } else {
185- console . error ( "Failed to reconnect after 5 attempts" ) ;
186- }
187- } ) ;
188145 } catch ( error ) {
189146 console . error ( "Failed to connect to background script:" , error ) ;
190147 }
191- } , [ reconnectAttempts ] ) ;
148+ } , [ ] ) ;
192149
193150 useEffect ( ( ) => {
194151 connectToBackground ( ) ;
195152
153+ const reconnectTimeout = reconnectTimeoutRef . current ;
154+
196155 // Cleanup function
197156 return ( ) => {
198- if ( portRef . current ) {
199- portRef . current . disconnect ( ) ;
200- portRef . current = null ;
201- }
202- if ( reconnectTimeoutRef . current ) {
203- clearTimeout ( reconnectTimeoutRef . current ) ;
204- }
205- if ( heartbeatIntervalRef . current ) {
206- clearInterval ( heartbeatIntervalRef . current ) ;
207- }
157+ portRef . current ?. disconnect ( ) ;
158+ portRef . current = null ;
159+ if ( reconnectTimeout ) clearTimeout ( reconnectTimeout ) ;
208160 } ;
209161 } , [ connectToBackground ] ) ;
210162
0 commit comments