Skip to content

Commit

Permalink
fix: Update connection status on disconnect (#176)
Browse files Browse the repository at this point in the history
- Connection status wasn't updating on disconnect editor / server (I
added comments to the 2 new lines that fixed this)
- Moved connection related commands + handlers to ConnectionController.
This should be the rest of the code. Just moving existing functionality.

Testing
- Run a script against a server to create connection + associate editor
- Status bar item should update to show the server the active editor is
connected to
- In the "CONNECTIONS" panel, click the split plug icon beside the
editor node to "Disconnect Editor"
- Status bar item should update to "Deephaven: Disconnected"
- Repeat but instead of "Disconnect Editor", click the "Disconnect from
Server" icon in the "SERVERS" panel (the split plug beside the server
node)
  • Loading branch information
bmingles authored Nov 18, 2024
1 parent b095482 commit 6be6d89
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 100 deletions.
108 changes: 106 additions & 2 deletions src/controllers/ConnectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,59 @@ import {
Logger,
updateConnectionStatusBarItem,
} from '../util';
import { UnsupportedConsoleTypeError } from '../common';
import { getConnectionsForConsoleType } from '../services';
import {
CONNECT_TO_SERVER_CMD,
CONNECT_TO_SERVER_OPERATE_AS_CMD,
DISCONNECT_EDITOR_CMD,
DISCONNECT_FROM_SERVER_CMD,
SELECT_CONNECTION_COMMAND,
UnsupportedConsoleTypeError,
} from '../common';
import { ControllerBase } from './ControllerBase';

const logger = new Logger('ConnectionController');

