From 8e29c5fe68c3439709b310b7e3e9d4409ecc6c99 Mon Sep 17 00:00:00 2001 From: Seth Falco Date: Sun, 26 May 2024 22:55:22 +0100 Subject: [PATCH] fix: send 1 notification when switching connection --- src/connectionController.ts | 47 +++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/src/connectionController.ts b/src/connectionController.ts index 1572fd834..d4a3b9ddf 100644 --- a/src/connectionController.ts +++ b/src/connectionController.ts @@ -327,11 +327,14 @@ export default class ConnectionController { this._connectingConnectionId = connectionId; this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE); + const prevConnectionId = this._currentConnectionId; + const nextConnectionName = this.getSavedConnectionName(connectionId); + if (this._activeDataService) { log.info('Disconnecting from the previous connection...', { connectionId: this._currentConnectionId, }); - await this.disconnect(); + await this.disconnect(true); } if (connectionAttempt.isClosed()) { @@ -350,7 +353,7 @@ export default class ConnectionController { throw new Error('Connect failed: connectionOptions are missing.'); } - this._statusView.showMessage('Connecting to MongoDB...'); + this._statusView.showMessage(`Connecting to ${nextConnectionName}...`); log.info('Connecting to MongoDB...', { connectionInfo: JSON.stringify( extractSecrets(this._connections[connectionId]).connectionInfo @@ -418,7 +421,20 @@ export default class ConnectionController { } log.info('Successfully connected', { connectionId }); - void vscode.window.showInformationMessage('MongoDB connection successful.'); + + if (prevConnectionId) { + const prevConnectionName = prevConnectionId + ? this.getSavedConnectionName(prevConnectionId) + : 'MongoDB server'; + + void vscode.window.showInformationMessage( + `Disconnected from ${prevConnectionName} and connected to ${nextConnectionName}.` + ); + } else { + void vscode.window.showInformationMessage( + `Connected to ${nextConnectionName}` + ); + } dataService.addReauthenticationHandler( this._reauthenticationHandler.bind(this) @@ -566,12 +582,17 @@ export default class ConnectionController { } } - async disconnect(): Promise { + /** + * @param quiet Don't display non-error notifications to the user. + * @returns If we disconnected from MongoDB successfully. + */ + async disconnect(quiet = false): Promise { log.info( 'Disconnect called, currently connected to', this._currentConnectionId ); + const disconnectingConnectionId = this._currentConnectionId; this._currentConnectionId = null; this._disconnecting = true; @@ -586,12 +607,24 @@ export default class ConnectionController { return false; } - this._statusView.showMessage('Disconnecting from current connection...'); + const disconnectingConnectionName = disconnectingConnectionId + ? this.getSavedConnectionName(disconnectingConnectionId) + : 'MongoDB server'; + + this._statusView.showMessage( + `Disconnecting from ${disconnectingConnectionName}...` + ); try { // Disconnect from the active connection. await this._activeDataService.disconnect(); - void vscode.window.showInformationMessage('MongoDB disconnected.'); + + if (!quiet) { + void vscode.window.showInformationMessage( + `Disconnected from ${disconnectingConnectionName}` + ); + } + this._activeDataService = null; void vscode.commands.executeCommand( @@ -607,7 +640,7 @@ export default class ConnectionController { } catch (error) { // Show an error, however we still reset the active connection to free up the extension. void vscode.window.showErrorMessage( - 'An error occurred while disconnecting from the current connection.' + `An error occurred while disconnecting from ${disconnectingConnectionName}.` ); }