Skip to content

Commit 1712a84

Browse files
fix: send 1 notification when switching connection
Co-authored-by: Alena Khineika <[email protected]>
1 parent e56ae19 commit 1712a84

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/connectionController.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,13 @@ export default class ConnectionController {
327327
this._connectingConnectionId = connectionId;
328328
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
329329

330+
const nextConnectionName = this.getSavedConnectionName(connectionId);
331+
330332
if (this._activeDataService) {
331333
log.info('Disconnecting from the previous connection...', {
332334
connectionId: this._currentConnectionId,
333335
});
334-
await this.disconnect();
336+
await this.disconnect({ quiet: true });
335337
}
336338

337339
if (connectionAttempt.isClosed()) {
@@ -350,7 +352,7 @@ export default class ConnectionController {
350352
throw new Error('Connect failed: connectionOptions are missing.');
351353
}
352354

353-
this._statusView.showMessage('Connecting to MongoDB...');
355+
this._statusView.showMessage(`Connecting to ${nextConnectionName}...`);
354356
log.info('Connecting to MongoDB...', {
355357
connectionInfo: JSON.stringify(
356358
extractSecrets(this._connections[connectionId]).connectionInfo
@@ -418,7 +420,9 @@ export default class ConnectionController {
418420
}
419421

420422
log.info('Successfully connected', { connectionId });
421-
void vscode.window.showInformationMessage('MongoDB connection successful.');
423+
void vscode.window.showInformationMessage(
424+
`Set the active connection to ${nextConnectionName}.`
425+
);
422426

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

569-
async disconnect(): Promise<boolean> {
573+
/**
574+
* @param quiet Don't display non-error notifications to the user.
575+
* @returns If we disconnected from MongoDB successfully.
576+
*/
577+
async disconnect({ quiet = false } = {}): Promise<boolean> {
570578
log.info(
571579
'Disconnect called, currently connected to',
572580
this._currentConnectionId
573581
);
574582

583+
const disconnectingConnectionId = this._currentConnectionId;
575584
this._currentConnectionId = null;
576585
this._disconnecting = true;
577586

@@ -586,12 +595,24 @@ export default class ConnectionController {
586595
return false;
587596
}
588597

589-
this._statusView.showMessage('Disconnecting from current connection...');
598+
const disconnectingConnectionName = disconnectingConnectionId
599+
? this.getSavedConnectionName(disconnectingConnectionId)
600+
: 'MongoDB server';
601+
602+
this._statusView.showMessage(
603+
`Disconnecting from ${disconnectingConnectionName}...`
604+
);
590605

591606
try {
592607
// Disconnect from the active connection.
593608
await this._activeDataService.disconnect();
594-
void vscode.window.showInformationMessage('MongoDB disconnected.');
609+
610+
if (!quiet) {
611+
void vscode.window.showInformationMessage(
612+
`Disconnected from ${disconnectingConnectionName}.`
613+
);
614+
}
615+
595616
this._activeDataService = null;
596617

597618
void vscode.commands.executeCommand(
@@ -607,7 +628,7 @@ export default class ConnectionController {
607628
} catch (error) {
608629
// Show an error, however we still reset the active connection to free up the extension.
609630
void vscode.window.showErrorMessage(
610-
'An error occurred while disconnecting from the current connection.'
631+
`An error occurred while disconnecting from ${disconnectingConnectionName}.`
611632
);
612633
}
613634

0 commit comments

Comments
 (0)