export class ConnectionController implements Disposable {
export class ConnectionController extends ControllerBase implements Disposable {
constructor(
context: vscode.ExtensionContext,
serverManager: IServerManager,
outputChannel: vscode.OutputChannel,
toastService: IToastService
) {
super();

this._context = context;
this._serverManager = serverManager;
this._outputChannel = outputChannel;
this._toaster = toastService;

this.initializeConnectionStatusBarItem();
this.initializeServerManager();

/** Create server connection */
this.registerCommand(CONNECT_TO_SERVER_CMD, this.onConnectToServer);

/** Create server connection operating as another user */
this.registerCommand(
CONNECT_TO_SERVER_OPERATE_AS_CMD,
this.onConnectToServerOperateAs
);

/** Disconnect editor */
this.registerCommand(DISCONNECT_EDITOR_CMD, this.onDisconnectEditor);

/** Disconnect from server */
this.registerCommand(
DISCONNECT_FROM_SERVER_CMD,
this.onDisconnectFromServer
);

/** Select connection to run scripts against */
this.registerCommand(
SELECT_CONNECTION_COMMAND,
this.onPromptUserToSelectConnection
);
}

private readonly _context: vscode.ExtensionContext;
Expand Down Expand Up @@ -230,6 +264,76 @@ export class ConnectionController implements Disposable {
return dhService;
};

/**
* Handle connecting to a server
*/
onConnectToServer = async (
serverState: ServerState,
operateAsAnotherUser?: boolean
): Promise<void> => {
const languageId = vscode.window.activeTextEditor?.document.languageId;

// DHE servers need to specify the console type for each worker creation.
// Use the active editor's language id to determine the console type.
const workerConsoleType =
serverState.type === 'DHE' ? getConsoleType(languageId) : undefined;

this._serverManager?.connectToServer(
serverState.url,
workerConsoleType,
operateAsAnotherUser
);
};

/**
* Handle connecting to a server as another user.
* @param serverState
*/
onConnectToServerOperateAs = async (
serverState: ServerState
): Promise<void> => {
this.onConnectToServer(serverState, true);
};

/**
* Disconnect editor from active connections.
* @param uri
*/
onDisconnectEditor = (uri: vscode.Uri): void => {
this._serverManager?.disconnectEditor(uri);
this.updateConnectionStatusBarItem();
};

/**
* Handle disconnecting from a server.
*/
onDisconnectFromServer = async (
serverOrConnectionState: ServerState | ConnectionState
): Promise<void> => {
// ConnectionState (connection only disconnect)
if ('serverUrl' in serverOrConnectionState) {
this._serverManager?.disconnectFromServer(
serverOrConnectionState.serverUrl
);
return;
}

// DHC ServerState
if (serverOrConnectionState.type === 'DHC') {
await this._serverManager?.disconnectFromServer(
serverOrConnectionState.url
);
}
// DHE ServerState
else {
await this._serverManager?.disconnectFromDHEServer(
serverOrConnectionState.url
);
}

this.updateConnectionStatusBarItem();
};

/**
* Prompt user to select a connection and apply the selection. The options
* presented to the user consist of:
Expand Down
98 changes: 0 additions & 98 deletions src/controllers/ExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import type { dh as DhcType } from '@deephaven/jsapi-types';
import type { EnterpriseDhType as DheType } from '@deephaven-enterprise/jsapi-types';
import {
CLEAR_SECRET_STORAGE_CMD,
CONNECT_TO_SERVER_CMD,
CONNECT_TO_SERVER_OPERATE_AS_CMD,
CREATE_NEW_TEXT_DOC_CMD,
DISCONNECT_EDITOR_CMD,
DISCONNECT_FROM_SERVER_CMD,
DOWNLOAD_LOGS_CMD,
OPEN_IN_BROWSER_CMD,
REFRESH_SERVER_CONNECTION_TREE_CMD,
Expand All @@ -16,15 +12,13 @@ import {
RUN_SELECTION_COMMAND,
SEARCH_CONNECTIONS_CMD,
SEARCH_PANELS_CMD,
SELECT_CONNECTION_COMMAND,
START_SERVER_CMD,
STOP_SERVER_CMD,
VIEW_ID,
type ViewID,
} from '../common';
import {
assertDefined,
getConsoleType,
getEditorForUri,
getTempDir,
isInstanceOf,
Expand Down Expand Up @@ -52,7 +46,6 @@ import {
CoreJsApiCache,
} from '../services';
import type {
ConnectionState,
Disposable,
IAsyncCacheService,
IConfigService,
Expand Down Expand Up @@ -455,27 +448,9 @@ export class ExtensionController implements Disposable {
/** Clear secret storage */
this.registerCommand(CLEAR_SECRET_STORAGE_CMD, this.onClearSecretStorage);

/** Create server connection */
this.registerCommand(CONNECT_TO_SERVER_CMD, this.onConnectToServer);

/** Create server connection operating as another user */
this.registerCommand(
CONNECT_TO_SERVER_OPERATE_AS_CMD,
this.onConnectToServerOperateAs
);

/** Create new document */
this.registerCommand(CREATE_NEW_TEXT_DOC_CMD, this.onCreateNewDocument);

/** Disconnect editor */
this.registerCommand(DISCONNECT_EDITOR_CMD, this.onDisconnectEditor);

/** Disconnect from server */
this.registerCommand(
DISCONNECT_FROM_SERVER_CMD,
this.onDisconnectFromServer
);

/** Download logs and open in editor */
this.registerCommand(DOWNLOAD_LOGS_CMD, this.onDownloadLogs);

Expand All @@ -488,12 +463,6 @@ export class ExtensionController implements Disposable {
/** Run selected code in active editor */
this.registerCommand(RUN_SELECTION_COMMAND, this.onRunSelectedCode);

/** Select connection to run scripts against */
this.registerCommand(
SELECT_CONNECTION_COMMAND,
this._connectionController.onPromptUserToSelectConnection
);

/** Refresh server tree */
this.registerCommand(REFRESH_SERVER_TREE_CMD, this.onRefreshServerStatus);

Expand Down Expand Up @@ -634,37 +603,6 @@ export class ExtensionController implements Disposable {
this._toaster?.info('Stored secrets have been removed.');
};

/**
* Handle connecting to a server
*/
onConnectToServer = async (
serverState: ServerState,
operateAsAnotherUser?: boolean
): Promise<void> => {
const languageId = vscode.window.activeTextEditor?.document.languageId;

// DHE servers need to specify the console type for each worker creation.
// Use the active editor's language id to determine the console type.
const workerConsoleType =
serverState.type === 'DHE' ? getConsoleType(languageId) : undefined;

this._serverManager?.connectToServer(
serverState.url,
workerConsoleType,
operateAsAnotherUser
);
};

/**
* Handle connecting to a server as another user.
* @param serverState
*/
onConnectToServerOperateAs = async (
serverState: ServerState
): Promise<void> => {
this.onConnectToServer(serverState, true);
};

/**
* Create a new text document based on the given connection capabilities.
* @param dhService
Expand All @@ -683,42 +621,6 @@ export class ExtensionController implements Disposable {
this._serverManager?.setEditorConnection(editor, dhService);
};

/**
* Disconnect editor from active connections.
* @param uri
*/
onDisconnectEditor = (uri: vscode.Uri): void => {
this._serverManager?.disconnectEditor(uri);
};

/**
* Handle disconnecting from a server.
*/
onDisconnectFromServer = async (
serverOrConnectionState: ServerState | ConnectionState
): Promise<void> => {
// ConnectionState (connection only disconnect)
if ('serverUrl' in serverOrConnectionState) {
this._serverManager?.disconnectFromServer(
serverOrConnectionState.serverUrl
);
return;
}

// DHC ServerState
if (serverOrConnectionState.type === 'DHC') {
await this._serverManager?.disconnectFromServer(
serverOrConnectionState.url
);
}
// DHE ServerState
else {
await this._serverManager?.disconnectFromDHEServer(
serverOrConnectionState.url
);
}
};

/**
* Handle download logs command
*/
Expand Down

0 comments on commit 6be6d89

Please sign in to comment.