这是indexloc提供的服务,不要输入任何密码
Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Deterministic Condition="'$(NCrunch)' == '1'">false</Deterministic>
</PropertyGroup>

<PropertyGroup Condition="$(TargetFramework.StartsWith('netstandard')) AND '$(OS)' == 'Windows_NT'">
<!-- the 2.1.503 F# compiler can produce PDBs that can't properly be converted, see https://github.com/Microsoft/visualfsharp/issues/5976 -->
<PublishWindowsPdb>false</PublishWindowsPdb>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions Microsoft.DotNet.Interactive.FSharp/FSharpKernel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ type FSharpKernel() =
let handleSubmitCode (codeSubmission: SubmitCode) (context: KernelInvocationContext) =
async {
let codeSubmissionReceived = CodeSubmissionReceived(codeSubmission.Code, codeSubmission)
context.OnNext(codeSubmissionReceived)
context.Publish(codeSubmissionReceived)
let result, errors =
try
script.Eval(codeSubmission.Code)
with
| ex -> Error(ex), [||]
if errors.Length > 0 then
let aggregateErrorMessage = System.String.Join("\n", errors)
context.OnNext(CommandFailed(aggregateErrorMessage, codeSubmission))
context.Publish(CommandFailed(aggregateErrorMessage, codeSubmission))
match result with
| Ok(Some(value)) ->
let value = value.ReflectionValue
let formattedValues = FormattedValue.FromObject(value)
context.OnNext(ReturnValueProduced(value, codeSubmission, formattedValues))
context.Publish(ReturnValueProduced(value, codeSubmission, formattedValues))
| Ok(None) -> ()
| Error(ex) -> context.OnError(ex)
context.OnNext(CodeSubmissionEvaluated(codeSubmission))
context.OnCompleted()
context.Publish(CodeSubmissionEvaluated(codeSubmission))
context.Complete()
}
override __.HandleAsync(command: IKernelCommand, _context: KernelInvocationContext): Task =
async {
Expand Down
10 changes: 5 additions & 5 deletions Microsoft.DotNet.Interactive.Jupyter/KernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ private static T UseHtml<T>(this T kernel)
.Trim();


context.OnNext(new Events.DisplayedValueProduced(
context.Publish(new Events.DisplayedValueProduced(
htmlContent,
context.Command,
formattedValues: new[]
{
new FormattedValue("text/html", htmlContent)
}));

context.OnCompleted();
context.Complete();
}
})
});
Expand Down Expand Up @@ -106,7 +106,7 @@ private static Command lsmagic()

supportedDirectives.Commands.AddRange(context.CurrentKernel.Directives);

context.OnNext(new Events.DisplayedValueProduced(supportedDirectives));
context.Publish(new Events.DisplayedValueProduced(supportedDirectives));

await context.CurrentKernel.VisitSubkernelsAsync(async k =>
{
Expand Down Expand Up @@ -137,15 +137,15 @@ private static Command javascript()
scriptContent))
.ToString();

