-
Notifications
You must be signed in to change notification settings - Fork 10.3k
.NET to JS Streaming Interop #34817
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
.NET to JS Streaming Interop #34817
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
48649a9
.NET to JS Streaming Interop
TanayParikh 77ae1dc
Refactor WebView Impl
TanayParikh a18f82b
Update TransmitDataStreamToJS.cs
TanayParikh 75c4071
Merge branch 'main' into taparik/dotnetToJSStreaming
TanayParikh 44f71a5
Fix Build
TanayParikh 87903e5
Unit Tests
TanayParikh c2af324
E2E Tests
TanayParikh 3d99404
E2E test fixes
TanayParikh 2503d28
Remove dotNetToJSReceiveDotNetStreamReference Sync Tests
TanayParikh a3b90f1
Cleanup usings
TanayParikh 6c690cd
Merge branch 'main' into taparik/dotnetToJSStreaming
TanayParikh 588590d
Cleanup Tests
TanayParikh 1a8bc1d
IAsyncEnumerable Based Approach
TanayParikh 3756c06
Merge branch 'main' into taparik/dotnetToJSStreaming
TanayParikh b380039
IAsyncEnumerable Based Approach Cleanup
TanayParikh 0361961
Merge branch 'main' into taparik/dotnetToJSStreaming
TanayParikh 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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Buffers; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Microsoft.AspNetCore.Components | ||
{ | ||
/// <Summary> | ||
/// A stream that pulls each chunk on demand using JavaScript interop. This implementation is used for | ||
/// WebAssembly and WebView applications. | ||
/// </Summary> | ||
internal static class TransmitDataStreamToJS | ||
{ | ||
internal static async Task TransmitStreamAsync(IJSRuntime runtime, long streamId, DotNetStreamReference dotNetStreamReference) | ||
{ | ||
var buffer = ArrayPool<byte>.Shared.Rent(32 * 1024); | ||
|
||
try | ||
{ | ||
int bytesRead; | ||
while ((bytesRead = await dotNetStreamReference.Stream.ReadAsync(buffer)) > 0) | ||
{ | ||
await runtime.InvokeVoidAsync("Blazor._internal.receiveDotNetDataStream", streamId, buffer, bytesRead, null); | ||
} | ||
|
||
// Notify client that the stream has completed | ||
await runtime.InvokeVoidAsync("Blazor._internal.receiveDotNetDataStream", streamId, Array.Empty<byte>(), 0, null); | ||
} | ||
catch (Exception ex) | ||
{ | ||
try | ||
{ | ||
// Attempt to notify the client of the error. | ||
await runtime.InvokeVoidAsync("Blazor._internal.receiveDotNetDataStream", streamId, Array.Empty<byte>(), 0, ex.Message); | ||
} | ||
catch | ||
{ | ||
// JS Interop encountered an issue, unable to send error message to JS. | ||
} | ||
|
||
throw; | ||
} | ||
finally | ||
{ | ||
ArrayPool<byte>.Shared.Return(buffer, clearArray: true); | ||
|
||
if (!dotNetStreamReference.LeaveOpen) | ||
{ | ||
dotNetStreamReference.Stream?.Dispose(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
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
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
Oops, something went wrong.
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.
Why do we need the concurrent dictionary here? Shouldn't this already be scoped to the circuit?
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.
cancellationToken.Register
which is used to evict streams which aren't read from after the timeout, does not capture the Synchronization context.. Hence, without aConcurrentDictionary
we could run into potential issues when the cancellation token expires (and attempts to evict the stream) at the exact time we get a call intoTryClaimPendingStreamForSending
to evict the stream for consumption.