Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update connection status on disconnect #176

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the issue of status not updating on editor disconnect.

};

/**
* 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();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes issue of connection status not updating on server disconnect.

};

/**
* 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