context.OnNext(new Events.DisplayedValueProduced(
context.Publish(new Events.DisplayedValueProduced(
scriptContent,
context.Command,
formattedValues: new[]
{
new FormattedValue("text/html",
value)
}));
context.OnCompleted();
context.Complete();
}
})
};
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.DotNet.Interactive.Rendering/PocketView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Microsoft.DotNet.Interactive.Rendering
{
/// <summary>
/// Writes HTML using a C# DSL, bypasing the need for specialized parser and compiler infrastructure such as Razor or WebForms require.
/// Writes HTML using a C# DSL, bypassing the need for specialized parser and compiler infrastructure such as Razor or WebForms require.
/// </summary>
public class PocketView : DynamicObject, ITag
{
Expand Down
14 changes: 7 additions & 7 deletions Microsoft.DotNet.Interactive/KernelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private async Task HandleLoadExtension(
await extension.OnLoadAsync(invocationContext.HandlingKernel);
}

context.OnCompleted();
context.Complete();
};

await next(loadExtension, invocationContext);
Expand Down Expand Up @@ -155,8 +155,8 @@ private async Task HandleDirectivesAndSubmitCode(
{
submitCode.Handler = context =>
{
context.OnNext(new CodeSubmissionEvaluated(submitCode));
context.OnCompleted();
context.Publish(new CodeSubmissionEvaluated(submitCode));
context.Complete();
return Task.CompletedTask;
};

Expand All @@ -178,14 +178,14 @@ private async Task HandleDisplayValue(
{
displayValue.Handler = invocationContext =>
{
invocationContext.OnNext(
invocationContext.Publish(
new Events.DisplayedValueProduced(
displayValue.FormattedValue,
displayValue,
formattedValues: new[] { displayValue.FormattedValue },
valueId: displayValue.ValueId));

invocationContext.OnCompleted();
invocationContext.Complete();

return Task.CompletedTask;
};
Expand All @@ -200,15 +200,15 @@ private async Task HandleUpdateDisplayValue(
{
displayedValue.Handler = invocationContext =>
{
invocationContext.OnNext(
invocationContext.Publish(
new DisplayedValueUpdated(
displayedValue.FormattedValue,
valueId: displayedValue.ValueId,
command: displayedValue,
formattedValues: new[] { displayedValue.FormattedValue }
));

invocationContext.OnCompleted();
invocationContext.Complete();

return Task.CompletedTask;
};
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.DotNet.Interactive/KernelCommandPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task SendAsync(
}
catch (Exception exception)
{
context.OnNext(
context.Publish(
new CommandFailed(
exception,
command));
Expand Down
8 changes: 4 additions & 4 deletions Microsoft.DotNet.Interactive/KernelInvocationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Microsoft.DotNet.Interactive
{
public class KernelInvocationContext : IObserver<IKernelEvent>, IDisposable
public class KernelInvocationContext : IDisposable
{
private readonly KernelInvocationContext _parentContext;
private static readonly AsyncLocal<Stack<KernelInvocationContext>> _currentStack = new AsyncLocal<Stack<KernelInvocationContext>>();
Expand All @@ -30,7 +30,7 @@ private KernelInvocationContext(

public IKernelCommand Command { get; }

public void OnCompleted()
public void Complete()
{
IsCompleted = true;
_events.OnCompleted();
Expand All @@ -41,11 +41,11 @@ public void OnError(Exception exception)
_events.OnError(exception);
}

public void OnNext(IKernelEvent @event)
public void Publish(IKernelEvent @event)
{
if (_parentContext != null)
{
_parentContext.OnNext(@event);
_parentContext.Publish(@event);
}
else
{
Expand Down
14 changes: 3 additions & 11 deletions Microsoft.DotNet.Interactive/KernelStreamClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public Task Start()
streamKernelCommand = obj.ToObject<StreamKernelCommand>();
IKernelCommand command = null;

if (obj.TryGetValue("Command", out var commandValue))
if (obj.TryGetValue("command", StringComparison.InvariantCultureIgnoreCase ,out var commandValue))
{
command = DeserializeCommand(streamKernelCommand.CommandType, commandValue);
}

if (streamKernelCommand.CommandType == "Quit")
if (streamKernelCommand.CommandType == nameof(Quit))
{
return;
}
Expand Down Expand Up @@ -85,14 +85,6 @@ public Task Start()
},
streamKernelCommand?.Id ?? -1);
}
catch
{
Write(new CommandNotRecognized
{
Body = obj ?? (object)line
}, streamKernelCommand?.Id ?? -1);
}

}
});
}
Expand All @@ -102,7 +94,7 @@ private void Write(IKernelEvent e, int id)
var wrapper = new StreamKernelEvent
{
Id = id,
Event = JsonConvert.SerializeObject(e),
Event = e,
EventType = e.GetType().Name
};
var serialized = JsonConvert.SerializeObject(wrapper, _jsonSerializerSettings);
Expand Down
14 changes: 12 additions & 2 deletions Microsoft.DotNet.Interactive/StreamKernelCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
namespace Microsoft.DotNet.Interactive
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Newtonsoft.Json;

namespace Microsoft.DotNet.Interactive
{
public class StreamKernelCommand
internal class StreamKernelCommand
{
[JsonProperty("id")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need both this and the CamelCasePropertyNamesContractResolver?

Copy link
Member Author

Choose a reason for hiding this comment

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

the contract resolver is to force the properties of nested objects

Copy link
Contributor

Choose a reason for hiding this comment

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

Understood, but then why are the attributes needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

I prefer to have them if the y get serialized outside of the api. Ideally i'd stick them on the domain commands and events

Copy link
Contributor

Choose a reason for hiding this comment

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

If that happens then the contract on the nested objects would be broken anyway.

For this reason I often make serialization-specific wrapper classes internal. The serialized output is the contract, and the class is an implementation detail that isn't intended to be used for other purposes.

public int Id { get; set; }

[JsonProperty("commandType")]
public string CommandType { get; set; }

[JsonProperty("command")]
public object Command { get; set; }
}
}
6 changes: 4 additions & 2 deletions Microsoft.DotNet.Interactive/StreamKernelEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

namespace Microsoft.DotNet.Interactive
{
public class StreamKernelEvent
internal class StreamKernelEvent
{
[JsonProperty("id")]
public int Id { get; set; }

[JsonProperty("eventType")]
public string EventType { get; set; }

[JsonProperty("event")]
public string Event { get; set; }
public object Event { get; set; }
}
}
20 changes: 14 additions & 6 deletions Microsoft.DotNet.Interactive/StreamKernelExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
using System.IO;
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;
using System.Threading;
using Microsoft.DotNet.Interactive.Commands;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Microsoft.DotNet.Interactive
{
public static class StreamKernelExtensions
{
static int id = 0;
private static int _id;
private static readonly JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};

public static int WriteMessage(this StreamWriter writer, IKernelCommand command)
public static int WriteMessage(this StreamWriter writer, IKernelCommand command, int? correlationId = null)
{
var message = new StreamKernelCommand()
var message = new StreamKernelCommand
{
Id = Interlocked.Increment(ref id),
Id = correlationId?? Interlocked.Increment(ref _id),
CommandType = command.GetType().Name,
Command = command
};

writer.WriteLine(JsonConvert.SerializeObject(message));
writer.WriteLine(JsonConvert.SerializeObject(message, _jsonSerializerSettings));
writer.Flush();
return message.Id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{"id":1,"eventType":"CodeSubmissionReceived","event":{"code":"var x = 123;","value":"var x = 123;","command":{"code":"var x = 123;","targetKernelName":null,"submissionType":0}}}
{"id":1,"eventType":"CompleteCodeSubmissionReceived","event":{"code":"var x = 123;","command":{"code":"var x = 123;","targetKernelName":null,"submissionType":0}}}
{"id":1,"eventType":"CodeSubmissionEvaluated","event":{"code":"var x = 123;","command":{"code":"var x = 123;","targetKernelName":null,"submissionType":0}}}
{"id":2,"eventType":"CodeSubmissionReceived","event":{"code":"x","value":"x","command":{"code":"x","targetKernelName":null,"submissionType":0}}}
{"id":2,"eventType":"CompleteCodeSubmissionReceived","event":{"code":"x","command":{"code":"x","targetKernelName":null,"submissionType":0}}}
{"id":2,"eventType":"ReturnValueProduced","event":{"value":123,"formattedValues":[{"mimeType":"text/plain","value":"123"}],"valueId":null,"command":{"code":"x","targetKernelName":null,"submissionType":0}}}
{"id":2,"eventType":"CodeSubmissionEvaluated","event":{"code":"x","command":{"code":"x","targetKernelName":null,"submissionType":0}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":-1,"eventType":"CommandParseFailure","event":{"command":null,"body":"{ hello"}}
Loading