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

refactor: Cleanup DHC Service #171

Merged
merged 7 commits into from
Nov 13, 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
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,12 @@
"icon": "$(refresh)"
},
{
"command": "vscode-deephaven.createAuthenticatedClient",
"title": "Create Authenticated Client"
"command": "vscode-deephaven.createCoreAuthenticatedClient",
"title": "Create Core Authenticated Client"
},
{
"command": "vscode-deephaven.createDHEAuthenticatedClient",
"title": "Create DHE Authenticated Client"
},
{
"command": "vscode-deephaven.startServer",
Expand Down Expand Up @@ -700,7 +704,11 @@
"when": "false"
},
{
"command": "vscode-deephaven.createAuthenticatedClient",
"command": "vscode-deephaven.createCoreAuthenticatedClient",
"when": "false"
},
{
"command": "vscode-deephaven.createDHEAuthenticatedClient",
"when": "false"
}
],
Expand Down
7 changes: 6 additions & 1 deletion src/common/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ function cmd<T extends string>(cmd: T): `${typeof EXTENSION_ID}.${T}` {
export const CLEAR_SECRET_STORAGE_CMD = cmd('clearSecretStorage');
export const CONNECT_TO_SERVER_CMD = cmd('connectToServer');
export const CONNECT_TO_SERVER_OPERATE_AS_CMD = cmd('connectToServerOperateAs');
export const CREATE_AUTHENTICATED_CLIENT_CMD = cmd('createAuthenticatedClient');
export const CREATE_CORE_AUTHENTICATED_CLIENT_CMD = cmd(
'createCoreAuthenticatedClient'
);
export const CREATE_DHE_AUTHENTICATED_CLIENT_CMD = cmd(
'createDHEAuthenticatedClient'
);
export const CREATE_NEW_TEXT_DOC_CMD = cmd('createNewTextDoc');
export const DISCONNECT_EDITOR_CMD = cmd('disconnectEditor');
export const DISCONNECT_FROM_SERVER_CMD = cmd('disconnectFromServer');
Expand Down
57 changes: 42 additions & 15 deletions src/controllers/ExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
runSelectedLinesHoverProvider,
} from '../providers';
import {
DhcServiceFactory,
DheJsApiCache,
DheService,
DheServiceCache,
Expand All @@ -47,6 +46,7 @@ import {
SecretService,
ServerManager,
URLMap,
CoreJsApiCache,
} from '../services';
import type {
ConnectionState,
Expand All @@ -57,17 +57,19 @@ import type {
IDheService,
IDheServiceFactory,
IDhcService,
IDhServiceFactory,
IDhcServiceFactory,
IPanelService,
ISecretService,
IServerManager,
IToastService,
Lazy,
ServerConnectionPanelNode,
ServerConnectionPanelTreeView,
ServerConnectionTreeView,
ServerState,
ServerTreeView,
CoreAuthenticatedClient,
ICoreClientFactory,
CoreUnauthenticatedClient,
} from '../types';
import { ServerConnectionTreeDragAndDropController } from './ServerConnectionTreeDragAndDropController';
import { ConnectionController } from './ConnectionController';
Expand Down Expand Up @@ -115,15 +117,17 @@ export class ExtensionController implements Disposable {
readonly _config: IConfigService;

private _connectionController: ConnectionController | null = null;
private _coreCredentialsCache: URLMap<Lazy<DhcType.LoginCredentials>> | null =
private _coreClientCache: URLMap<CoreAuthenticatedClient> | null = null;
private _coreClientFactory: ICoreClientFactory | null = null;
private _coreJsApiCache: IAsyncCacheService<URL, typeof DhcType> | null =
null;
private _dheClientCache: URLMap<DheAuthenticatedClient> | null = null;
private _dheClientFactory: IDheClientFactory | null = null;
private _dheServiceCache: IAsyncCacheService<URL, IDheService> | null = null;
private _panelController: PanelController | null = null;
private _panelService: IPanelService | null = null;
private _pipServerController: PipServerController | null = null;
private _dhcServiceFactory: IDhServiceFactory | null = null;
private _dhcServiceFactory: IDhcServiceFactory | null = null;
private _dheJsApiCache: IAsyncCacheService<URL, DheType> | null = null;
private _dheServiceFactory: IDheServiceFactory | null = null;
private _secretService: ISecretService | null = null;
Expand Down Expand Up @@ -204,12 +208,10 @@ export class ExtensionController implements Disposable {
* Initialize panel controller.
*/
initializePanelController = (): void => {
assertDefined(this._coreCredentialsCache, 'coreCredentialsCache');
assertDefined(this._panelService, 'panelService');
assertDefined(this._serverManager, 'serverManager');

this._panelController = new PanelController(
this._coreCredentialsCache,
this._serverManager,
this._panelService
);
Expand Down Expand Up @@ -237,15 +239,23 @@ export class ExtensionController implements Disposable {
* Initialize user login controller.
*/
initializeUserLoginController = (): void => {
assertDefined(this._coreClientCache, 'coreClientCache');
assertDefined(this._coreClientFactory, 'coreClientFactory');
assertDefined(this._coreJsApiCache, 'coreJsApiCache');
assertDefined(this._dheClientCache, 'dheClientCache');
assertDefined(this._dheClientFactory, 'dheClientFactory');
assertDefined(this._secretService, 'secretService');
assertDefined(this._serverManager, 'serverManager');
assertDefined(this._toaster, 'toaster');

this._userLoginController = new UserLoginController(
this._coreClientCache,
this._coreClientFactory,
this._coreJsApiCache,
this._dheClientCache,
this._dheClientFactory,
this._secretService,
this._serverManager,
this._toaster
);

Expand Down Expand Up @@ -308,13 +318,23 @@ export class ExtensionController implements Disposable {
initializeServerManager = (): void => {
assertDefined(this._pythonDiagnostics, 'pythonDiagnostics');
assertDefined(this._outputChannel, 'outputChannel');
assertDefined(this._secretService, 'secretService');
assertDefined(this._toaster, 'toaster');

this._coreCredentialsCache = new URLMap<Lazy<DhcType.LoginCredentials>>();
this._coreJsApiCache = new CoreJsApiCache();
this._context.subscriptions.push(this._coreJsApiCache);

this._dheJsApiCache = new DheJsApiCache();
this._context.subscriptions.push(this._dheJsApiCache);

this._coreClientFactory = async (
url: URL
): Promise<CoreUnauthenticatedClient> => {
assertDefined(this._coreJsApiCache, 'coreJsApiCache');
const dhc = await this._coreJsApiCache.get(url);
return new dhc.CoreClient(url.toString()) as CoreUnauthenticatedClient;
};

this._dheClientFactory = async (
url: URL
): Promise<DheUnauthenticatedClient> => {
Expand All @@ -323,22 +343,23 @@ export class ExtensionController implements Disposable {
return createDheClient(dhe, getWsUrl(url));
};

this._coreClientCache = new URLMap();
this._dheClientCache = new URLMap();

this._panelService = new PanelService();
this._context.subscriptions.push(this._panelService);

this._dhcServiceFactory = new DhcServiceFactory(
this._coreCredentialsCache,
this._dhcServiceFactory = DhcService.factory(
this._coreClientCache,
this._panelService,
this._pythonDiagnostics,
this._outputChannel,
this._secretService,
this._toaster
);

this._dheServiceFactory = DheService.factory(
this._config,
this._coreCredentialsCache,
this._dheClientCache,
this._dheJsApiCache,
this._toaster
Expand All @@ -349,11 +370,12 @@ export class ExtensionController implements Disposable {

this._serverManager = new ServerManager(
this._config,
this._coreCredentialsCache,
this._coreClientCache,
this._dhcServiceFactory,
this._dheClientCache,
this._dheServiceCache,
this._outputChannel,
this._secretService,
this._toaster
);
this._context.subscriptions.push(this._serverManager);
Expand Down Expand Up @@ -639,8 +661,6 @@ export class ExtensionController implements Disposable {

// DHC ServerState
if (serverOrConnectionState.type === 'DHC') {
this._coreCredentialsCache?.delete(serverOrConnectionState.url);

await this._serverManager?.disconnectFromServer(
serverOrConnectionState.url
);
Expand Down Expand Up @@ -669,9 +689,16 @@ export class ExtensionController implements Disposable {
};

onOpenInBrowser = async (serverState: ServerState): Promise<void> => {
const psk = await this._secretService?.getPsk(serverState.url);
const serverUrl = new URL(serverState.url);

if (psk != null) {
serverUrl.searchParams.set('psk', psk);
}

vscode.commands.executeCommand(
'vscode.open',
vscode.Uri.parse(serverState.url.toString())
vscode.Uri.parse(serverUrl.toString())
);
};

Expand Down
16 changes: 3 additions & 13 deletions src/controllers/PanelController.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as vscode from 'vscode';
import type { dh as DhcType } from '@deephaven/jsapi-types';
import type {
ConnectionState,
IPanelService,
IServerManager,
Lazy,
VariableDefintion,
WorkerURL,
} from '../types';
Expand All @@ -16,7 +14,7 @@ import {
getPanelHtml,
Logger,
} from '../util';
import { DhcService, type URLMap } from '../services';
import { DhcService } from '../services';
import {
DEEPHAVEN_POST_MSG,
OPEN_VARIABLE_PANELS_CMD,
Expand All @@ -29,14 +27,9 @@ import { ControllerBase } from './ControllerBase';
const logger = new Logger('PanelController');

export class PanelController extends ControllerBase {
constructor(
coreCredentialsCache: URLMap<Lazy<DhcType.LoginCredentials>>,
serverManager: IServerManager,
panelService: IPanelService
) {
constructor(serverManager: IServerManager, panelService: IPanelService) {
super();

this._coreCredentialsCache = coreCredentialsCache;
this._panelService = panelService;
this._serverManager = serverManager;

Expand All @@ -53,9 +46,6 @@ export class PanelController extends ControllerBase {
);
}

private readonly _coreCredentialsCache: URLMap<
Lazy<DhcType.LoginCredentials>
>;
private readonly _panelService: IPanelService;
private readonly _serverManager: IServerManager;

Expand Down Expand Up @@ -84,7 +74,7 @@ export class PanelController extends ControllerBase {
// Respond to login credentials request from DH iframe
if (message === DEEPHAVEN_POST_MSG.loginOptionsRequest) {
const credentials =
await this._coreCredentialsCache.get(serverOrWorkerUrl)?.();
await this._serverManager.getWorkerCredentials(serverOrWorkerUrl);

if (credentials == null) {
logger.error('Failed to get credentials for worker', serverOrWorkerUrl);
Expand Down
Loading