@@ -64,35 +64,38 @@ export const AgentMetadata: FC<AgentMetadataProps> = ({
64
64
}
65
65
66
66
let timeoutId : number | undefined = undefined ;
67
- let latestSocket : OneWayWebSocket | undefined = undefined ;
67
+ let activeSocket : OneWayWebSocket | undefined = undefined ;
68
68
let retries = 0 ;
69
69
70
70
const createNewConnection = ( ) => {
71
71
const socket = watchAgentMetadata ( agent . id ) ;
72
- latestSocket = socket ;
72
+ activeSocket = socket ;
73
73
74
74
socket . addEventListener ( "error" , ( ) => {
75
75
setActiveMetadata ( undefined ) ;
76
76
window . clearTimeout ( timeoutId ) ;
77
77
78
+ // The error event is supposed to fire when an error happens
79
+ // with the connection itself, which implies that the connection
80
+ // would auto-close. Couldn't find a definitive answer on MDN,
81
+ // though, so closing it manually just to be safe
82
+ socket . close ( ) ;
83
+ activeSocket = undefined ;
84
+
78
85
retries ++ ;
79
- if ( retries < maxSocketErrorRetryCount ) {
86
+ if ( retries >= maxSocketErrorRetryCount ) {
80
87
displayError (
81
- "Unexpected disconnect while watching Metadata changes. Creating new connection.. ." ,
88
+ "Unexpected disconnect while watching Metadata changes. Please try refreshing the page ." ,
82
89
) ;
83
- timeoutId = window . setTimeout ( ( ) => {
84
- createNewConnection ( ) ;
85
- } , 3_000 ) ;
86
90
return ;
87
91
}
88
92
89
93
displayError (
90
- "Unexpected disconnect while watching Metadata changes. Cannot connect to server " ,
94
+ "Unexpected disconnect while watching Metadata changes. Creating new connection... " ,
91
95
) ;
92
- // The socket should already be closed by this point, but doing
93
- // this just to be thorough
94
- socket . close ( ) ;
95
- latestSocket = undefined ;
96
+ timeoutId = window . setTimeout ( ( ) => {
97
+ createNewConnection ( ) ;
98
+ } , 3_000 ) ;
96
99
} ) ;
97
100
98
101
socket . addEventListener ( "message" , ( e ) => {
@@ -112,7 +115,7 @@ export const AgentMetadata: FC<AgentMetadataProps> = ({
112
115
createNewConnection ( ) ;
113
116
return ( ) => {
114
117
window . clearTimeout ( timeoutId ) ;
115
- latestSocket ?. close ( ) ;
118
+ activeSocket ?. close ( ) ;
116
119
} ;
117
120
} , [ agent . id , storybookMetadata ] ) ;
118
121
0 commit comments