-
Notifications
You must be signed in to change notification settings - Fork 563
enable display update #388
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
667178c
introduce display handle
colombod 7ec51c1
fix messages
colombod 4dae66c
renamed as per PR feedback
colombod 6f9a947
split files
colombod 90493c0
test for event order
colombod cb642a0
change isLastValue to isEval for ValueProduced event
colombod bd2afd7
rename to IsReturnValue
colombod 5067eeb
rename Display to DisplayedValue and delete unused interface
colombod 8a885fe
restrict constructor preconditions
colombod 679798f
use nameof
colombod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
Microsoft.DotNet.Interactive/Commands/UpdateDisplayedValue.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // 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; | ||
|
|
||
| namespace Microsoft.DotNet.Interactive.Commands | ||
| { | ||
| public class UpdateDisplayedValue : KernelCommandBase | ||
| { | ||
| public UpdateDisplayedValue(FormattedValue formattedValue, string valueId) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(valueId)) | ||
| { | ||
| throw new ArgumentException("Value cannot be null or whitespace.", nameof(valueId)); | ||
| } | ||
| FormattedValue = formattedValue; | ||
| ValueId = valueId; | ||
| } | ||
|
|
||
| public FormattedValue FormattedValue { get; } | ||
| public string ValueId { get; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // 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; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.DotNet.Interactive.Commands; | ||
| using Microsoft.DotNet.Interactive.Rendering; | ||
|
|
||
| namespace Microsoft.DotNet.Interactive | ||
| { | ||
| public class DisplayedValue | ||
| { | ||
| private readonly string _displayId; | ||
| private readonly string _mimeType; | ||
|
|
||
| public DisplayedValue(string displayId, string mimeType) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(displayId)) | ||
| { | ||
| throw new ArgumentException("Value cannot be null or whitespace.", nameof(displayId)); | ||
| } | ||
|
|
||
| if (string.IsNullOrWhiteSpace(mimeType)) | ||
| { | ||
| throw new ArgumentException("Value cannot be null or whitespace.", nameof(mimeType)); | ||
| } | ||
| _displayId = displayId; | ||
| _mimeType = mimeType; | ||
| } | ||
|
|
||
| public void Update(object updatedValue) | ||
| { | ||
| var formatted = new FormattedValue( | ||
| _mimeType, | ||
| updatedValue.ToDisplayString(_mimeType)); | ||
|
|
||
| var kernel = KernelInvocationContext.Current.Kernel; | ||
|
|
||
| Task.Run(() => | ||
| kernel.SendAsync(new UpdateDisplayedValue(formatted, _displayId))) | ||
| .Wait(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,7 +71,13 @@ private void AddDirectiveMiddleware() | |
| context, | ||
| next), | ||
|
|
||
| _ => next(command, context) | ||
| UpdateDisplayedValue updateDisplayValue => | ||
| HandleUpdateDisplayValue( | ||
| updateDisplayValue, | ||
| context, | ||
| next), | ||
|
|
||
| _ => next(command, context) | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -161,33 +167,44 @@ private async Task HandleDisplayValue( | |
| KernelInvocationContext pipelineContext, | ||
| KernelPipelineContinuation next) | ||
| { | ||
| displayValue.Handler = context => | ||
| displayValue.Handler = invocationContext => | ||
| { | ||
| context.OnNext( | ||
| invocationContext.OnNext( | ||
| new ValueProduced( | ||
| displayValue.FormattedValue, | ||
| displayValue, | ||
| formattedValues: new[] { displayValue.FormattedValue })); | ||
| formattedValues: new[] { displayValue.FormattedValue }, | ||
| valueId: displayValue.ValueId)); | ||
|
|
||
| context.OnCompleted(); | ||
| invocationContext.OnCompleted(); | ||
|
|
||
| return Task.CompletedTask; | ||
| }; | ||
|
|
||
| displayValue.Handler = invocationContext => | ||
| await next(displayValue, pipelineContext); | ||
| } | ||
|
|
||
| private async Task HandleUpdateDisplayValue( | ||
| UpdateDisplayedValue displayedValue, | ||
| KernelInvocationContext pipelineContext, | ||
| KernelPipelineContinuation next) | ||
| { | ||
| displayedValue.Handler = invocationContext => | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this just over-write the Handler set in the line above this? |
||
| { | ||
| invocationContext.OnNext( | ||
| new ValueProduced( | ||
| displayValue.FormattedValue, | ||
| displayValue, | ||
| formattedValues: new[] { displayValue.FormattedValue })); | ||
| displayedValue.FormattedValue, | ||
| displayedValue, | ||
| formattedValues: new[] { displayedValue.FormattedValue }, | ||
| valueId: displayedValue.ValueId, | ||
| isUpdatedValue: true)); | ||
|
|
||
| invocationContext.OnCompleted(); | ||
|
|
||
| return Task.CompletedTask; | ||
| }; | ||
|
|
||
| await next(displayValue, pipelineContext); | ||
| await next(displayedValue, pipelineContext); | ||
| } | ||
|
|
||
| private Parser BuildDirectiveParser() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,6 +99,58 @@ public async Task Display_helper_can_be_called_without_specifying_class_name() | |
| v.Value.ToString().Contains("<b>hi!</b>")); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Displayed_value_can_be_updated() | ||
| { | ||
| var kernel = CreateKernel(); | ||
|
|
||
| await kernel.SendAsync(new SubmitCode("var d = display(b(\"hello\")); d.Update(b(\"world\"));")); | ||
|
|
||
| var formatted = | ||
| KernelEvents | ||
| .OrderBy(e => e.Timestamp) | ||
| .ValuesOnly() | ||
| .OfType<ValueProduced>() | ||
| .SelectMany(v => v.FormattedValues); | ||
|
|
||
| formatted | ||
| .Should() | ||
| .ContainSingle(v => | ||
| v.MimeType == "text/html" && | ||
| v.Value.ToString().Contains("<b>hello</b>")) | ||
| .And | ||
| .ContainSingle(v => | ||
| v.MimeType == "text/html" && | ||
| v.Value.ToString().Contains("<b>world</b>")); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assertion doesn't check that they're also in the correct order. |
||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Value_display_and_update_are_in_right_order() | ||
| { | ||
| var kernel = CreateKernel(); | ||
|
|
||
| await kernel.SendAsync(new SubmitCode("var d = display(b(\"hello\")); d.Update(b(\"world\"));")); | ||
|
|
||
| var formatted = | ||
| KernelEvents | ||
| .OrderBy(e => e.Timestamp) | ||
| .ValuesOnly() | ||
| .OfType<ValueProduced>() | ||
| .SelectMany(v => v.FormattedValues).ToList(); | ||
|
|
||
| var firstValue = formatted.Select((v, i) => new {v, i}).First(e => e.v.MimeType == "text/html" && | ||
| e.v.Value.ToString() | ||
| .Contains("<b>hello</b>")).i; | ||
|
|
||
| var updatedValue = formatted.Select((v, i) => new { v, i }).First(e => e.v.MimeType == "text/html" && | ||
| e.v.Value.ToString() | ||
| .Contains("<b>world</b>")).i; | ||
|
|
||
| updatedValue | ||
| .Should() | ||
| .BeGreaterOrEqualTo(firstValue); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Javascript_helper_emits_string_as_content_within_a_script_element() | ||
| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this should implicitly be true if an id is set. Would it not be a coding error if this is set to
truewhileidis set tonull?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is false if id is not set, but can be false even if Id is set