@@ -102,6 +102,10 @@ export class ConnectionFactory {
102102 this . setupFileWatcher ( connection , patchedConfig )
103103 }
104104
105+ // Remove any existing object with the same name and source to avoid duplicates
106+ this . connections = this . connections . filter (
107+ ( conn ) => ! ( conn . server . name === name && conn . server . source === source ) ,
108+ )
105109 this . connections . push ( connection )
106110 return connection
107111 }
@@ -111,7 +115,7 @@ export class ConnectionFactory {
111115 * @param name Server name
112116 * @param source Optional config source
113117 */
114- async closeConnection ( name : string , source ?: ConfigSource ) : Promise < void > {
118+ async closeConnection ( name : string , source ?: ConfigSource , allowKeep ?: boolean ) : Promise < void > {
115119 // Find and close connections
116120 const connections = source ? this . findConnections ( name , source ) : this . findConnections ( name )
117121
@@ -126,12 +130,14 @@ export class ConnectionFactory {
126130 }
127131 }
128132
129- // Remove from array
130- this . connections = this . connections . filter ( ( conn ) => {
131- if ( conn . server . name !== name ) return true
132- if ( source && conn . server . source !== source ) return true
133- return false
134- } )
133+ // Remove from array unless allowKeep is true
134+ if ( ! allowKeep ) {
135+ this . connections = this . connections . filter ( ( conn ) => {
136+ if ( conn . server . name !== name ) return true
137+ if ( source && conn . server . source !== source ) return true
138+ return false
139+ } )
140+ }
135141 }
136142
137143 /**
@@ -180,11 +186,20 @@ export class ConnectionFactory {
180186 }
181187
182188 for ( const conn of connections ) {
189+ // Set status to connecting
190+ conn . server . status = "connecting"
191+ conn . server . error = ""
192+
193+ // Notify status change if callback exists
194+ if ( this . onStatusChange ) {
195+ this . onStatusChange ( conn . server )
196+ }
197+
183198 const config = JSON . parse ( conn . server . config )
184199 const connSource = conn . server . source || "global"
185200
186- // Close existing connection
187- await this . closeConnection ( name , connSource )
201+ // Close existing connection but do not remove the object, so notifyServersChanged can find "connecting"
202+ await this . closeConnection ( name , connSource , true )
188203
189204 // Create new connection
190205 await this . createConnection ( name , config , connSource )
0 commit comments