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

API updates for new untitled notebooks and outputs #1361

Merged
merged 6 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
28 changes: 20 additions & 8 deletions src/dotnet-interactive-vscode/common/clientMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

import { KernelTransport } from './interfaces/contracts';
import { InteractiveClient } from "./interactiveClient";
import { ErrorOutputCreator, InteractiveClient } from "./interactiveClient";
import { ReportChannel, Uri } from "./interfaces/vscode-like";
import { vsCodeCellOutputToContractCellOutput } from './vscode/vscodeUtilities';

export interface ClientMapperConfiguration {
kernelTransportCreator: (notebookUri: Uri) => Promise<KernelTransport>,
createErrorOutput: ErrorOutputCreator,
diagnosticChannel?: ReportChannel,
}

export class ClientMapper {
private clientMap: Map<string, Promise<InteractiveClient>> = new Map();
private clientCreationCallbackMap: Map<string, (client: InteractiveClient) => Promise<void>> = new Map();

constructor(readonly kernelTransportCreator: (notebookPath: string) => Promise<KernelTransport>, readonly diagnosticChannel?: ReportChannel) {
constructor(readonly config: ClientMapperConfiguration) {
}

private writeDiagnosticMessage(message: string) {
if (this.diagnosticChannel) {
this.diagnosticChannel.appendLine(message);
if (this.config.diagnosticChannel) {
this.config.diagnosticChannel.appendLine(message);
}
}

static keyFromUri(uri: Uri): string {
return uri.fsPath;
return uri.toString();
}

tryGetClient(uri: Uri): Promise<InteractiveClient | undefined> {
Expand All @@ -41,8 +48,12 @@ export class ClientMapper {
this.writeDiagnosticMessage(`creating client for '${key}'`);
clientPromise = new Promise<InteractiveClient>(async (resolve, reject) => {
try {
const transport = await this.kernelTransportCreator(uri.fsPath);
const client = new InteractiveClient(transport);
const transport = await this.config.kernelTransportCreator(uri);
const config = {
transport,
createErrorOutput: this.config.createErrorOutput,
};
const client = new InteractiveClient(config);

let onCreate = this.clientCreationCallbackMap.get(key);
if (onCreate) {
Expand Down Expand Up @@ -98,6 +109,7 @@ export class ClientMapper {
}

isDotNetClient(uri: Uri): boolean {
return this.clientMap.has(uri.fsPath);
const key = ClientMapper.keyFromUri(uri);
return this.clientMap.has(key);
}
}
29 changes: 19 additions & 10 deletions src/dotnet-interactive-vscode/common/interactiveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,33 @@ import {
Cancel
} from './interfaces/contracts';
import { Eol } from './interfaces';
import { clearDebounce, createErrorOutput, createOutput } from './utilities';
import { clearDebounce, createOutput } from './utilities';

import * as vscodeLike from './interfaces/vscode-like';

export interface ErrorOutputCreator {
(message: string, outputId?: string): vscodeLike.NotebookCellOutput;
}

export interface InteractiveClientConfiguration {
readonly transport: KernelTransport,
readonly createErrorOutput: ErrorOutputCreator,
}

export class InteractiveClient {
private nextOutputId: number = 1;
private nextToken: number = 1;
private tokenEventObservers: Map<string, Array<KernelEventEnvelopeObserver>> = new Map<string, Array<KernelEventEnvelopeObserver>>();
private deferredOutput: Array<vscodeLike.NotebookCellOutput> = [];
private valueIdMap: Map<string, { idx: number, outputs: Array<vscodeLike.NotebookCellOutput>, observer: { (outputs: Array<vscodeLike.NotebookCellOutput>): void } }> = new Map<string, { idx: number, outputs: Array<vscodeLike.NotebookCellOutput>, observer: { (outputs: Array<vscodeLike.NotebookCellOutput>): void } }>();

constructor(readonly kernelTransport: KernelTransport) {
kernelTransport.subscribeToKernelEvents(eventEnvelope => this.eventListener(eventEnvelope));
constructor(readonly config: InteractiveClientConfiguration) {
config.transport.subscribeToKernelEvents(eventEnvelope => this.eventListener(eventEnvelope));
}

public tryGetProperty<T>(propertyName: string): T | null {
try {
return <T>((<any>this.kernelTransport)[propertyName]);
return <T>((<any>this.config.transport)[propertyName]);
}
catch {
return null;
Expand Down Expand Up @@ -134,7 +143,7 @@ export class InteractiveClient {
case CommandFailedType:
{
const err = <CommandFailed>eventEnvelope.event;
const errorOutput = createErrorOutput(err.message, this.getNextOutputId());
const errorOutput = this.config.createErrorOutput(err.message, this.getNextOutputId());
outputs.push(errorOutput);
reportOutputs();
reject(err);
Expand Down Expand Up @@ -190,7 +199,7 @@ export class InteractiveClient {
break;
}
}, configuration?.token).catch(e => {
const errorOutput = createErrorOutput('' + e, this.getNextOutputId());
const errorOutput = this.config.createErrorOutput('' + e, this.getNextOutputId());
outputs.push(errorOutput);
reportOutputs();
reject(e);
Expand Down Expand Up @@ -251,7 +260,7 @@ export class InteractiveClient {
};
token = token || this.getNextToken();
let disposable = this.subscribeToKernelTokenEvents(token, observer);
await this.kernelTransport.submitCommand(command, SubmitCodeType, token);
await this.config.transport.submitCommand(command, SubmitCodeType, token);
return disposable;
}

Expand All @@ -266,7 +275,7 @@ export class InteractiveClient {
}

dispose() {
this.kernelTransport.dispose();
this.config.transport.dispose();
}

private submitCommandAndGetResult<TEvent extends KernelEvent>(command: KernelCommand, commandType: KernelCommandType, expectedEventType: KernelEventType, token: string | undefined): Promise<TEvent> {
Expand Down Expand Up @@ -300,7 +309,7 @@ export class InteractiveClient {
break;
}
});
await this.kernelTransport.submitCommand(command, commandType, token);
await this.config.transport.submitCommand(command, commandType, token);
});
}

Expand All @@ -322,7 +331,7 @@ export class InteractiveClient {
break;
}
});
await this.kernelTransport.submitCommand(command, commandType, token);
await this.config.transport.submitCommand(command, commandType, token);
});
}

Expand Down
4 changes: 3 additions & 1 deletion src/dotnet-interactive-vscode/common/interactiveNotebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ export function isDotnetInteractiveLanguage(language: string): boolean {
return language.startsWith(notebookLanguagePrefix);
}

export const jupyterViewType = 'jupyter-notebook';

export function isJupyterNotebookViewType(viewType: string): boolean {
return viewType === 'jupyter-notebook';
return viewType === jupyterViewType;
}

export function languageToCellKind(language: string): NotebookCellKind {
Expand Down
12 changes: 7 additions & 5 deletions src/dotnet-interactive-vscode/common/interfaces/vscode-like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ export enum NotebookCellKind {
Code = 2
}

export const ErrorOutputMimeType = 'application/x.notebook.error-traceback';
export const ErrorOutputMimeType = 'application/vnd.code.notebook.error';

export interface NotebookCellOutputItem {
readonly mime: string;
readonly value: unknown;
readonly metadata?: Record<string, string | number | boolean | unknown>;
readonly value: Uint8Array | unknown;
readonly metadata?: { [key: string]: any };
}

export interface NotebookCellOutput {
readonly id: string;
readonly outputs: NotebookCellOutputItem[];
id: string;
outputs: NotebookCellOutputItem[];
metadata?: { [key: string]: any };
}

export enum NotebookCellRunState {
Expand All @@ -45,6 +46,7 @@ export interface NotebookCellMetadata {

export interface Uri {
fsPath: string;
scheme: string;
toString: () => string;
}

Expand Down
2 changes: 1 addition & 1 deletion src/dotnet-interactive-vscode/common/ipynbUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function getLanguageInfoMetadata(metadata: any): LanguageInfoMetadata {
return languageMetadata;
}

function mapIpynbLanguageName(name: string | undefined): string | undefined {
export function mapIpynbLanguageName(name: string | undefined): string | undefined {
if (name) {
// The .NET Interactive Jupyter kernel serializes the language names as "C#", "F#", and "PowerShell"; these
// need to be normalized to .NET Interactive kernel language names.
Expand Down
Loading