Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: send 1 notification when switching connection
Browse files Browse the repository at this point in the history
Co-authored-by: Alena Khineika <[email protected]>
SethFalco and alenakhineika committed Jun 7, 2024
1 parent e56ae19 commit 1712a84
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
@@ -327,11 +327,13 @@ export default class ConnectionController {
this._connectingConnectionId = connectionId;
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);

const nextConnectionName = this.getSavedConnectionName(connectionId);

if (this._activeDataService) {
log.info('Disconnecting from the previous connection...', {
connectionId: this._currentConnectionId,
});
await this.disconnect();
await this.disconnect({ quiet: true });
}

if (connectionAttempt.isClosed()) {
@@ -350,7 +352,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 +420,9 @@ export default class ConnectionController {
}

log.info('Successfully connected', { connectionId });
void vscode.window.showInformationMessage('MongoDB connection successful.');
void vscode.window.showInformationMessage(
`Set the active connection to ${nextConnectionName}.`
);

dataService.addReauthenticationHandler(
this._reauthenticationHandler.bind(this)
@@ -566,12 +570,17 @@ export default class ConnectionController {
}
}

async disconnect(): Promise<boolean> {
/**
* @param quiet Don't display non-error notifications to the user.
* @returns If we disconnected from MongoDB successfully.
*/
async disconnect({ quiet = false } = {}): Promise<boolean> {
log.info(
'Disconnect called, currently connected to',
this._currentConnectionId
);

const disconnectingConnectionId = this._currentConnectionId;
this._currentConnectionId = null;
this._disconnecting = true;

@@ -586,12 +595,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 +628,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}.`
);
}

0 comments on commit 1712a84

Please sign in to comment.