Skip to content
16 changes: 15 additions & 1 deletion NotebookTestScript.dib
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,28 @@ return kernels;
1234
```

And the `POLYGLOT NOTEBOOK: VARIABLES` window should contain a variable with name `asdf`, value `1234` and kernel Name `csharp`
And the `POLYGLOT NOTEBOOK: VARIABLES` window should contain a variable with name `csharpVarFromJs`, value `1234` and kernel Name `csharp`

#!javascript

await kernel.root.send({ commandType: 'SubmitCode', command: { code: 'var csharpVarFromJs = 1234; Console.WriteLine(csharpVarFromJs);', targetKernelName: 'csharp' } });

#!markdown

# Execute the next cell. After execution, the output immediately below should be:

```
2345
```
And the `POLYGLOT NOTEBOOK: VARIABLES` window should contain a variable with name `csharpVarFromJsUsingApi`, value `1234` and kernel Name `csharp`

#!javascript

let command = new polyglotNotebooks.KernelCommandEnvelope(polyglotNotebooks.SubmitCodeType, { code: 'var csharpVarFromJsUsingApi = 2345; Console.WriteLine(csharpVarFromJsUsingApi);', targetKernelName: 'csharp' });
await kernel.root.send(command);

#!markdown

# Execute the next cell, the output should be displayed as HTML like so:

| Name | Salary |
Expand Down
33 changes: 15 additions & 18 deletions src/polyglot-notebooks-browser/src/kernel-client-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import * as dotnetInteractiveInterfaces from "./polyglot-notebooks-interfaces";
import { Kernel, IKernelCommandHandler } from "./polyglot-notebooks/kernel";
import { TokenGenerator } from "./polyglot-notebooks/tokenGenerator";
import { signalTransportFactory } from "./signalr-client";
import * as commandsAndEvents from "./polyglot-notebooks/commandsAndEvents";
import { createDefaultClientFetch } from "./clientFetch";
Expand Down Expand Up @@ -97,10 +96,10 @@ class InteractiveConsoleWrapper {
}
]
};
const eventEnvelope: commandsAndEvents.KernelEventEnvelope = {
eventType: commandsAndEvents.DisplayedValueProducedType,
event: displayedValue,
};
const eventEnvelope = new commandsAndEvents.KernelEventEnvelope(
commandsAndEvents.DisplayedValueProducedType,
displayedValue,
);

ClientEventQueueManager.addEventToClientQueue(this.clientFetch, this.commandToken, eventEnvelope);
}
Expand All @@ -116,14 +115,12 @@ export class KernelClientImpl implements dotnetInteractiveInterfaces.DotnetInter
receiver: IKernelCommandAndEventReceiver;
};
private _clientSideKernel: Kernel;
private _tokenGenerator: TokenGenerator;
private _configureRequire: (confing: any) => any;

constructor(parameters: KernelClientImplParameteres) {
this._clientFetch = parameters.clientFetch;
this._rootUrl = parameters.rootUrl;
this._kernelChannel = parameters.channel;
this._tokenGenerator = new TokenGenerator();
this._configureRequire = parameters.configureRequire;
this._clientSideKernel = parameters.clientSideKernel;
}
Expand Down Expand Up @@ -222,18 +219,18 @@ export class KernelClientImpl implements dotnetInteractiveInterfaces.DotnetInter
}

public async submitCode(code: string, targetKernelName?: string): Promise<string> {
let token: string = this._tokenGenerator.createToken();
let command: commandsAndEvents.SubmitCode = {
code: code,
targetKernelName: targetKernelName
}

await this._kernelChannel.sender.send({ command, commandType: commandsAndEvents.SubmitCodeType, token });
return token;
const commandEnvelpe = new commandsAndEvents.KernelCommandEnvelope(commandsAndEvents.SubmitCodeType, command);
await this._kernelChannel.sender.send(commandEnvelpe);
return commandEnvelpe.getOrCreateToken();
}

public async submitCommand(commandType: string, command?: any, targetKernelName?: string): Promise<string> {
let token: string = this._tokenGenerator.createToken();


if (!command) {
command = {};
Expand All @@ -242,9 +239,9 @@ export class KernelClientImpl implements dotnetInteractiveInterfaces.DotnetInter
if (targetKernelName) {
command.targetKernelName = targetKernelName;
}

await this._kernelChannel.sender.send({ command, commandType: <any>commandType, token });
return token;
const commandEnvelpe = new commandsAndEvents.KernelCommandEnvelope(<commandsAndEvents.KernelCommandType>commandType, command);
await this._kernelChannel.sender.send(commandEnvelpe);
return commandEnvelpe.getOrCreateToken();
}

public getConsole(commandToken: string): any {
Expand All @@ -268,10 +265,10 @@ export class KernelClientImpl implements dotnetInteractiveInterfaces.DotnetInter
const failedEvent: commandsAndEvents.CommandFailed = {
message: `${err}`
};
const eventEnvelope: commandsAndEvents.KernelEventEnvelope = {
eventType: commandsAndEvents.CommandFailedType,
event: failedEvent,
};
const eventEnvelope = new commandsAndEvents.KernelEventEnvelope(
commandsAndEvents.CommandFailedType,
failedEvent,
);
ClientEventQueueManager.addEventToClientQueue(this._clientFetch, commandToken, eventEnvelope);
}

Expand Down
29 changes: 15 additions & 14 deletions src/polyglot-notebooks-browser/tests/kernel-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe("polyglot-notebooks", () => {
}
});
let token = await client.submitCode("var a = 12");
expect(token).to.be.equal(transport!.commandsSent[0].token);
expect(token).to.be.equal(transport!.commandsSent[0].getOrCreateToken());
});

it("sends SubmitCode command", async () => {
Expand Down Expand Up @@ -181,10 +181,10 @@ describe("polyglot-notebooks", () => {
data: "Test"
};
let commandType: commandsAndEvents.KernelCommandType = <commandsAndEvents.KernelCommandType>"CustomCommand";
let commandEnvelopeIn: commandsAndEvents.KernelCommandEnvelope = {
let commandEnvelopeIn = new commandsAndEvents.KernelCommandEnvelope(
commandType,
command: commandIn
};
commandIn
);

transport!.fakeIncomingSubmitCommand(commandEnvelopeIn);
await delay(500);
Expand All @@ -211,30 +211,31 @@ describe("polyglot-notebooks", () => {
client.registerCommandHandler({
commandType: commandsAndEvents.SubmitCodeType, handle: (invocation) => {
let submitCode = <commandsAndEvents.SubmitCode>invocation.commandEnvelope.command;
let event: commandsAndEvents.KernelEventEnvelope = {
eventType: commandsAndEvents.CodeSubmissionReceivedType,
event: {
let event = new commandsAndEvents.KernelEventEnvelope(
commandsAndEvents.CodeSubmissionReceivedType,
{
code: submitCode.code
},
command: invocation.commandEnvelope
};
invocation.commandEnvelope
);

invocation.context.publish(event);
return Promise.resolve();
}
});


transport!.fakeIncomingSubmitCommand({ commandType: commandsAndEvents.SubmitCodeType, command: <commandsAndEvents.SubmitCode>{ code: "39 + 3" } });
const command = new commandsAndEvents.KernelCommandEnvelope(commandsAndEvents.SubmitCodeType, <commandsAndEvents.SubmitCode>{ code: "39 + 3" });
transport!.fakeIncomingSubmitCommand(command);

let eventIn: commandsAndEvents.CodeSubmissionReceived = {
code: "39 + 3"
};

let eventEnvelopeIn: commandsAndEvents.KernelEventEnvelope = {
eventType: commandsAndEvents.CodeSubmissionReceivedType,
event: eventIn
};
let eventEnvelopeIn = new commandsAndEvents.KernelEventEnvelope(
commandsAndEvents.CodeSubmissionReceivedType,
eventIn
);

await delay(500);
const publishedEvents = transport!.eventsPublished.filter(e => e.eventType === eventEnvelopeIn.eventType);
Expand Down
12 changes: 6 additions & 6 deletions src/polyglot-notebooks-browser/tests/testSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as rxjs from "rxjs";
import { DotnetInteractiveClient, KernelClientContainer } from "../src/polyglot-notebooks-interfaces";
import * as connection from "../src/polyglot-notebooks/connection";
import * as commandsAndEvents from "../src/polyglot-notebooks/commandsAndEvents";
import { TokenGenerator } from "../src/polyglot-notebooks/tokenGenerator";


export function asKernelClientContainer(client: DotnetInteractiveClient): KernelClientContainer {
Expand All @@ -18,10 +17,9 @@ export function configureFetchForKernelDiscovery(rootUrl: string) {
}

export class MockKernelCommandAndEventChannel {

private static _counter = 0;
Copy link
Contributor

@jonsequitur jonsequitur Jun 2, 2023

Choose a reason for hiding this comment

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

The monotonically incrementing integer has a high likelihood of colliding with the debug implementation of on the .NET side. Does this or equivalent ever run in the actual extension?

Copy link
Member Author

Choose a reason for hiding this comment

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

this is test only

public commandsSent: Array<commandsAndEvents.KernelCommandEnvelope>;
public eventsPublished: Array<commandsAndEvents.KernelEventEnvelope>;
private tokenGenerator = new TokenGenerator();
private eventObservers: { [key: string]: commandsAndEvents.KernelEventEnvelopeObserver } = {};
private commandHandlers: { [key: string]: commandsAndEvents.KernelCommandEnvelopeHandler } = {};

Expand Down Expand Up @@ -51,9 +49,11 @@ export class MockKernelCommandAndEventChannel {
this.sender = connection.KernelCommandAndEventSender.FromObserver(this._senderSubject);
this.receiver = connection.KernelCommandAndEventReceiver.FromObservable(this._receiverSubject);
}

private static createToken() {
return `token-${MockKernelCommandAndEventChannel._counter++}`
}
public subscribeToKernelEvents(observer: commandsAndEvents.KernelEventEnvelopeObserver) {
let key = this.tokenGenerator.createToken();
let key = MockKernelCommandAndEventChannel.createToken();
this.eventObservers[key] = observer;

return {
Expand All @@ -64,7 +64,7 @@ export class MockKernelCommandAndEventChannel {
}

public setCommandHandler(handler: commandsAndEvents.KernelCommandEnvelopeHandler) {
let key = this.tokenGenerator.createToken();
let key = MockKernelCommandAndEventChannel.createToken();
this.commandHandlers[key] = handler;
}

Expand Down
10 changes: 5 additions & 5 deletions src/polyglot-notebooks-vscode-common/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ export async function activate(context: vscode.ExtensionContext) {
if (!value) {
commandInvocation.context.fail('Input request cancelled');
} else {
commandInvocation.context.publish({
eventType: commandsAndEvents.InputProducedType,
event: {
commandInvocation.context.publish(new commandsAndEvents.KernelEventEnvelope(
commandsAndEvents.InputProducedType,
{
value
},
command: commandInvocation.commandEnvelope,
});
commandInvocation.commandEnvelope,
));
}
}
});
Expand Down
Loading