Skip to content

Commit

Permalink
fix: send 1 notification when switching connection
Browse files Browse the repository at this point in the history
Co-authored-by: Alena Khineika <alena.khineika@gmail.com>
  • Loading branch information
SethFalco and alenakhineika committed Jun 7, 2024
1 parent dcf97f7 commit 5aecbe8
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -566,12 +570,13 @@ export default class ConnectionController {
}
}

async disconnect(): Promise<boolean> {
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;

Expand All @@ -586,12 +591,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(
Expand All @@ -607,7 +624,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}.`
);
}

Expand Down

0 comments on commit 5aecbe8

Please sign in to comment.