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

Simplify code in JupyterConnection #13515

Merged
merged 2 commits into from
May 17, 2023
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
15 changes: 5 additions & 10 deletions src/kernels/jupyter/connection/jupyterConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ export class JupyterConnection implements IExtensionSyncActivationService {
private async createConnectionInfoFromUri(uri: string) {
// Prepare our map of server URIs
await this.updateServerUri(uri);
return createRemoteConnectionInfo(uri, this.getServerUri.bind(this));

const idAndHandle = extractJupyterServerHandleAndId(uri);
const server = this.uriToJupyterServerUri.get(uri);
return createRemoteConnectionInfo(uri, server, idAndHandle?.id);
}

private async validateRemoteConnection(connection: IJupyterConnection): Promise<void> {
Expand All @@ -96,7 +99,7 @@ export class JupyterConnection implements IExtensionSyncActivationService {
}
}

public async updateServerUri(uri: string): Promise<void> {
private async updateServerUri(uri: string): Promise<void> {
const idAndHandle = extractJupyterServerHandleAndId(uri);
if (idAndHandle) {
try {
Expand Down Expand Up @@ -124,12 +127,4 @@ export class JupyterConnection implements IExtensionSyncActivationService {
}
}
}

private getServerUri(uri: string): { server: IJupyterServerUri; serverId: string } | undefined {
const idAndHandle = extractJupyterServerHandleAndId(uri);
if (idAndHandle) {
const server = this.uriToJupyterServerUri.get(uri);
return server ? { server, serverId: idAndHandle.id } : undefined;
}
}
}
7 changes: 3 additions & 4 deletions src/kernels/jupyter/jupyterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ export async function handleExpiredCertsError(

export function createRemoteConnectionInfo(
uri: string,
getJupyterServerUri: (uri: string) => { server: IJupyterServerUri; serverId: string } | undefined
serverUri?: IJupyterServerUri,
serverId?: string
): IJupyterConnection {
let url: URL;
try {
Expand All @@ -101,9 +102,7 @@ export function createRemoteConnectionInfo(
throw err;
}

const info = getJupyterServerUri(uri);
const serverUri = info?.server;
const serverId = info?.serverId || '';
serverId = serverId || '';

const baseUrl = serverUri
? serverUri.baseUrl
Expand Down
30 changes: 5 additions & 25 deletions src/standalone/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

import { ExtensionMode, NotebookController, NotebookDocument, Uri, commands, window, workspace } from 'vscode';
import { JupyterConnection } from '../../kernels/jupyter/connection/jupyterConnection';
import { computeServerId, generateUriFromRemoteProvider } from '../../kernels/jupyter/jupyterUtils';
import { JupyterServerSelector } from '../../kernels/jupyter/connection/serverSelector';
import {
Expand All @@ -20,6 +19,7 @@ import { traceError } from '../../platform/logging';
import { IControllerRegistration } from '../../notebooks/controllers/types';
import { sendTelemetryEvent } from '../../telemetry';
import { noop } from '../../platform/common/utils/misc';
import { isRemoteConnection } from '../../kernels/types';

export const IExportedKernelServiceFactory = Symbol('IExportedKernelServiceFactory');
export interface IExportedKernelServiceFactory {
Expand Down Expand Up @@ -86,13 +86,8 @@ function waitForNotebookControllersCreationForServer(
return new Promise<void>((resolve) => {
controllerRegistration.onDidChange((e) => {
for (let controller of e.added) {
if (
controller.connection.kind === 'connectToLiveRemoteKernel' ||
controller.connection.kind === 'startUsingRemoteKernelSpec'
) {
if (controller.connection.serverId === serverId) {
resolve();
}
if (isRemoteConnection(controller.connection) && controller.connection.serverId === serverId) {
resolve();
}
}
});
Expand Down Expand Up @@ -165,7 +160,6 @@ export function buildApi(
addRemoteJupyterServer: async (providerId: string, handle: JupyterServerUriHandle) => {
sendApiUsageTelemetry(extensions, 'addRemoteJupyterServer');
await new Promise<void>(async (resolve) => {
const connection = serviceContainer.get<JupyterConnection>(JupyterConnection);
const selector = serviceContainer.get<JupyterServerSelector>(JupyterServerSelector);
const uri = generateUriFromRemoteProvider(providerId, handle);
const serverId = await computeServerId(uri);
Expand All @@ -176,23 +170,9 @@ export function buildApi(
controllerRegistration
);

await connection.updateServerUri(uri);
await selector.setJupyterURIToRemote(uri);

if (
controllerRegistration.all.find(
(metadata) =>
(metadata.kind === 'connectToLiveRemoteKernel' ||
metadata.kind === 'startUsingRemoteKernelSpec') &&
metadata.serverId === serverId
) !== undefined
) {
resolve();
return;
} else {
await controllerCreatedPromise;
resolve();
}
await controllerCreatedPromise;
resolve();
});
},
openNotebook: async (uri: Uri, kernelId: string) => {
Expand Down